-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandshakeHandler.cpp
More file actions
179 lines (130 loc) · 5.47 KB
/
HandshakeHandler.cpp
File metadata and controls
179 lines (130 loc) · 5.47 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Created by Sierra Kilo on 10-Mar-19.
//
#include <cstdio>
#ifdef __WIN32
#include <ws2tcpip.h>
#endif
#include <string>
#include "HandshakeHandler.h"
#include "RequestHandler.h"
HandshakeHandler::HandshakeHandler(AppState* appState) {
this->appState = appState;
this->stopHandshakeListener = false;
}
void HandshakeHandler::acceptNewConnection(SOCKET newSocket, nlohmann::json inMsgData) {
printf("A new connection has been accepted!\n");
// Join the previous Request Listener Thread before starting a new one!
if (this->requestListenerThread.joinable()) {
this->requestListenerThread.join();
}
// set the inConnection status and ip bond at the RemoteServer
this->appState->setInConnectionValue(true);
this->appState->setIpBondAddress(inMsgData["client_ip"]);
// send a success response to the client to inform him that the
// make_connection request has been accepted
std::map<std::string, std::string> msgData;
std::string temp_s = JSON::convertJsonToString(JSON::prepareJsonReply("SUCCESS", msgData));
const char *outboundMsg = temp_s.c_str();
ConnectionHandler::sendMsg(newSocket, outboundMsg);
// start the request handler for the accepted client
this->requestHandler = new RequestHandler(this->appState, newSocket);
this->requestListenerThread = std::thread(&RequestHandler::requestListener, this->requestHandler);
}
void HandshakeHandler::rejectNewConnection(SOCKET rejSocket) {
// reply to the client by sending an error response with the appropriate message
std::map<string, string> msgData;
msgData["fail_message"] = "Connection Denied! Server is already in a connection with a client.";
std::string temp_s = JSON::convertJsonToString(JSON::prepareJsonReply("FAIL", msgData));
const char *outboundMsg = temp_s.c_str();
ConnectionHandler::sendMsg(rejSocket, outboundMsg);
}
void HandshakeHandler::handshakeListener() {
SOCKET s;
struct sockaddr_in server , client;
socklen_t c;
unsigned short PORT = 7890;
printf("\nInitialising socket...");
if (sockInit() != 0) {
printf("Socket initialization failed.");
}
printf("Socket initialised!\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == 0) {
// 0 means INVALID_SOCKET in WinSock
printf("Error. Could not create socket.");
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
// // ******* Start - Part used to reuse socket. Useful when application crashes during testing,
// // causing the port to remain locked *******
// int yes=1;
// //char yes='1'; // Solaris people use this
//
// // lose the pesky "Address already in use" error message
// if (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
// perror("setsockopt");
// exit(1);
// }
// // ******* End *******
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == -1) {
printf("Socket bind failed.");
exit(EXIT_FAILURE);
}
puts("Socket bind done");
// // ************************************************************* TEST *************************************************************
// // set options to new_socket
// int val = 1;
//
// // After creating the socket
// if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&val, sizeof(val))){
// fprintf(stderr, "setsockopt failure : %d", errno);
// }
// // ************************************************************* TEST *************************************************************
//Listen to incoming connections
listen(s , 3);
//Accept an incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while (!this->stopHandshakeListener) {
cout << "==========> HandshakeListener LOOP" << endl;
SOCKET newSocket = accept(s , (struct sockaddr *)&client, &c);
// receive the first message the client sends after accept(),
// which by protocol is the "make_connection" request
#define DEFAULT_BUFLEN 1000
int recv_size;
char recv_buf[DEFAULT_BUFLEN] = {0};
recv_size = ConnectionHandler::recvMsg(newSocket, recv_buf);
if (recv_size == -1) {
// -1 means SOCKET_ERROR in WinSock
puts("Receive failed");
} else {
// receive was successful
nlohmann::json jsonReceivedMsg = nlohmann::json::parse(recv_buf);
string request = jsonReceivedMsg["request"];
if (request == "INITIALIZE_NEW_CONNECTION") {
// if the server is not already dedicated to a connection with a client
if (!this->appState->isInConnection()) {
acceptNewConnection(newSocket, jsonReceivedMsg["data"]);
} else {
// if the server is already in a connection with a client,
// reject the connection request
rejectNewConnection(newSocket);
}
} else {
cout << "*** ERROR: The client has broken the defined protocol! ***" << endl;
}
}
}
}
HandshakeHandler::~HandshakeHandler() {
if (requestListenerThread.joinable()) {
requestListenerThread.join();
}
delete this->requestHandler;
cout << "==========> HandshakeHandler DESTRUCTOR!" << endl;
}