Example #1
0
 public NetworkEvent(ushort networkedObjectId, NetworkEventType networkEventType, NetworkSubeventType networkSubeventType, object data)
 {
     this.destNetId           = (NetworkIdentity)NetworkSerializer.GetNetworkedObjects()[networkedObjectId];
     this.networkEventType    = networkEventType;
     this.networkSubeventType = networkSubeventType;
     this.data = data;
 }
        // Check if message is valid. If so, distribute messages to their appropriate network identities. Else, get the rest if the message.
        private static void ReceiveCallback(IAsyncResult ar)
        {
            RecvState state = (RecvState)ar.AsyncState;

            int    bytesRead     = state.workSocket.EndReceive(ar);
            ushort messageLength = BitConverter.ToUInt16(state.recvBuffer, 0);

            bool hasTerminator = state.recvBuffer[messageLength] == 255 && state.recvBuffer[messageLength + 1] == 255 && state.recvBuffer[messageLength + 2] == 255;

            if (bytesRead > 2 && hasTerminator)
            {
                NetworkSerializer.DistributeMessages(state.recvBuffer);
                state.recvBuffer = new byte[RECV_BUFFER_SIZE];
            }

            else
            {
                state.workSocket.BeginReceive(state.recvBuffer, 0, RECV_BUFFER_SIZE, 0, new AsyncCallback(ReceiveCallback), state);
            }
        }