Example #1
0
        private void AsyncReceiveCallBack(IAsyncResult IA)
        {
            Socket socket = IA.AsyncState as Socket;

            if (socket != null && socket.Connected)
            {
                try
                {
                    int    datalength = (IA.AsyncState as Socket).EndReceive(IA);
                    string msg        = "";
                    if (datalength == 0)//1.断开连接
                    {
                        Disconnect();
                        OnServerClose?.Invoke(this, "服务器断开连接");
                    }
                    else
                    {
                        msg = Encoding.Default.GetString(_buffer, 0, datalength);
                        if (_heartBeatEnable && msg == "HEARTBEAT")//2.心跳数据解析
                        {
                            _alive = true;
                        }
                        else
                        {
                            ReceiveData?.Invoke(this, msg);//3.数据接收
                        }
                    }
                }
                catch
                { }
            }
        }
Example #2
0
        private void HeartBeat(int heartBeatMillSeconds)
        {
            if (_heartBeatEnable == false)
            {
                return;
            }
            _alive = true;

            if (ISConnect)
            {
                _heartThread = new Thread(() =>
                {
                    while (true)
                    {
                        Thread.Sleep(heartBeatMillSeconds);
                        if (!_alive)
                        {
                            Disconnect();
                            OnServerClose?.Invoke(this, "服务器心跳停止");
                        }
                        _alive = false;
                        SendMessage("HEARTBEAT");
                    }
                });
                _heartThread.Start();
            }
        }
Example #3
0
 public void Close()
 {
     if (socket != null)
     {
         socket.Close();
         socket.Dispose();
     }
     OnServerClose.Invoke(this, null);
     Console.WriteLine($"TCPServer: Stop Listening...");
 }
        private HandleResult ServerClose(IServer sender, IntPtr connId, SocketOperation socketOperation, int errorCode)
        {
            var extra = sender.GetExtra <TcpPortForwardingExtra>(connId);

            if (extra != null)
            {
                sender.RemoveExtra(connId);

                if (extra.ReleaseType == TcpPortForwardingReleaseType.None)
                {
                    extra.ReleaseType = TcpPortForwardingReleaseType.Server;
                    extra.Agent.Disconnect(extra.AgentConnId);
                }
            }

            return(OnServerClose?.Invoke(sender, connId, socketOperation, errorCode) ?? HandleResult.Ok);
        }
Example #5
0
 public void ServerClose()
 {
     OnServerClose?.Invoke();
 }