File tree Expand file tree Collapse file tree 2 files changed +19
-9
lines changed Expand file tree Collapse file tree 2 files changed +19
-9
lines changed Original file line number Diff line number Diff line change @@ -126,7 +126,8 @@ void Server::handleClientMessages() {
126
126
ClientInfo &client = m_info.clients ()[i];
127
127
if (m_selector.isReady (*client.tcpSocket )) {
128
128
sf::Packet packet;
129
- if (client.tcpSocket ->receive (packet) == sf::Socket::Done) {
129
+ sf::Socket::Status status = client.tcpSocket ->receive (packet);
130
+ if (status == sf::Socket::Done) {
130
131
Network::Command command;
131
132
packet >> command;
132
133
@@ -138,23 +139,30 @@ void Server::handleClientMessages() {
138
139
it.second (client, packet);
139
140
140
141
if (command == Network::Command::ClientDisconnect) {
141
- m_selector.remove (*client.tcpSocket );
142
- m_info.removeClient (client.id );
143
-
144
- if (m_isSingleplayer && m_info.clients ().size () == 0 ) {
145
- m_tcpListener.close ();
146
- m_isRunning = false ;
147
- }
148
-
142
+ disconnectClient (client);
149
143
--i;
150
144
}
151
145
}
152
146
}
153
147
}
148
+ else if (status == sf::Socket::Disconnected) {
149
+ disconnectClient (client);
150
+ --i;
151
+ }
154
152
}
155
153
}
156
154
}
157
155
156
+ void Server::disconnectClient (ClientInfo &client) {
157
+ m_selector.remove (*client.tcpSocket );
158
+ m_info.removeClient (client.id );
159
+
160
+ if (m_isSingleplayer && m_info.clients ().size () == 0 ) {
161
+ m_tcpListener.close ();
162
+ m_isRunning = false ;
163
+ }
164
+ }
165
+
158
166
void Server::sendToAllClients (sf::Packet &packet) {
159
167
for (ClientInfo &client : m_info.clients ()) {
160
168
client.tcpSocket ->send (packet);
Original file line number Diff line number Diff line change @@ -66,6 +66,8 @@ class Server {
66
66
void handleNewConnections ();
67
67
void handleClientMessages ();
68
68
69
+ void disconnectClient (ClientInfo &client);
70
+
69
71
bool m_isRunning = false ;
70
72
bool m_isSingleplayer = false ;
71
73
You can’t perform that action at this time.
0 commit comments