private void dropClient(UserThread client) { client.Stop(); var disconnectPacket = new Packet(NetConstants.PacketTypes.UserLeft, client.GetBytes()); sendPacketToAllClients(disconnectPacket); }
private void sendPacketToClient(Packet p, UserThread client) { if (client.StopCalled) { return; } try { var stream = client.TcpClient.GetStream(); stream.Write(p.Bytes, 0, p.TotalSize); } catch { dropClient(client); } }
private void handleIncomingPacket(UserThread sender, Packet packet) { int newOffset; if (packet.PacketType == NetConstants.PacketTypes.UserRegister) { var registerString = PacketHelper.GetStringFromBytes(packet.DataBytes, 0, out newOffset); if (registerString == NetConstants.UserRegisterString && !sender.IsRegistered) { var name = PacketHelper.GetStringFromBytes(packet.DataBytes, newOffset, out newOffset); sender.Username = name; sender.IsRegistered = true; var joinPacket = PacketHelper.CreateUserJoinedPacket(sender); sendPacketToAllClientsExcept(joinPacket, sender.UserGuid, true); var clientsPacket = PacketHelper.CreateUsersAlreadyHerePacket(AllRegisteredClients); //Send info of all clients already in lobby to new client sendPacketToClient(clientsPacket, sender); } } if (packet.PacketType == NetConstants.PacketTypes.UserLeft) { var leftPacket = PacketHelper.CreateUserLeftPacket(sender); sendPacketToAllClientsExcept(leftPacket, sender.UserGuid, true); } if (packet.PacketType == NetConstants.PacketTypes.MemoryData && sender.IsRegistered) { //Add the clients Guid in front of the data var newData = PacketHelper.ConcatBytes(sender.UserGuid.ToByteArray(), packet.DataBytes); var newPacket = new Packet(NetConstants.PacketTypes.MemoryData, newData); sendPacketToAllClientsExcept(newPacket, sender.UserGuid, true); } if (packet.PacketType == NetConstants.PacketTypes.UserChat) { var text = PacketHelper.GetStringFromBytes(packet.DataBytes, 0, out newOffset); var chatPacket = PacketHelper.CreateServerToUserChatMessage(sender, text); sendPacketToAllClients(chatPacket, true); } }
private void clientListenLoop(UserThread client) { var stream = client.TcpClient.GetStream(); var size = client.TcpClient.ReceiveBufferSize; try { while (!client.StopCalled) { byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); handleIncomingPacket(client, new Packet(buffer)); } } catch (IOException) { //Stream was closed, most likely due to the server shutting down client.Stop(); } }
private void acceptIncomingConnections(TcpListener tcp) { while (!_stopCalled) { try { var client = tcp.AcceptTcpClient(); var clientThread = new UserThread(client); var registerPacket = PacketHelper.CreateServerRegistrationPacket(clientThread.UserGuid); sendPacketToClient(registerPacket, clientThread); _clients.Add(clientThread); clientThread.Start(clientListenLoop, clientStopped); } catch (SocketException) { break; } } sendShutdownToAll(); }
public static Packet CreateServerToUserChatMessage(UserThread sender, string text) { var data = ConcatBytes(sender.GetBytes(), GetStringBytes(text)); return(new Packet(NetConstants.PacketTypes.ServerToUserChat, data)); }
public static Packet CreateUserLeftPacket(UserThread user) { return(new Packet(NetConstants.PacketTypes.UserLeft, user.GetBytes())); }
private void clientStopped(UserThread client) { client.TcpClient.Close(); }