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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
| #include "ServerManager.h"
#include "ModelManager.h"
#define BUFFER_SIZE 65538
void ServerManager::error_print(const char * ptr) {
perror(ptr);
exit(EXIT_FAILURE);
}
bool ServerManager::initialization(const int& port, const cv::VideoCapture& cap, char* addr){
m_port = htons(port);
m_addr = addr;
m_cap = cap;
return true;
}
bool ServerManager::initialization(const int& port, char* addr){
m_port = htons(port);
m_addr = addr;
return true;
}
bool ServerManager::build_connect() {
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = m_addr?inet_addr(m_addr):INADDR_ANY;
server_addr.sin_port = m_port;
// create socket
m_server = socket(AF_INET, SOCK_STREAM, 0);
if(m_server < 0)
error_print("socket bind error");
// can reuse port
int on = 1;
if(setsockopt(m_server,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0)
error_print("setsockopt error");
// bind addr
if(bind(m_server, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
error_print("bind error");
// listen only one client
if(listen(m_server, 1) < 0)
error_print("listen failed");
cout << "ServerManager is listening, plesae wait..." << endl;
return true;
}
bool ServerManager::acceptClient(){
struct sockaddr_in accept_addr;
socklen_t accept_len = sizeof(accept_addr);
bzero(&accept_addr,sizeof(accept_addr));
// accept client connection
if((m_accept = accept(m_server,(struct sockaddr*)&accept_addr,&accept_len)) < 0)
error_print("accept error");
std::cout << "Connection established" << std::endl;
return true;
}
bool ServerManager::send_data_frame(ModelManager& model) {
char *json_output = nullptr;
json_output = model.createJson();
if (json_output == nullptr) {
return false;
}
// printf("send data %s\n", json_output);
// just send json_output, dont memcpy new char*!!! it wastes me two hours
// send json
int result = send(m_accept, json_output, strlen(json_output), 0);
if (result == -1) {
cout << "send fail" << endl;
return false;
}
return true;
}
bool ServerManager::receive_data_frame(Mat& frame, ModelManager& model) {
// recv frame size
int data_size;
if (recv(m_accept, &data_size, sizeof(data_size), 0) != sizeof(data_size)) {
// when client close, then close connection
close(m_accept);
cout << "close connection to client" << endl;
acceptClient(); // restart a new accept, to accept new connection
return false;
}
cout << data_size << endl;
// recv frame data
// char buf[data_size];
// std::vector<uchar> decode;
// int bytes_received = 0;
// do
// {
// int nBytes = recv(m_accept, buf, data_size - bytes_received, 0);
// for (int i = 0; i < nBytes; i++) // maybe can use memcpy, maybe faster
// {
// decode.emplace_back(buf[i]);
// }
// cout << bytes_received << endl;
// bytes_received += nBytes;
// } while (bytes_received < data_size);
char *recv_char = new char[data_size];
std::vector<uchar> decode(data_size, 0);
int index = 0;
int bytes_received = 0;
int count = data_size;
while (count > 0)// if count >= 0, dead loop
{
int iRet = recv(m_accept, recv_char, count, 0);
if (index >= data_size) index = data_size;
memcpy(&decode[index], recv_char , iRet);
index += iRet;
if (!iRet) { return -1; }
count -= iRet;
}
// decode message
frame = imdecode(decode, cv::IMREAD_COLOR);
// push into Model's queueMat
model.mtxQueueImg.lock();
model.queueMat.push(frame);
model.mtxQueueImg.unlock();
return true;
}
bool ServerManager::free_connect() {
m_cap.release();
close(m_accept);
close(m_server);
return true;
}
|