public void OnDisconnect() { ClientListUpdate.Invoke(new List <CharacterData>()); m_initState.Init(false); m_currentState = m_initState; m_TCPClient.Unsubscribe(this); #if DEBUG_LOG Debug.Log("Lobby : lost connection to game server. Trying to reconnect..."); #endif // DEBUG_LOG }
public void ReceivePacket(tcp.TCPToolkit.Packet packet) { ServerStartsMessage ready = common.serialization.IConvertible.CreateFromBytes <ServerStartsMessage>(packet.Data.ArraySegment()); if (ready != null) { #if DEBUG_LOG Debug.Log("Received server start message."); #endif // DEBUG_LOG m_serverSentSignal = true; return; } // loads other players in lobby, receives message from server indicating a new player joined CharacterListMessage clientList = common.serialization.IConvertible.CreateFromBytes <CharacterListMessage>(packet.Data.ArraySegment()); if (clientList != null) { Debug.Log("Received " + clientList.PlayerCharacters.Value.Count + " characters from server"); foreach (common.serialization.types.String id in clientList.PlayerCharacters.Value.Values) { Debug.Log("Fetching character " + id.Value + " from microservice"); // fetch character data from microservice string strID = id.Value; m_characterService.GetCharacter(strID, (CharacterData character) => { Debug.Log("Got character from " + character.PlayerID + " : " + character.Name); m_clientCharacters[character.PlayerID.GetHashCode()] = character; ClientListUpdate.Invoke(new List <CharacterData>(m_clientCharacters.Values)); }); } return; } System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); ServerInitMessage start = common.serialization.IConvertible.CreateFromBytes <ServerInitMessage>(packet.Data.ArraySegment()); watch.Stop(); if (start != null) { #if DEBUG_LOG Debug.Log("Time elapsed for world deserialization : " + watch.ElapsedMilliseconds + " ms"); #endif // DEBUG_LOG m_awaitedInitMessage = start; return; } }
public void OnClientDisconnect(string clientID, Socket handler, ClientListUpdate clientListUpdateHandler) { //separate disconnect function as the functionality for handling a disconnect is the same //even if the client crashes/is forcibly closed or even if it closes gracefully //(by sending a ClientQuit packet) lock (_messsageQueuesLocker) { ClientListUpdateEvent -= clientListUpdateHandler; connectedClients.Remove(clientID.ToString()); disconnectedClients.Add(clientID.ToString()); } Message clientDisconnectedListMessage = new Message { ReceiverClientID = clientID, MessageType = MessageType.ClientDisconnectedList, MessageBody = string.Join(",", disconnectedClients.ToArray()) }; Message im = new Message { SenderClientID = null, ReceiverClientID = null, MessageType = MessageType.ClientQuitUpdate, MessageBody = clientID.ToString(), Broadcast = false }; handler.Shutdown(SocketShutdown.Both); handler.Close(); foreach (Socket s in connectedClients.Values) { SendPacket(clientDisconnectedListMessage, s); SendPacket(im, s); } ClientListUpdateEvent?.Invoke(); }