Exemplo n.º 1
0
        public static ServerTcpMessage[] Unpack(/*ServerType serverType,*/ byte[] responseData)
        {
            DebugUtils.Assert(responseData != null && responseData.Length > 0, "Response data is null!");

            using (ByteStreamReader reader = new ByteStreamReader(responseData))
            {
                long serverId = reader.ReadLong();
                ClientTcpMessage.sessionId = reader.ReadInt(); //if sessionId is 4 bytes, that's OK.
                int msgNum = reader.ReadByte();
                ServerTcpMessage[] responseMessages = new ServerTcpMessage[msgNum];
                for (int i = 0; i < msgNum; i++)
                {
                    int  len  = reader.ReadShort();
                    int  code = reader.ReadInt();
                    long seq  = reader.ReadLong();

                    if (Enum.IsDefined(typeof(MsgCode), code))
                    {
                        responseMessages[i] = new ServerTcpMessage((MsgCode)code, seq, reader.ReadBytes(len));
                        DebugUtils.Log(DebugUtils.Type.Protocol, "Receive network message, protocol code " + code);
                    }
                    else
                    {
                        DebugUtils.LogError(DebugUtils.Type.Protocol, "For now, the client can't recognize the received protocol code " + code);
                    }
                }

                return(responseMessages);
            }
        }
Exemplo n.º 2
0
    /// <summary>
    /// Called when stream is identified as a ConnectionRequest object.
    /// </summary>
    /// <param name="stream">Data stream.</param>
    /// <param name="flags">Stream flags.</param>
    /// <param name="endpoint">Data source.</param>
    /// <returns><c>true</c> if stream was handled correctly, <c>false</c> otherwise.</returns>
    bool HandleConnectionMsg(ByteStreamReader stream, byte flags, IPEndPoint endpoint)
    {
        if (HasFlag(flags, ( byte )MsgFlags.ConnectionRequest))
        {
            byte content = stream.ReadByte();
            m_connectionMessagesReceiver.HandleConnectionData(( HandshakeMessage )content, stream, endpoint);
            return(true);
        }

        return(false);
    }
Exemplo n.º 3
0
    /// <summary>
    /// Called when byte array is identified as an event. Deserializes and dispatches it.
    /// </summary>
    /// <param name="stream">Data stream.</param>
    /// <param name="flags">Stream flags (reliable or unreliable event?).</param>
    /// <returns><c>true</c> if stream was handled correctly, <c>false</c> otherwise.</returns>
    bool HandleEvent(ByteStreamReader stream, byte flags)
    {
        if (HasFlag(flags, ( byte )MsgFlags.UnreliableEvent))
        {
            byte      eventType = stream.ReadByte();
            EventBase evnt      = m_events.CreateEvent(eventType);
            //Network.Log("Event type: " + evnt.GetEventType());
            evnt.Deserialize(stream);
            m_dispatcher.PushEvent(evnt);
            return(true);
        }
        else if (HasFlag(flags, ( byte )MsgFlags.ReliableEvent))
        {
            byte      eventType = stream.ReadByte();
            EventBase evnt      = m_events.CreateEvent(eventType);

            evnt.Deserialize(stream);
            m_dispatcher.PushEvent(evnt);

            return(true);
        }

        return(false);
    }
Exemplo n.º 4
0
    /// <summary>
    /// Takes care of data from a byte array, calling all the necessary deserializations and dispatches.
    /// </summary>
    /// <param name="data">The data.</param>
    /// <param name="endpoint">The data source.</param>
    /// <returns><c>true</c> if data was successfully handles, <c>false</c> otherwise.</returns>
    public bool HandleData(byte[] data, IPEndPoint endpoint = null)
    {
        System.Diagnostics.Debug.Assert(data.Length < c_maxPacketSize);
        m_stream = new ByteStreamReader(data);

        byte flags = m_stream.ReadByte();

        if (HandleEvent(m_stream, flags))
        {
            return(true);
        }
        if (HandleConnectionMsg(m_stream, flags, endpoint))
        {
            return(true);
        }

        return(false);
    }
Exemplo n.º 5
0
    public bool Deserialize(ByteStreamReader reader)
    {
        id = reader.ReadInt();

        int changeMask = reader.ReadByte();

        bool changedPosition = (changeMask & maskOfPositionChange) != 0,
             changedHealth   = (changeMask & maskOfHealthChange) != 0;

        if (changedHealth)
        {
            power = reader.ReadFloat();
            Console.WriteLine("Applying health: " + power);
        }
        if (changedPosition)
        {
            position = reader.ReadVector2();
            Console.WriteLine("Applying position: " + position);
        }

        return(true);
    }
 protected override void Parse(ref ByteStreamReader bsr)
 {
     Conditions = new List <string>();
     CustomInterruptConditions = new List <string>();
     ConditionsPreIgnore       = new List <string>();
     IgnoreConditions          = new List <string>();
     foreach (List <string> condList in new[] { Conditions, CustomInterruptConditions, ConditionsPreIgnore, IgnoreConditions })
     {
         for (;;)
         {
             if (bsr.ReadByte() != 0)
             {
                 bsr.CurrentByteIndex--;
                 condList.Add(bsr.ReadNullTerminatedString());
             }
             else
             {
                 break;
             }
         }
     }
 }