-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcpserver.h
112 lines (81 loc) · 2.36 KB
/
tcpserver.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @author Derendyaev Ilya, [email protected]
* @date 1.1.2019
* TCP receiver and receiver
**/
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <chrono>
#include <thread>
#include <vector>
#include <list>
#include <mutex>
#include <opencv2/opencv.hpp>
// net socket
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
using namespace std;
/// \brief Class using to TCP connect server and client
/// \details
///
class TCPServer
{
public:
enum State {kOk = 0, kError = -1, kSendHeader = -2, kNotInit = -3, kSendStream = -4}; ///< State this object
struct SocketAndState {
State state {kNotInit};
int socket{kNotInit};
mutex scOutLock;
mutex scInLock;
vector<char> inMsg;
vector<char> outMsg;
};
cv::Mat emptyImage;
std::list<unique_ptr<SocketAndState>> sockList;
std::mutex sockListLock;
void appendMsg(vector<char> data); ///< Add data to buffer
int sendMsgs(); ///< Send message
State sendMsg(SocketAndState& sc); ///< Send message
State sendMsg(SocketAndState& sc, std::vector<char>& outMsg);
int sendMsgs(std::vector<char>& outMsg);
void readMsg(int sc, vector<char>& msg);
void readMsg(SocketAndState& sc);
void clearMsgs();
void clearErrSockets(void);
void closeSockets(void);
int disconnect();
int connectSrv(const string& ipAddr, int port);
int connectSrv();
State createListener();
int waitConnect(SocketAndState& ss);
State getState();
inline bool isValid()
{
return valid;
}
TCPServer(const string& dstAddr, const u_int16_t dstPort);
~TCPServer();
private:
bool valid{false};
string destAddr;
int destPort{kNotInit};
int localPort{kNotInit};
// socket
int sockIn{kNotInit};
std::mutex sockInLock;
int sockOut{kNotInit};
struct sockaddr_in addr;
State currentState{kNotInit};
bool isConnect{false};
struct sockaddr_in serv_addr, peerAddr;
};
#endif //TCPCANSENDER_VSCOMSENDER_H