private void MessagingTick() { foreach (ConcurrentQueue <IncomingServerSessionMessage> incomingMessage in this._incomingMessages) { int count = incomingMessage.Count; for (int peerNo = 0; peerNo < count; ++peerNo) { IncomingServerSessionMessage result = (IncomingServerSessionMessage)null; incomingMessage.TryDequeue(out result); ServersideSession peer = result.Peer; NetworkMessage networkMessage = result.NetworkMessage; try { networkMessage.BeginRead(); byte id = networkMessage.ReadByte(); if (!peer.ContainsMessageHandler(id)) { networkMessage.ResetRead(); peer.OnMessageReceived((INetworkMessageReader)networkMessage); } else { MessageContract messageContract = MessageContract.CreateMessageContract(peer.GetMessageContractType(id)); messageContract.DeserializeFromNetworkMessage((INetworkMessageReader)networkMessage); peer.HandleMessage(messageContract); } } catch (Exception ex) { this.RemovePeer(peerNo); } } } }
public ServersideSession GetPeer(int peerIndex) { ServersideSession serversideSession = (ServersideSession)null; this._peers[peerIndex % this._readWriteThreadCount].TryGetValue(peerIndex, out serversideSession); return(serversideSession); }
private void RemovePeer(int peerNo) { ServersideSession peer = (ServersideSession)null; if (!this._peers[peerNo % this._readWriteThreadCount].TryRemove(peerNo, out peer)) { return; } peer.Socket.Close(); peer.OnDisconnected(); this.OnRemoveConnection(peer); this._disconnectedPeers[peerNo % this._readWriteThreadCount].TryRemove(peer.Index, out peer); }
private void IncomingConnectionsTick() { TcpSocket incomingConnection = this._serverSocket.GetLastIncomingConnection(); if (incomingConnection == null) { return; } ServersideSession serversideSession = this.OnNewConnection(); ++this._lastUniqueClientId; serversideSession.InitializeSocket(this._lastUniqueClientId, incomingConnection); this._peers[this._lastUniqueClientId % this._readWriteThreadCount].TryAdd(this._lastUniqueClientId, serversideSession); }
protected abstract void OnRemoveConnection(ServersideSession peer);
internal void AddDisconnectedPeer(ServersideSession peer) => this._disconnectedPeers[peer.Index % this._readWriteThreadCount].TryAdd(peer.Index, peer);