protected void IO_Completed(object sender, SocketAsyncEventArgs args) { //操作成功 if (args.SocketError == SocketError.Success) { ChannelSocketAsyncEventArgs eargs = args as ChannelSocketAsyncEventArgs; ProcessCompleted(eargs); return; } //收到断开连接的请求 else if (args.SocketError == SocketError.ConnectionReset) { Trace.WriteLine($"args.SocketError: SocketError.ConnectionReset"); Shutdown(); return; } else if (args.SocketError == SocketError.WouldBlock) { Trace.WriteLine($"args.SocketError: SocketError.WouldBlock"); } else { Trace.WriteLine($"args.SocketError:{ args.SocketError }"); Shutdown(); return; } }
private void ProcessSend(ChannelSocketAsyncEventArgs args) { if (args.Buffer != null) { args.SetBuffer(null, 0, 0); } invoker.fireOnChannelSend(args.ByteBuf, ExitAndTrySending); }
public AbstractSocketChannel() { this.sendingSocketAsyncEventArgs = new ChannelSocketAsyncEventArgs(); this.receivingSocketAsyncEventArgs = new ChannelSocketAsyncEventArgs(); this.sendingSocketAsyncEventArgs.Completed += IO_Completed; this.receivingSocketAsyncEventArgs.Completed += IO_Completed; config = new ChannelConfig(); }
public TcpClientChannel() { this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { Blocking = false, NoDelay = true }; connectEventArgs = new ChannelSocketAsyncEventArgs(); connectEventArgs.Completed += IO_Completed; }
/// <summary> /// 接收完成并处理数据 /// </summary> /// <param name="args"></param> private void ProcessReceive(ChannelSocketAsyncEventArgs args) { try { if (args.BytesTransferred > 0) { IByteBuf buf = args.ByteBuf; buf.SetWriteIndex(args.BytesTransferred); invoker.fireOnChannelRead(buf, ExitAndTryReceiving); } } catch (Exception ex) { Trace.WriteLine("processReceive.Unpacking:" + ex.Message); Shutdown(); } }
protected virtual void ProcessCompleted(ChannelSocketAsyncEventArgs args) { switch (args.LastOperation) { case SocketAsyncOperation.Receive: ProcessReceive(args); break; case SocketAsyncOperation.Send: ProcessSend(args); break; case SocketAsyncOperation.Disconnect: Trace.WriteLine("Disconnect"); break; default: Trace.WriteLine("default"); break; } }
private void ProcessConnect(ChannelSocketAsyncEventArgs args) { var channel = NewChannelFactory(args.ConnectSocket); invoker.fireOnChannelRead(channel); }