public override void ProcessSend(SocketAsyncEventArgs e) { if (e.LastOperation != SocketAsyncOperation.Send) { return; } if (e.SocketError == SocketError.Success) { // done echoing data back to the client AsyncUserToken token = (AsyncUserToken)e.UserToken; // read the next block of data send from the client if (!token.Socket.ReceiveAsync(e)) { ProcessReceived(e); } } else { Close(); } }
public override void ProcessReceived(SocketAsyncEventArgs e) { // check if the remote host closed the connection AsyncUserToken token = (AsyncUserToken)e.UserToken; if (e.BytesTransferred <= 0 || e.SocketError != SocketError.Success) { Close(); return; } this.LastActiveTime = DateTime.Now; Console.Write("."); ResolveResult result = _commandResolver.Parse(e, Encoding.UTF8.GetBytes("|")[0]); if (result.FoundCompleteRequest) { _commandResolver = new DefaultAsyncCommandResolver(_commandResolver); string commandLine = result.RequestType == RequestType.PolicyFileRequest ? result.Requests[0] : string.Join <string>("|", result.Requests); if (string.IsNullOrEmpty(commandLine)) { if (!token.Socket.ReceiveAsync(e)) { ProcessReceived(e); } return; } try { byte[] response = Encoding.UTF8.GetBytes(commandLine); //Take Care !!! Buffer.BlockCopy(response, 0, e.Buffer, e.Offset, response.Length); e.SetBuffer(e.Offset, response.Length); //CPU high here bool willRaiseEvent = token.Socket.SendAsync(e); if (!willRaiseEvent) { ProcessSend(e); } if (result.RequestType != RequestType.PolicyFileRequest) { OnRequestReceived.OnEvent <SocketPlainTextEventArgs>(this, new SocketPlainTextEventArgs() { Content = commandLine }); } } catch (Exception ex) { if (log.IsErrorEnabled) { log.Error("Session ProcessReceive Exception:", ex); } } } else { _commandResolver = new DefaultAsyncCommandResolver(_commandResolver); if (!token.Socket.ReceiveAsync(e)) { ProcessReceived(e); } return; } }