예제 #1
0
 public ConnectionLoop(IDuplexPipe transport, SocketDescriptor socketDescriptor, PeerManager peerManager,
                       ChannelReader <byte> writeAvailReceiver, ChannelWriter <byte> eventNotify, ILogger <ConnectionLoop>?logger = null, Func <Task>?cleanup = null)
 {
     PeerManager         = peerManager;
     _transport          = transport;
     _socketDescriptor   = socketDescriptor;
     _logger             = logger;
     _writeAvailReceiver = writeAvailReceiver;
     _eventNotify        = eventNotify;
     _cleanup            = cleanup;
     _id = Guid.NewGuid();
 }
        public (SocketDescriptor, ChannelReader <byte>) GetNewSocket(PipeWriter writer)
        {
            Interlocked.Increment(ref CurrentNum);

            // We only ever need a channel of depth 1 here: if we returned a non-full write to the PeerManager,
            // we will eventually get notified that there is room in the socket to write new bytes, which will generate
            // an event. That event will be popped off the queue before we call WriteBufferSpaceAvail, ensuring that we
            // have room to push a new `1` if, during the WriteBufferSpaceAvail call, send_data() returns a non-full write.
            var writeAvail = Channel.CreateBounded <byte>(1);

            Sockets[CurrentNum] = new SocketDescriptor((UIntPtr)CurrentNum, writer, writeAvail.Writer);
            return(Sockets[(int)CurrentNum], writeAvail.Reader);
        }