Пример #1
0
            public void Reception()
            {
                int  recHostID;
                int  recConnectionID;
                int  recChannelID;
                int  recSize;
                byte error;

                byte[] buffer = new byte[STANDARD_BUFFER_SIZE];

                NetworkEventType e;

                e = NetworkTransport.Receive(out recHostID, out recConnectionID, out recChannelID, buffer, STANDARD_BUFFER_SIZE, out recSize, out error);
                if (error != 0)
                {
                    if ((NetworkError)error == NetworkError.MessageToLong)
                    {
                        // Re-recevoir le message avec un buffer plus grand.
                        buffer = new byte[FRAGMENTED_BUFFER_SIZE];
                        e      = NetworkTransport.Receive(out recHostID, out recConnectionID, out recChannelID, buffer, FRAGMENTED_BUFFER_SIZE, out recSize, out error);
                    }
                    else
                    {
                        Debugger.Log("ERREUR - NETWORKRECEIVER - RECEPTION() - " + (NetworkError)error);
                    }
                }

                switch (e)
                {
                case (NetworkEventType.ConnectEvent):
                    Debugger.Log("Connexion établie. ID = " + recConnectionID);
                    NetworkSocket.OnConnectionEstablished(recConnectionID);
                    break;

                case (NetworkEventType.DisconnectEvent):
                    Debugger.Log("Connexion fermée. ID = " + recConnectionID);
                    NetworkSocket.OnDisconnection(recConnectionID);
                    break;

                case (NetworkEventType.DataEvent):

                    // Des données ont été reçues. D'abord, construire l'objet ReceivedMessage en reconstituant le NetworkMessage :
                    MemoryStream    stream = new MemoryStream(buffer);
                    object          info   = Formatter.Deserialize(stream);
                    ReceivedMessage msg    = new ReceivedMessage((NetworkMessage)info);
                    // Ensuite, remplir l'objet NetworkMessageInfo de msg :
                    msg.RecInfo.ConnectionID = recConnectionID;

                    //_____________________________________________________
                    // Enfin, envoyer le ReceivedMessage au MessageReader.

                    MessageReader.AddMessageToQueue(msg);
                    break;
                }
            }