Ejemplo n.º 1
0
 public void RequestSyncData(ServiceType type, ulong characterId, uint id, Action <float> setter)
 {
     RequestSyncDataImpl(type, characterId, id, ms =>
     {
         ms.Read(mBuffer, 0, 4);
         setter(SerializerUtility.ReadInt(this.mBuffer) / 10000.0f);
     });
 }
Ejemplo n.º 2
0
 public void RequestSyncData(ServiceType type, ulong characterId, uint id, Action <long> setter)
 {
     RequestSyncDataImpl(type, characterId, id, ms =>
     {
         ms.Read(mBuffer, 0, 8);
         setter(SerializerUtility.ReadLong(this.mBuffer));
     });
 }
Ejemplo n.º 3
0
        void CopyDataToSendBuffer(IEnumerable <ServiceDesc> messages)
        {
            var ms = mMemoryStream;

            ms.Seek(0, SeekOrigin.Begin);
            ms.SetLength(0);
            {
                // foreach(var message in messages)
                var __enumerator1 = (messages).GetEnumerator();
                while (__enumerator1.MoveNext())
                {
                    var message = __enumerator1.Current;
                    {
                        var l = ms.Length;
                        ms.Seek(l + 6, SeekOrigin.Begin);
                        SerializerUtility.Serialize(ms, message);
                        var length = ms.Length - l - 6;
                        if (length > 128)
                        {
                            var b = LZ4Codec.Encode32(ms.GetBuffer(), (int)(l + 6), (int)length, mEncodeBuffer, 0,
                                                      mEncodeBuffer.Length);
                            ms.SetLength(l + 6);
                            ms.Seek(l + 6, SeekOrigin.Begin);
                            ms.Write(mEncodeBuffer, 0, b);
                            prefixHandler.WriteInt32(sendPrefixBuffer, 0, (int)b + (1 << 30));
                        }
                        else
                        {
                            prefixHandler.WriteInt32(sendPrefixBuffer, 0, (int)length);
                        }
                        ms.Seek(l, SeekOrigin.Begin);
                        r.NextBytes(head);
                        head[0] = (byte)((head[0] & 0xFE) | 0x08);
                        ms.WriteByte(head[0]);
                        ms.WriteByte((byte)(0xFF ^ head[0]));
                        ms.Write(sendPrefixBuffer, 0, 4);
                        ms.Seek(ms.Length, SeekOrigin.Begin);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// If buffer still has data after call this function, it will return true.
        /// </summary>
        /// <param name="receiveSendEventArgs"></param>
        /// <param name="receiveSendToken"></param>
        /// <param name="remainingBytesToProcess"></param>
        /// <returns></returns>
        private bool ReceiveOneMessage(byte[] buffer, DataHoldingUserToken receiveSendToken,
                                       ref int remainingBytesToProcess)
        {
            // If we have not got all of the prefix then we need to work on it.
            // receivedPrefixBytesDoneCount tells us how many prefix bytes were
            // processed during previous receive ops which contained data for
            // this message. (In normal use, usually there will NOT have been any
            // previous receive ops here. So receivedPrefixBytesDoneCount would be 0.)
            if (receiveSendToken.state == DataHoldingUserToken.State.Prefix)
            {
                remainingBytesToProcess = prefixHandler.HandlePrefix(buffer, receiveSendToken,
                                                                     remainingBytesToProcess);

                if (remainingBytesToProcess == 0)
                {
                    //Jump out of the method, since there is no more data.
                    return(false);
                }
            }

            if (receiveSendToken.state == DataHoldingUserToken.State.Error)
            {
                StartDisconnect();
            }

            // If we have processed the prefix, we can work on the message now.
            // We'll arrive here when we have received enough bytes to read
            // the first byte after the prefix.
            bool incomingTcpMessageIsReady = messageHandler.HandleMessage(buffer, receiveSendToken,
                                                                          ref remainingBytesToProcess);

            if (incomingTcpMessageIsReady)
            {
                // Process the data received, this function should use the data in dataReceived, and Deserialize the data
                // then the dataReceived will be reused  to receive other data.
                //OnMessageRecieved(receiveSendToken.SessionId, receiveSendToken.dataReceived);
                byte[] b;
                int    length = 0;
                if (receiveSendToken.currentMessageCompressed)
                {
                    try
                    {
                        length = LZ4Codec.Decode32(receiveSendToken.dataReceived.GetBuffer(), 0, (int)receiveSendToken.dataReceived.Length, mDecodeBuffer, 0, mDecodeBuffer.Length, false);
                        b      = mDecodeBuffer;
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Message can not unwrap, this may caused by a hacker's attack." + ex.ToString());
                        b = null;
                    }
                }
                else
                {
                    b      = receiveSendToken.dataReceived.GetBuffer();
                    length = (int)receiveSendToken.dataReceived.Length;
                }

                if (b != null)
                {
                    using (var ms = new MemoryStream(b, 0, length))
                    {
                        var message = SerializerUtility.Deserialize(ms);
                        receiveSendToken.Reset();

                        if (message.Type == (int)MessageType.Ping)
                        {
                            latency = DateTime.Now - DateTime.FromBinary((long)message.ClientId);
                        }
                        else
                        {
                            if (MessageReceived != null)
                            {
                                MessageReceived(message);
                            }
                        }
                    }
                }

                // continue to process another message which is not completed.
                if (remainingBytesToProcess > 0)
                {
                    return(true);
                }

                return(false);
            }
            else
            {
                // Since we have NOT gotten enough bytes for the whole message,
                // we need to do another receive op. Reset some variables first.

                // All of the data that we receive in the next receive op will be
                // message. None of it will be prefix. So, we need to move the
                // receiveSendToken.receiveMessageOffset to the beginning of the
                // buffer space for this SAEA.
                receiveSendToken.receiveMessageOffset = receiveSendToken.bufferOffsetReceive;
                return(false);
            }
        }
Ejemplo n.º 5
0
        public static ServiceDesc Deserialize(Stream s)
        {
            ServiceDesc desc = new ServiceDesc();

            byte[] buffer = new byte[9];
            s.Read(buffer, 0, 1);
            byte mask = buffer[0];

            s.Read(buffer, 0, 1);
            desc._Type = buffer[0];
            if ((mask & 1) != 0)
            {
                s.Read(buffer, 0, 1);
                desc._ServiceType = buffer[0];
            }
            if ((mask & 2) != 0)
            {
                s.Read(buffer, 0, 4);
                desc._FuncId = (uint)SerializerUtility.ReadInt(buffer);
            }
            if ((mask & 4) != 0)
            {
                s.Read(buffer, 0, 4);
                desc._PacketId = (uint)SerializerUtility.ReadInt(buffer);
            }
            if ((mask & 8) != 0)
            {
                s.Read(buffer, 0, 2);
                desc._Error = (uint)SerializerUtility.ReadShort(buffer);
            }
            if ((mask & 16) != 0)
            {
                desc._ClientId = (ulong)SerializerUtility.ReadLongOpt(s, buffer);
            }
            if ((mask & 32) != 0)
            {
                desc._CharacterId = (ulong)SerializerUtility.ReadLongOpt(s, buffer);
            }
            if ((mask & 64) != 0)
            {
                s.Read(buffer, 0, 2);
                var   length = SerializerUtility.ReadShort(buffer);
                ulong l;
                if (desc._Routing == null)
                {
                    desc._Routing = new List <ulong>();
                }
                for (int i = 0; i < length; i++)
                {
                    l = (ulong)SerializerUtility.ReadLongOpt(s, buffer);
                    desc._Routing.Add(l);
                }
            }
            if ((mask & 128) != 0)
            {
                s.Read(buffer, 0, 4);
                var length = SerializerUtility.ReadInt(buffer);
                desc.Data = new byte[length];
                s.Read(desc.Data, 0, length);
            }
            else
            {
                desc._Data = sEmptyByteArray;
            }

            return(desc);
        }
Ejemplo n.º 6
0
        public static void Serialize(Stream s, ServiceDesc desc)
        {
            if (desc == null)
            {
                return;
            }

            if (!s.CanWrite || !s.CanSeek)
            {
                return;
            }

            byte[] buffer = new byte[9];
            var    pos    = s.Position;

            s.WriteByte(0);
            byte mask = 0;

            buffer[0] = (byte)desc._Type;
            s.Write(buffer, 0, 1);
            if (desc._ServiceType != default(int))
            {
                mask     |= 1;
                buffer[0] = (byte)desc._ServiceType;
                s.Write(buffer, 0, 1);
            }
            if (desc._FuncId != default(int))
            {
                mask |= 2;
                SerializerUtility.WriteInt(buffer, (int)desc._FuncId);
                s.Write(buffer, 0, 4);
            }
            if (desc._PacketId != default(int))
            {
                mask |= 4;
                SerializerUtility.WriteInt(buffer, (int)desc._PacketId);
                s.Write(buffer, 0, 4);
            }
            if (desc._Error != default(int))
            {
                mask |= 8;
                SerializerUtility.WriteShort(buffer, (short)desc._Error);
                s.Write(buffer, 0, 2);
            }
            if (desc._ClientId != default(ulong))
            {
                mask |= 16;
                SerializerUtility.WriteLongOpt(s, buffer, (long)desc._ClientId);
            }
            if (desc._CharacterId != default(ulong))
            {
                mask |= 32;
                SerializerUtility.WriteLongOpt(s, buffer, (long)desc._CharacterId);
            }
            if (desc._Routing != null && desc._Routing.Count != 0)
            {
                mask |= 64;
                SerializerUtility.WriteShort(buffer, (short)desc._Routing.Count);
                s.Write(buffer, 0, 2);
                {
                    var __list1      = desc._Routing;
                    var __listCount1 = __list1.Count;
                    for (int __i1 = 0; __i1 < __listCount1; ++__i1)
                    {
                        var r = __list1[__i1];
                        {
                            SerializerUtility.WriteLongOpt(s, buffer, (long)r);
                        }
                    }
                }
            }
            if (desc.Data != null && desc.Data.Length != 0)
            {
                mask |= 128;
                SerializerUtility.WriteInt(buffer, desc.Data.Length);
                s.Write(buffer, 0, 4);
                s.Write(desc.Data, 0, desc.Data.Length);
            }

            buffer[0] = mask;
            var last = s.Position;

            s.Seek(pos, SeekOrigin.Begin);
            s.Write(buffer, 0, 1);
            s.Seek(last, SeekOrigin.Begin);
        }