public bool TryGetConnection(int connectionId, out LLApiConnection connection) { connection = null; lock (lockConnectionObject) { return Connections.TryGetValue(connectionId, out connection); } }
public void Listen() { bool stopListen = false; ushort messagesReaded = 0; while (!Disconnect && !stopListen) { messagesReaded++; int recConnectionId; int recChannelId; byte[] recBuffer = new byte[1024]; int bufferSize = 1024; int dataSize; byte error; NetworkReader reader; InputMessage message; NetworkEventType recNetworkEvent; try { recNetworkEvent = NetworkTransport.ReceiveFromHost(SocketId, out recConnectionId, out recChannelId, recBuffer, bufferSize, out dataSize, out error); if (error != (byte)NetworkError.Ok) { Debug.Log("Error [" + (NetworkError)error + "] receiving a Message from : " + SocketId + " | conID : " + recConnectionId + " | channel : " + recChannelId); } else { switch (recNetworkEvent) { case NetworkEventType.Nothing: stopListen = true; break; case NetworkEventType.ConnectEvent: string address; int remotePort; NetworkID networkID; NodeID nodeID; NetworkTransport.GetConnectionInfo(SocketId, recConnectionId, out address, out remotePort, out networkID, out nodeID, out error); if (error == (byte)NetworkError.Ok) { LLApiConnection connection = new LLApiConnection(recConnectionId, address, remotePort); addConnection(connection); } reader = new NetworkReader(recBuffer); message = new InputMessage(recConnectionId, SocketId, recChannelId, Subjects.Connect, reader, GameServer.getServerTime()); addInputMessageToQueue(message); Debug.Log("New Connection: " + SocketId + "[" + address + ":" + remotePort + "] con: " + recConnectionId + " Size: " + dataSize); break; case NetworkEventType.DataEvent: Debug.Log("Incoming message Dataevent received: " + SocketId + " con: " + recConnectionId + " Size: " + dataSize); reader = new NetworkReader(recBuffer); Subjects subject = (Subjects)reader.ReadUInt16(); message = new InputMessage(recConnectionId, SocketId, recChannelId, subject, reader, GameServer.getServerTime()); addInputMessageToQueue(message); break; case NetworkEventType.DisconnectEvent: Debug.Log("Remote client event disconnected: " + SocketId + " con: " + recConnectionId + " Size: " + dataSize); reader = new NetworkReader(recBuffer); message = new InputMessage(recConnectionId, SocketId, recChannelId, Subjects.Disconnect, reader, GameServer.getServerTime()); addInputMessageToQueue(message); removeConnection(recConnectionId); break; } } }catch(Exception e) { Debug.LogError("LLApiServer Error Receiving Data : " + e.Message); Debug.LogException(e); } finally { if (messagesReaded >= MaxMessages) { stopListen = true; } } } }
private void addConnection(LLApiConnection connection) { lock (lockConnectionObject) { Connections.Add(connection.ConnectionId, connection); } }