This repository has been archived by the owner on Jun 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerClient.h
145 lines (99 loc) · 3.07 KB
/
ServerClient.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
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
#pragma once
#include <iostream>
#define QUIT_SIGNAL "/quit"
#define CONNECTION_TIMEOUT_PERIOD 5000
#define SOCKET_SET_POLL_PERIOD 10
#define CONNECTION_SUCCESSFUL "/success"
#define SHUTDOWN_SIGNAL "/shutdown"
class ServerClient {
private:
unsigned int serverPort;
unsigned int bufferSize;
std::string serverHostname;
IPaddress serverIP;
int ID;
std::string clientName;
TCPsocket clientSocket;
SDLNet_SocketSet socketSet;
bool shutdownClient;
void(*newMessageReaction)(ServerInfo*);
public:
ServerClient(std::string serverAddress, unsigned int serverPort, unsigned int bufferSize, void reaction(ServerInfo*), std::string name) {
this->shutdownClient = false;
this->serverHostname = serverAddress;
newMessageReaction = reaction;
this->serverPort = serverPort;
this->bufferSize = bufferSize;
clientName = name;
socketSet = SDLNet_AllocSocketSet(2);
if (socketSet == NULL)
std::cout << "Failed to allocate the socket set : " << SDLNet_GetError() << std::endl;
}
~ServerClient(){
SDLNet_TCP_Close(clientSocket);
SDLNet_FreeSocketSet(socketSet);
}
void connectToServer(){
int hostResolved = SDLNet_ResolveHost(&serverIP, serverHostname.c_str(), serverPort);
clientSocket = SDLNet_TCP_Open(&serverIP);
if (clientSocket) {
SDLNet_TCP_AddSocket(socketSet, clientSocket);
int activeSockets = SDLNet_CheckSockets(socketSet, CONNECTION_TIMEOUT_PERIOD);
int gotServerResponse = SDLNet_SocketReady(clientSocket);
if (gotServerResponse != 0) {
char* buffer = new char[bufferSize];
int serverResponseByteCount = SDLNet_TCP_Recv(clientSocket, buffer, bufferSize - 1);
ServerInfo* serverInfo = new ServerInfo(buffer);
ID = serverInfo->clientID;
delete[] buffer;
ClientInfo* info = new ClientInfo();
info->ID = ID;
info->message = new std::string(CONNECTION_SUCCESSFUL);
info->name = clientName;
sendToServer(info);
delete info;
delete serverInfo;
}
else
shutdownClient = true;
}
else {
shutdownClient = true;
}
}
void checkForIncomingMessages(){
ServerInfo* info;
int activeSockets = SDLNet_CheckSockets(socketSet, SOCKET_SET_POLL_PERIOD);
if (activeSockets != 0) {
int gotMessage = SDLNet_SocketReady(clientSocket);
if (gotMessage != 0) {
char* buffer = new char[bufferSize];
int serverResponseByteCount = SDLNet_TCP_Recv(clientSocket, buffer, bufferSize - 1);
if (serverResponseByteCount != 0) {
info = new ServerInfo(buffer);
if (*info->message == SHUTDOWN_SIGNAL)
shutdownClient = true;
newMessageReaction(info);
delete info;
}
delete[] buffer;
}
}
}
void sendToServer(ClientInfo* info) {
std::string str;
info->convertToString(str);
const char* message = str.c_str();
unsigned int msgLength = strlen(message);
SDLNet_TCP_Send(clientSocket, (void *)message, msgLength);
}
bool getShutdownStatus() {
return shutdownClient;
}
std::string getClientName() {
return clientName;
}
int getClientID() {
return ID;
}
};