public ChannelWrapperTests(TaskClientConnector connector) : base(connector) { // init various things receiver = new SocketAsyncEventArgs(); receiver.SetBuffer(receiveBuffer, 0, receiveBuffer.Length); receiverArgs = new DirectBytesReceivedEventArgs(receiver); sender = new SocketAsyncEventArgs(); sender.SetBuffer(sendBuffer, 0, sendBuffer.Length); sendBufferWriter = new MemoryStream(sendBuffer, true); // establish a connection Task<Socket> task = connector.ConnectAsync(); task.Wait(TimeSpan.FromSeconds(2)); wrapper = new ChannelWrapperBase(task.Result, receiver, receiverArgs, sender, sendBufferWriter); WaitForServerConnections(1); // get the other end of the socket otherEnd = Connections[0]; // set up event handlers wrapper.BytesReceived += OnBytesReceived; wrapper.Closed += OnClosed; wrapper.DuplexChannelClosed += OnDuplexChannelClosed; // start receive loop wrapper.Start(); }
/// <summary> /// Creates the non-reusable socket wrapper. /// </summary> /// <param name="socket">socket channel to use for communications</param> /// <param name="receiver"><see cref="SocketAsyncEventArgs"/> to be used for receiving, it should have its buffer already set up</param> /// <param name="receivedEventArgs"><see cref="DirectBytesReceivedEventArgs"/> object to be used for <see cref="BytesReceived"/> event notifications (should point to <paramref name="receiver"/>)</param> /// <param name="sender"><see cref="SocketAsyncEventArgs"/> to be used for sending, it should have its buffer already set up</param> /// <param name="sendBufferWriter"><see cref="MemoryStream"/> to be used for writing the data to sending buffer. It should already point to <paramref name="sender"/>'s buffer</param> /// <remarks>All these weird arguments allow for preallocation and pooling of <see cref="SocketAsyncEventArgs"/> and other related resources</remarks> public ChannelWrapperBase(Socket socket, SocketAsyncEventArgs receiver, DirectBytesReceivedEventArgs receivedEventArgs, SocketAsyncEventArgs sender, MemoryStream sendBufferWriter) { this.socket = socket; socket.NoDelay = true; socket.Blocking = false; this.receiver = receiver; receiver.Completed += OnReceiveCompleted; this.receivedEventArgs = receivedEventArgs; receivedEventArgs.ProceedReceive += OnProceedReceive; this.sender = sender; sender.Completed += OnSendCompleted; this.sendBufferWriter = sendBufferWriter; outgoingBufferOffset = sender.Offset; }
public ImmutableChannel(Socket channelSocket, SocketAsyncEventArgs receiver, SocketAsyncEventArgs sender, DirectBytesReceivedEventArgs receiverArgs, MemoryStream senderWriter, object additionalData = null) { this.channelSocket = channelSocket; wrapper = new ChannelWrapperBase(channelSocket, receiver, receiverArgs, sender, senderWriter); wrapper.BytesReceived += OnBytesReceived; wrapper.Closed += OnChannelClosed; wrapper.DuplexChannelClosed += OnChannelError; this.additionalData = additionalData; }