private void OnConnect(IAsyncResult ar) { Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); try { OnClientAccept?.Invoke(this, new TcpSocketClient(handler)); } catch (Exception ex) { handler.Close(); } globalSocket.BeginAccept(OnConnect, globalSocket); }
public async Task StartAsync(CancellationToken?token = null) { _tokenSource = CancellationTokenSource.CreateLinkedTokenSource(token ?? new CancellationToken()); _token = _tokenSource.Token; _listener.Start(); Listening = true; try { await Task.Run(async() => { while (!_token.IsCancellationRequested) { try { var result = await _listener.AcceptTcpClientAsync(); ListenToClientAsync(result, _token); OnClientAccept?.Invoke(this, new ClientAcceptEventArgs(Name, result)); } catch (Exception e) { _Logger.Log(Name, e.ToLog("Failed to accept client!"), LogLevels.Fatal); } } }, _token); } catch (Exception e) { _Logger.Log(Name, e.ToLog("Unexpected exit from listenner!"), LogLevels.Fatal); } finally { _listener.Stop(); Listening = false; } }
public int Accept(OnClientAccept onAccept, OnZitiClientData onClientData) { this.onAccept = onAccept; this.onClientData = onClientData; return(Native.API.ziti_accept(nativeConnection, native_on_accept, native_on_client_data)); }
/// <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); } }