Пример #1
0
        private void ReceiveThread()
        {
            while (IsConnected)
            {
                try
                {
                    IPEndPoint ip   = mServerAdress;
                    byte[]     data = Receive(ref ip);

                    if (data.Length > 0)
                    {
                        lock (mKCP)
                        {
                            mKCP.Input(data);

                            for (int size = mKCP.PeekSize(); size > 0; size = mKCP.PeekSize())
                            {
                                MessageBuffer message = new MessageBuffer(size);

                                if (mKCP.Recv(message.DataBuffer) > 0)
                                {
                                    if (message.IsValid())
                                    {
                                        OnMessage?.Invoke(message);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Close();
                    throw e;
                }

                Thread.Sleep(1);
            }
        }
Пример #2
0
        private void OnReceiveKcp(byte[] data)
        {
            if (mKCP == null)
            {
                return;
            }
            lock (mKCP)
            {
                mKCP.Input(data);

                for (int size = mKCP.PeekSize(); size > 0; size = mKCP.PeekSize())
                {
                    MessageBuffer message = new MessageBuffer(size);
                    if (mKCP.Recv(message.buffer) > 0)
                    {
                        if (onMessage != null && message.IsValid())
                        {
                            onMessage(message);
                        }
                    }
                }
            }
        }
Пример #3
0
        public static void Dispatch(MessageBuffer message)
        {
            if (message == null)
            {
                return;
            }
            int id = message.id();

            Type type = GetType((MessageID)id);

            if (type == null)
            {
                return;
            }

            object data = ProtoTransfer.DeserializeProtoBuf(message.body(), type);

            if (data == null)
            {
                return;
            }

            mDispatcher.Dispatch(id, data, type);
        }
Пример #4
0
        void ReceiveThread()
        {
            while (IsActive)
            {
                var sessions = mService.sessions;
                for (int i = 0; i < sessions.Count; ++i)
                {
                    Session c = sessions[i];
                    if (c == null)
                    {
                        continue;
                    }
                    try
                    {
                        if (c.IsConnected == false)
                        {
                            continue;
                        }

                        int receiveSize = c.socket.Receive(MessageBuffer.head, MessageBuffer.MESSAGE_HEAD_SIZE, SocketFlags.None);
                        if (receiveSize == 0)
                        {
                            continue;
                        }

                        if (receiveSize != MessageBuffer.MESSAGE_HEAD_SIZE)
                        {
                            continue;
                        }

                        if (MessageBuffer.IsValid(MessageBuffer.head) == false)
                        {
                            continue;
                        }
                        int bodySize = 0;
                        if (MessageBuffer.Decode(MessageBuffer.head, MessageBuffer.MESSAGE_BODY_SIZE_OFFSET, ref bodySize) == false)
                        {
                            continue;
                        }
                        MessageBuffer message = new MessageBuffer(MessageBuffer.MESSAGE_HEAD_SIZE + bodySize);

                        Array.Copy(MessageBuffer.head, 0, message.buffer, 0, MessageBuffer.head.Length);

                        if (bodySize > 0)
                        {
                            int receiveBodySize = c.socket.Receive(message.buffer, MessageBuffer.MESSAGE_BODY_OFFSET, bodySize, SocketFlags.None);

                            if (receiveBodySize != bodySize)
                            {
                                continue;
                            }
                        }

                        if (onReceive != null)
                        {
                            onReceive(new MessageInfo(message, c));
                        }
                    }
                    catch (SocketException e)
                    {
                        mService.Debug(e.Message);
                        c.Disconnect();
                    }
                    catch (Exception e)
                    {
                        mService.CatchException(e);
                        throw e;
                    }
                }

                Thread.Sleep(1);
            }
        }
Пример #5
0
 public static T DeserializeProtoBuf <T>(MessageBuffer buffer) where T : class, ProtoBuf.IExtensible
 {
     return(DeserializeProtoBuf <T>(buffer.body()));
 }
Пример #6
0
 public static T DeserializeProtoBuf3 <T>(MessageBuffer buffer) where T : Google.Protobuf.IMessage, new()
 {
     return(DeserializeProtoBuf3 <T>(buffer.Body()));
 }
Пример #7
0
        private void ReceiveThread()
        {
            while (IsConnected)
            {
                try
                {
                    int receiveSize = socket.Receive(MessageBuffer.head, MessageBuffer.MESSAGE_HEAD_SIZE, SocketFlags.None);
                    //收到的字节数不是预定的消息头的长度的话,或者消息头的结构定义不正确的话,那么这条消息是不正确的
                    if (receiveSize == 0 || receiveSize != MessageBuffer.MESSAGE_HEAD_SIZE || MessageBuffer.IsValid(MessageBuffer.head) == false)
                    {
                        continue;
                    }

                    //获取要获取的消息长度 ,bodySize返回
                    int bodySize = 0;
                    if (MessageBuffer.Decode(MessageBuffer.head, MessageBuffer.MESSAGE_BODY_SIZE_OFFSET, ref bodySize) == false)
                    {
                        continue;
                    }
                    MessageBuffer message = new MessageBuffer(MessageBuffer.MESSAGE_HEAD_SIZE + bodySize);

                    //将接收的包头拷贝到message里
                    Array.Copy(MessageBuffer.head, 0, message.buffer, 0, MessageBuffer.head.Length);

                    //接收包头
                    if (bodySize > 0)
                    {
                        int receiveBodySize = socket.Receive(message.buffer, MessageBuffer.MESSAGE_BODY_OFFSET, bodySize, SocketFlags.None);
                        if (receiveBodySize != bodySize)
                        {
                            continue;
                        }
                    }

                    if (onReceive != null)
                    {
                        onReceive(new MessageInfo(message, this));
                    }
                }
                catch (SocketException e)
                {
                    mService.Debug(e.Message);
                    Disconnect();
                }
                catch (Exception e)
                {
                    mService.CatchException(e);
                    throw e;
                }

                Thread.Sleep(1);
            }
        }
Пример #8
0
 public MessageInfo(MessageBuffer message, Session c)
 {
     mSession       = c;
     mMessageBuffer = message;
 }