コード例 #1
0
        /// <summary>
        /// 本地綁定伺服器
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <param name="error">綁定失敗時, 回傳錯誤訊息</param>
        /// <returns></returns>
        public bool Listen(IPAddress address, int port, out string error)
        {
            if (Interlocked.CompareExchange(ref _status, Status.Starting, Status.Stopped) != Status.Stopped)
            {
                error = "Server was run at ip " + CurrentIP + ":" + CurrentPort;
                return(false);
            }

            try
            {
                CurrentIP    = address;
                CurrentPort  = port;
                _tcpListener = new TcpListener(address, port);
                _tcpListener.Start();
                _tcpListener.Server.IOControl(IOControlCode.KeepAliveValues, KeepAlive(1, 1000, 1000), null);
                Thread thread = new Thread(() =>
                {
                    try
                    {
                        TcpClient tmpTcpClient;
                        while (_tcpListener.Server.IsBound)
                        {
                            //建立與客戶端的連線
                            tmpTcpClient = _tcpListener.AcceptTcpClient();
                            tmpTcpClient.Client.IOControl(IOControlCode.KeepAliveValues, KeepAlive(1, 0, 100), null);
                            if (tmpTcpClient.Client.IsSocketConnected())
                            {
                                //tmpTcpClient.NoDelay = true;
                                Task.Run(() =>
                                {
                                    CommunicatetManager manager = _factory.CreateManager(tmpTcpClient);
                                    _clients.TryAdd(manager, manager.ClientIP);
                                    OnClientAccept?.Invoke(this, manager);
                                    manager.Communicate(); // blocking thread
                                    _clients.TryRemove(manager, out string ip);
                                }).ConfigureAwait(false);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //
                    }
                    finally
                    {
                        _clients.Clear();
                        Interlocked.Exchange(ref _status, Status.Stopped);
                        _tcpListener.Stop();
                        _tcpListener = null;
                    }

                    if (AutoRestart)
                    {
                        while (!SpinWait.SpinUntil(() => Listen(CurrentIP, CurrentPort, out string _error), 500))
                        {
                            ;
                        }
                    }
                    else
                    {
                        CurrentPort    = -1;
                        CurrentIP      = null;
                        OnClientAccept = null;
                    }
                })
                {
                    IsBackground = true
                };
                thread.Start();
                error = null;
                return(thread.IsAlive);
            }
            catch (Exception ex)
            {
                Interlocked.Exchange(ref _status, Status.Stopped);
                if (_tcpListener.IsNonNull())
                {
                    _tcpListener.Stop();
                    _tcpListener = null;
                }
                error = ex.Message;
                return(false);
            }
        }
コード例 #2
0
 public EventCommunicatet(CommunicatetManager manager)
 {
     Manager = manager;
 }