Exemple #1
0
        void ListenThread()
        {
            ProsicsServer instance = this;

            try
            {
                while (true)
                {
                    Socket client = _serverSocket.Accept();
                    //client.IOControl(IOControlCode.KeepAliveValues,NetWorkHelper.GetKeppAlive(),null);
                    ConnectionThread ct = new ConnectionThread(client);
                    Logger.Log("#### new connection :" + client.RemoteEndPoint.ToString());
                    lock (_connections)
                    {
                        _connections.Add(ct);
                    }
                    ct.eventThreadWillTerminate = OnConnectionWillTerminate;
                    ct.eventReceiveData         = OnReceiveData;
                    ct.Start();
                }
            }
            catch (System.Exception e)
            {
                Logger.Log(e.ToString());
                _isRunning = false;
            }
            finally
            {
                Stop();
                NetWorkHelper.SafeClose(_serverSocket);
                Logger.Log("Server is Terminated.");
            }
        }
Exemple #2
0
 public void Close()
 {
     NetWorkHelper.SafeClose(_socket);
 }
Exemple #3
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.");
            }
        }