示例#1
0
        private void SendUnreliableDataPackets()
        {
            ArraySegment <byte> buffer;

            while (_queuedUnreliableTransmissions.Read(out buffer))
            {
                SentTraffic.Update(buffer.Count);

                SendUnreliable(buffer);

                _byteBufferPool.Put(buffer.Array);
            }
        }
示例#2
0
        private ushort?ProcessReceivedPacket(ArraySegment <byte> data)
        {
            var reader = new PacketReader(data);

            MessageTypes header;

            if (!reader.ReadPacketHeader(out header))
            {
                Log.Warn("Discarding packet - incorrect magic number.");
                return(null);
            }

            Log.Trace("Received Packet type: {0}", header);

            switch (header)
            {
            case MessageTypes.VoiceData:
                if (CheckSessionId(ref reader, header))
                {
                    _voiceReceiver.ReceiveVoiceData(ref reader);
                    _recvVoiceData.Update(reader.Read.Count);
                }
                break;

            case MessageTypes.TextData:
                if (CheckSessionId(ref reader, header))
                {
                    _textReceiver.ProcessTextMessage(ref reader);
                    _recvTextData.Update(reader.Read.Count);
                }
                break;

            case MessageTypes.HandshakeResponse:
                _serverNegotiator.ReceiveHandshakeResponseHeader(ref reader);
                _peers.ReceiveHandshakeResponseBody(ref reader);
                _recvHandshakeResponse.Update(reader.Read.Count);

                if (_serverNegotiator.LocalId.HasValue)
                {
                    OnServerAssignedSessionId(_serverNegotiator.SessionId, _serverNegotiator.LocalId.Value);
                }

                break;

            case MessageTypes.RemoveClient:
                if (CheckSessionId(ref reader, header))
                {
                    _peers.ProcessRemoveClient(ref reader);
                    _recvRemoveClient.Update(reader.Read.Count);
                }
                break;

            case MessageTypes.ClientState:
                if (CheckSessionId(ref reader, header))
                {
                    _peers.ProcessClientState(null, ref reader);
                    _recvClientState.Update(reader.Read.Count);
                }
                break;

            case MessageTypes.DeltaChannelState:
                if (CheckSessionId(ref reader, header))
                {
                    _peers.ProcessDeltaChannelState(ref reader);
                    _recvDeltaState.Update(reader.Read.Count);
                }
                break;

            case MessageTypes.ErrorWrongSession:
            {
                var session = reader.ReadUInt32();
                if (_serverNegotiator.SessionId != session)
                {
                    FatalError(string.Format("Kicked from session - wrong session ID. Mine:{0} Theirs:{1}", _serverNegotiator.SessionId, session));
                }
            }
            break;

            case MessageTypes.HandshakeP2P:
                if (CheckSessionId(ref reader, header))
                {
                    ushort id;
                    reader.ReadhandshakeP2P(out id);

                    _recvHandshakeP2P.Update(reader.Read.Count);

                    return(id);
                }
                break;

            case MessageTypes.ServerRelayReliable:
            case MessageTypes.ServerRelayUnreliable:
            case MessageTypes.HandshakeRequest:
                Log.Error("Client received packet '{0}'. This should only ever be received by the server", header);
                break;

            default:
                Log.Debug("Ignoring a packet with an unknown header: '{0}'", header);
                break;
            }

            return(null);
        }