Ejemplo n.º 1
0
        public void Stop()
        {
            if (_isRunning)
            {
                Logger.Log("Server will Terminated.");
                _listenThread.Abort();
            }
            lock (_connections)
            {
                if (_connections.Count > 0)
                {
                    foreach (ConnectionThread t in _connections)
                    {
                        t.eventThreadWillTerminate = null;
                        t.eventReceiveData         = null;
                    }

                    while (_connections.Count > 0)
                    {
                        ConnectionThread ct = _connections [0];
                        _connections.RemoveAt(0);
                        ct.Close();
                    }
                    Logger.Log("All ConnectionThread has been Close");
                }
            }
        }
Ejemplo n.º 2
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.");
            }
        }
Ejemplo n.º 3
0
 void OnConnectionWillTerminate(ConnectionThread connection)
 {
     if (this == null)
     {
         return;
     }
     lock (_connections)
     {
         if (_connections != null && _connections.Contains(connection))
         {
             _connections.Remove(connection);
         }
         Logger.Log("a ConnectThread has removed.");
     }
 }
Ejemplo n.º 4
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.");
            }
        }