Esempio n. 1
0
    // ======================================
    // Server message reception
    // ======================================


    private static Message ReceiveServerMessage(BitBuffer bitBuffer)
    {
        var messageType = bitBuffer.GetEnum <MessageType>((int)MessageType.TOTAL);

        Message message;

        switch (messageType)
        {
        case MessageType.PLAYER_CONNECTED:
            message = PlayerConnectedMessage.CreatePlayerConnectedMessageToReceive();
            break;

        case MessageType.PLAYER_DISCONNECTED:
            message = PlayerDisconnectedMessage.CreatePlayerDisconnectedMessageToReceive();
            break;

        case MessageType.SNAPSHOT:
            message = new SnapshotMessage();
            break;

        case MessageType.ACK_RELIABLE_MAX_WAIT_TIME:
            message = AckReliableMessage.CreateAckReliableMessageMessageToReceive();
            break;

        case MessageType.ACK_RELIABLE_SEND_EVERY_PACKET:
            message = AckReliableSendEveryFrameMessage.CreateAckReliableSendEveryFrameMessageMessageToReceive();
            break;

        default:
            return(null);    // Return null if the message is not a "from server" type
        }
        message.Load(bitBuffer);
        return(message);
    }
Esempio n. 2
0
    private Message readServerMessage(BitBuffer bf)
    {
        MessageType messageType   = bf.GetEnum <MessageType> ((int)MessageType.TOTAL);
        Message     serverMessage = null;

        switch (messageType)
        {
        case MessageType.PLAYER_CONNECTED:
            serverMessage = PlayerConnectedMessage.CreatePlayerConnectedMessageToReceive();
            break;

        case MessageType.PLAYER_DISCONNECTED:
            serverMessage = PlayerDisconnectedMessage.CreatePlayerDisconnectedMessageToReceive();
            break;

        case MessageType.SNAPSHOT:
            serverMessage = new SnapshotMessage();
            break;

        case MessageType.ACK_RELIABLE_MAX_WAIT_TIME:
            serverMessage = AckReliableMessage.CreateAckReliableMessageMessageToReceive();
            break;

        case MessageType.ACK_RELIABLE_SEND_EVERY_PACKET:
            serverMessage = AckReliableSendEveryFrameMessage.CreateAckReliableSendEveryFrameMessageMessageToReceive();
            break;

        default:
            Debug.LogError("Got a Server message that cannot be understood");
            return(null);
        }
        serverMessage.Load(bf);
        return(serverMessage);
    }
Esempio n. 3
0
    Message ReadServerMessage(BitBuffer bitBuffer)
    {
        MessageType messageType   = bitBuffer.GetEnum <MessageType> ((int)MessageType.TOTAL);
        Message     serverMessage = null;

        switch (messageType)
        {
        case MessageType.PLAYER_CONNECTED:
            serverMessage = PlayerConnectedMessage.CreatePlayerConnectedMessageToReceive();
            break;

        case MessageType.PLAYER_DISCONNECTED:
            serverMessage = PlayerDisconnectedMessage.CreatePlayerDisconnectedMessageToReceive();
            break;

        case MessageType.SNAPSHOT:
            serverMessage = new SnapshotMessage();
            break;

        case MessageType.ACK_RELIABLE_MAX_WAIT_TIME:
            Debug.Log("Client::got ack reliable wait time ");
            serverMessage = AckReliableMessage.CreateAckReliableMessageMessageToReceive();
            break;

        case MessageType.ACK_RELIABLE_SEND_EVERY_PACKET:
            serverMessage = AckReliableSendEveryFrameMessage.CreateAckReliableSendEveryFrameMessageMessageToReceive();
            break;

        default:
            Debug.LogError("Got a server message that cannot be understood");
            return(null);
        }
        serverMessage.Load(bitBuffer);

        if (serverMessage.Reliability == ReliabilityType.RELIABLE_MAX_WAIT_TIME)
        {
            if (serverMessage.ReliabilityId == (LastReceivedReliableMessageId + 1))
            {
                //accept it... valid message since its +1 since the last received
                //send to the sender that its reliable message has been received
                outMessages.Add(AckReliableMessage.CreateAckReliableMessageMessageToSend(serverMessage.ReliabilityId));
                LastReceivedReliableMessageId = serverMessage.ReliabilityId;
            }
            else
            {
                //we need to discard it... either its been already processed or out of order
                return(null);
            }
        }
        else if (serverMessage.Reliability == ReliabilityType.RELIABLE_SEND_EVERY_PACKET)
        {
            if (serverMessage.ReliabilityId > LastReceivedSendInEveryFramePacketMessageId)
            {
                //set the last "reliable send in every packet" message id.. it will get sent to the server on the next packet
                LastReceivedSendInEveryFramePacketMessageId = serverMessage.ReliabilityId;
                Debug.Log("Client::lastReceivedSendInEveryFramePacketMessageId = " + lastReceivedSendInEveryFramePacketMessageId);
            }
            else
            {
                //discard
                return(null);
            }
        }

        return(serverMessage);
    }