Beispiel #1
0
        void AcceptThread()
        {
            while (IsActive)
            {
                try
                {
                    Socket s = AcceptSocket();
                    //Socket s = AcceptTcpClient();
                    if (s != null)
                    {
                        if (onAccept != null)
                        {
                            onAccept(s);
                        }
                    }

                    Thread.Sleep(1);
                }
                catch (Exception e)
                {
                    mService.CatchException(e);
                    throw e;
                }
            }
        }
Beispiel #2
0
        void ReceiveThread()
        {
            while (IsActive)
            {
                try
                {
                    IPEndPoint ip   = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     data = Receive(ref ip);

                    if (data.Length > 0)
                    {
                        Session c = mService.GetSession(ip);

                        if (MessageBuffer.IsValid(data))
                        {
                            var buffer = new MessageBuffer(data);
                            if (c == null || c.id != buffer.extra())
                            {
                                c = mService.GetSession(buffer.extra());
                            }

                            if (c != null)
                            {
                                if (c.udpAdress == null || c.udpAdress.Equals(ip) == false)
                                {
                                    c.udpAdress = ip;
                                }

                                if (onReceive != null)
                                {
                                    onReceive(new MessageInfo(buffer, c));
                                }
                            }
                        }
                    }

                    Thread.Sleep(1);
                }
                catch (SocketException e)
                {
                    mService.Debug(e.Message);
                    continue;
                }
                catch (Exception e)
                {
                    mService.CatchException(e);
                    throw e;
                }
            }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        void ReceiveThread()
        {
            while (IsActive)
            {
                try
                {
                    IPEndPoint ip   = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     data = Receive(ref ip);

                    Session c = mService.GetSession(ip);

                    if (c == null)
                    {
                        var sessions = mService.sessions;

                        for (int i = 0; i < sessions.Count; ++i)
                        {
                            sessions[i].OnReceiveKcp(data, ip);
                        }
                    }
                    else
                    {
                        c.OnReceiveKcp(data, ip);
                    }

                    Thread.Sleep(1);
                }
                catch (SocketException e)
                {
                    //mService.Debug(e.Message);
                    continue;
                }
                catch (Exception e)
                {
                    mService.CatchException(e);
                    throw e;
                }
            }
        }
        private void ReceiveThread()
        {
            while (IsConnected)
            {
                Session c = this;
                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);
            }
        }