Пример #1
0
        public void Thread_Com()
        {
            long lastTicks        = DateTime.Now.Ticks;
            long lasstServerTicks = DateTime.Now.Ticks;

            try
            {
                while (true)
                {
                    try
                    {
                        if (!_socket.Connected)
                        {
                            Logger.Log("connection is closed");
                            break;
                        }
                        if (DateTime.Now.Ticks - lasstServerTicks >= _heartbeatOutTime * 10000000)
                        {
                            Logger.Log("heartbeat outtime.");
                            break;
                        }

                        if (DateTime.Now.Ticks - lastTicks >= _heartbeatInterval * 10000000)
                        {
                            lastTicks = DateTime.Now.Ticks;
                            SendPackage(NetBitPacker.Packet(null, 255));
                        }


                        int count = _socket.Receive(_tmpBuff);


                        if (count > 0)
                        {
                            NetWorkHelper.PushToBuffer(_tmpBuff, count, _mainBuff);
                            int         msgType;
                            List <byte> data = NetBitPacker.GetPacket(_mainBuff, out msgType);
                            if (data != null)
                            {
                                //心跳包
                                if (msgType == 255)
                                {
                                    Logger.Log("heart beat");
                                    lasstServerTicks = DateTime.Now.Ticks;
                                }
                                else if (data != null)
                                {
                                    if (eventReceived != null)
                                    {
                                        eventReceived(data);
                                    }
                                }
                            }
                        }
                        else if (count == 0)
                        {
                            throw new Exception(" Server is closed.");
                        }
                    }
                    catch (SocketException e)
                    {
                        if (e.SocketErrorCode == SocketError.WouldBlock)
                        {
                            continue;
                        }
                        else
                        {
                            Logger.Log(e.SocketErrorCode.ToString());
                            Logger.LogException(e);
                            break;
                        }
                    }
                    catch (ThreadAbortException e)
                    {
                        throw e;
                    }
                    catch (Exception e)
                    {
                        break;
                    }
                }
            }
            finally
            {
                Logger.Log("Connection lost");
                OnConnectionLost();
            }
        }
Пример #2
0
        void IOThread()
        {
            ConnectionThread instance  = this;
            long             lastTicks = DateTime.Now.Ticks;

            try
            {
                while (true)
                {
                    try
                    {
                        //心跳检测

                        if (DateTime.Now.Ticks - lastTicks >= _heartbeatInterval * 10000000)
                        {
                            throw new Exception(" Heartbeat out time.");
                        }


                        int count = _socket.Receive(_tmpBuff);
                        if (count > 0)
                        {
                            Logger.Log("receive :" + count);
                            NetWorkHelper.PushToBuffer(_tmpBuff, count, _mainBuff);
                            int         msgType;
                            List <byte> data = NetBitPacker.GetPacket(_mainBuff, out msgType);
                            if (data != null)
                            {
                                //心跳包
                                if (msgType == 255)
                                {
                                    Logger.Log("heart beat");
                                    lastTicks = DateTime.Now.Ticks;
                                    //发送心跳包
                                    _socket.Send(NetBitPacker.Packet(null, 255));
                                }
                                else
                                {
                                    if (eventReceiveData != null)
                                    {
                                        eventReceiveData(data);
                                    }
                                }
                            }
                        }
                        else if (count == 0)
                        {
                            throw new Exception(" client has disconnect!");
                        }
                    }
                    catch (SocketException e)
                    {
                        if (e.SocketErrorCode == SocketError.WouldBlock)
                        {
                            continue;
                        }
                        else if (e.SocketErrorCode == SocketError.TimedOut)
                        {
                            continue;
                        }
                        else
                        {
                            Logger.Log(e.SocketErrorCode.ToString());
                            Logger.Log(e);
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Log(e);
                        break;
                    }
                }
            }
            finally
            {
                NetWorkHelper.SafeClose(_socket);
                if (eventThreadWillTerminate != null)
                {
                    eventThreadWillTerminate(this);
                }
                Logger.Log("<ConnectionThread> has Terminated.");
            }
        }