Esempio n. 1
0
        /// <summary>
        /// Sends an event to a client.
        /// </summary>
        /// <param name="e">Event to send.</param>
        /// <param name="connectionId">Target client's connection id</param>
        /// <param name="reliable">Is it reliable event</param>
        /// <param name="internalResend">If <c>false</c> this event will be added to reliable events queue as a new event</param>
        /// <returns><c>true</c> if the target client is connected</returns>
        public bool Send(EventBase e, int connectionId, bool reliable = false, bool internalResend = false)
        {
            ByteStreamWriter stream = new ByteStreamWriter();

            stream.WriteByte(reliable ? ( byte )MsgFlags.ReliableEvent : ( byte )MsgFlags.UnreliableEvent);
            stream.WriteByte(e.GetId());

            e.Serialize(stream);
            Connection c = m_server.GetConnectionById(connectionId);

            if (c != null)
            {
                c.Send(stream.GetBytes());
                if (reliable && !internalResend && e is ReliableEventBase)
                {
                    ReliableEventBase reb = e as ReliableEventBase;
                    m_reliablesToRepeat[reb.m_reliableEventId] = new REventIdPair(connectionId, reb);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 2
0
    /// <summary>
    /// Starts handshake process.
    /// </summary>
    /// <param name="receivePort">The port server should send data to.</param>
    public void HandshakeStepOne(int receivePort)
    {
        ByteStreamWriter writer = new ByteStreamWriter();

        writer.WriteByte((byte)MsgFlags.ConnectionRequest);
        writer.WriteByte((byte)HandshakeMessage.SYN);
        writer.WriteInteger(receivePort);
        m_sender.Send(writer.GetBytes());
        State = ConnectionState.Connecting;
    }
Esempio n. 3
0
    private void HandshakeStepTwo(BaseConnectionEntity con)
    {
        ByteStreamWriter writer = new ByteStreamWriter();

        writer.WriteByte((byte)MsgFlags.ConnectionRequest);
        writer.WriteByte((byte)HandshakeMessage.SYNACK);
        writer.WriteInteger(con.m_connectionId);
        con.m_sender.Send(writer.GetBytes());
        con.m_sender.Send(writer.GetBytes());
        con.m_sender.Send(writer.GetBytes());
        con.m_sender.Send(writer.GetBytes());
    }
Esempio n. 4
0
        /// <summary>
        /// Sends an event to a server.
        /// </summary>
        /// <param name="e">Event to send.</param>
        /// <param name="reliable">Is it reliable event</param>
        /// <param name="internalResend">If <c>false</c> this event will be added to reliable events queue as a new event</param>
        /// <returns><c>true</c> if the target client is connected</returns>
        public void Send(EventBase e, bool reliable = false, bool internalResend = false)
        {
            ByteStreamWriter stream = new ByteStreamWriter();

            stream.WriteByte(reliable ? (byte)MsgFlags.ReliableEvent : (byte)MsgFlags.UnreliableEvent);
            stream.WriteByte(e.GetId());
            e.Serialize(stream);
            m_client.Send(stream.GetBytes());
            if (reliable && !internalResend && e is ReliableEventBase)
            {
                ReliableEventBase reb = e as ReliableEventBase;
                m_reliablesToRepeat[reb.m_reliableEventId] = reb;
            }
        }
Esempio n. 5
0
        public void Shutdown()
        {
            ByteStreamWriter writer = new ByteStreamWriter();

            writer.WriteByte((byte)MsgFlags.ConnectionRequest);
            writer.WriteByte((byte)HandshakeMessage.Disconnect);
            writer.WriteInteger(ConnectionId);

            for (int i = 0; i < 5; ++i)
            {
                Send(writer);
            }

            m_client.Shutdown();
        }
Esempio n. 6
0
    public void Serialize(ByteStreamWriter writer)
    {
        writer.WriteInteger(id);

        int changeMask = 0;

        if (healthDirty)
        {
            changeMask = changeMask | maskOfHealthChange;
        }
        if (positionDirty)
        {
            changeMask = changeMask | maskOfPositionChange;
        }

        writer.WriteByte((byte)changeMask);

        if (healthDirty)
        {
            writer.WriteFloat(power);
        }
        if (positionDirty)
        {
            writer.WriteVector2(position);
        }
    }
Esempio n. 7
0
    /// <summary>
    /// Sends a confirmation of receiving the Connection ID to server (finalizes the connection on the Client side)
    /// </summary>
    /// <param name="connectionId">The connection identifier.</param>
    public void HandshakeStepThree(int connectionId)
    {
        ByteStreamWriter writer = new ByteStreamWriter();

        writer.WriteByte((byte)MsgFlags.ConnectionRequest);
        writer.WriteByte((byte)HandshakeMessage.ACK);
        writer.WriteInteger(connectionId);

        for (int i = 0; i < 5; ++i)
        {
            m_sender.Send(writer.GetBytes());
        }

        ConnectionId = connectionId;
        State        = ConnectionState.Connected;

        Network.Log(State + ", " + ConnectionId);
    }
Esempio n. 8
0
 public byte[] Encode()
 {
     using (ByteStreamWriter writer = new ByteStreamWriter())
     {
         writer.WriteShort(data.Length + 35);   //version(2)+roleId(8)+SessionID(4)+sequence(8)+gameVersion(2)+resVersion(4)+platform(1)+protocalCode(4) = 33
         writer.WriteShort(version);
         writer.WriteLong(playerId);
         writer.WriteInt(sessionId);
         writer.WriteLong(sequence);
         writer.WriteShort(gameVersion);
         writer.WriteInt(resVersion);
         writer.WriteByte(platform);
         writer.WriteInt(protocalCode);
         writer.WriteBytes(data);
         //Jiawen: optimise GetBuff function to save some new operations.
         return(writer.GetBuffer());
     }
 }