hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef TCP_SERVER_H
#define TCP_SERVER_H
 
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
 
#include <algorithm>
#include <atomic>
#include <cctype>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <list>
 
#include "logger/log.h"
 
using namespace std;
 
#define MAXPACKETSIZE 8192
#define MAX_CLIENT 1000
 
using RecvCallBack = add_pointer<void(int sockfd, char* buffer, int size)>::type;
 
class TCPServer
{
  public:
    TCPServer() : sockfd(-1), quit_(false), exited_(true), serverAddress{0}, clientAddress{0}, callback_(nullptr){};
    virtual ~TCPServer();
 
    int Send(int cilent_socket, char* buff, int size);
    int Process(int port);
 
    void RegisterRecvCallBack(RecvCallBack cb)
    {
        callback_ = cb;
    }
    void UnRegisterRecvCallBack()
    {
        callback_ = nullptr;
    }
    void SaveExit();
    bool Exited() const
    {
        return exited_.load();
    }
 
  private:
    void Accepted();
    int Recvieve(int cilent_socket);
 
  private:
    int sockfd;
    std::atomic_bool quit_;
    std::atomic_bool exited_;
    struct sockaddr_in serverAddress;
    struct sockaddr_in clientAddress;
    RecvCallBack callback_;
    std::unique_ptr<std::thread> accept_thread_;
 
  public:
    std::vector<std::unique_ptr<std::thread>> recv_threads_;
    std::list<std::thread::id> recv_threads_finished_id_;
};
 
#endif