public void Connect(TcpClient _socket) { socket = _socket; socket.ReceiveBufferSize = dataBufferSize; socket.SendBufferSize = dataBufferSize; stream = socket.GetStream(); receivedData = new Packet(); receiveBuffer = new byte[dataBufferSize]; stream.BeginRead(receiveBuffer, 0, dataBufferSize, new AsyncCallback(ReceiveCallback), null); ServerSend.Welcome(id, "Welcome to the server!"); }
public static void JoinedGame(int _fromClient, Packet _packet) { int _clientIdCheck = _packet.ReadInt(); int _hostingId = _packet.ReadInt(); Console.WriteLine("Handling game joiner."); if (ConsistencyCheck(_fromClient, _clientIdCheck)) { Server.activeGames.Add(_hostingId, _fromClient); Server.clients[_fromClient].player.enemId = _hostingId; Server.clients[_hostingId].player.enemId = _fromClient; ServerSend.RemRoomToAll(_hostingId); Server.lobbyGames.Remove(_hostingId); ServerSend.SendJoinGame(_hostingId, _fromClient); } }
public static void WelcomeReceived(int _fromClient, Packet _packet) { int _clientIdCheck = _packet.ReadInt(); string _username = _packet.ReadString(); Console.WriteLine($"{Server.clients[_fromClient].tcp.socket.Client.RemoteEndPoint} connected successfully and is now player {_fromClient}."); Console.WriteLine($"Player name is {_username}."); if (_fromClient != _clientIdCheck) { Console.WriteLine($"Player \"{_username}\" (ID: {_fromClient}) has assumed the wrong Client ID ({_clientIdCheck})!"); } Server.clients[_fromClient].SendIntoLobby(_username); foreach (KeyValuePair <int, int> entry in Server.lobbyGames) { ServerSend.SendRoomToSingle(_fromClient, entry.Key, entry.Value); } }
private void Disconnect() { Console.WriteLine($"{tcp.socket.Client.RemoteEndPoint} has disconnected."); if (Server.lobbyGames.ContainsKey(player.id)) { Server.lobbyGames.Remove(player.id); ServerSend.RemRoomToAll(player.id); } if (Server.activeGames.ContainsKey(player.id)) { Server.activeGames.Remove(player.id); ServerSend.SendForfeit(player.enemId); } if (Server.activeGames.ContainsKey(player.enemId)) { Server.activeGames.Remove(player.enemId); ServerSend.SendForfeit(player.enemId); } player = null; tcp.Disconnect(); }
public static void Forfeit(int _fromClient, Packet _packet) { int _clientIdCheck = _packet.ReadInt(); int opponentId = Server.clients[_fromClient].player.enemId; Server.clients[_fromClient].player.enemId = 0; if (opponentId != 0) { Server.clients[opponentId].player.enemId = 0; } if (Server.activeGames.ContainsKey(_fromClient)) { Server.activeGames.Remove(_fromClient); } if (Server.activeGames.ContainsKey(opponentId)) { Server.activeGames.Remove(opponentId); } ServerSend.SendForfeit(opponentId); Console.WriteLine("Ending game due to forfeit."); }