private void InitSocket(Socket s) { try { _socket = s; _socket.Blocking = false; _nodelay = _socket.NoDelay; _receiveEvent = new SocketAsyncEventArgs(); _receiveEvent.SetBuffer(_peekbuff, 0, 1); _receiveEvent.SocketFlags = SocketFlags.Peek; _receiveEvent.Completed += (sender, e) => _loop.Post(ReceiveAsyncComplete); _sendEvent = new SocketAsyncEventArgs(); _sendEvent.Completed += (sender, e) => _loop.Post(SendAsyncComplete); _sendAsyncInprogress = false; _receiveAsyncInprogress = false; _receiveOffset = 0; _receiveEnd = 0; _receiveStarted = false; _remoteEndpoint = _socket.RemoteEndPoint; } catch (Exception err) { DebugOutput.Error($"Socket init failed: err={err}"); Disconnect(); } }
public void Listen(EndPoint bindAddress) { if (_socket != null) { throw new Exception("Already listening"); } _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.Blocking = false; _socket.Bind(bindAddress); _socket.Listen(_backlog); _acceptEventArgs = new SocketAsyncEventArgs(); _acceptEventArgs.Completed += (sender, e) => _loop.Post(AcceptComplete, e); // Starts accept. if (!_socket.AcceptAsync(_acceptEventArgs)) { AcceptComplete(_acceptEventArgs); } }
protected override void QueueTask(Task task) { eventLoop.Post(PostCallback, task); }