Пример #1
0
        protected sealed override async Task TcpAcceptedImplAsync(NetTcpListenerPort listener, ConnSock s)
        {
            using (SslSock ssl = new SslSock(s))
            {
                await ssl.StartSslServerAsync(this.Options.SslServerAuthenticationOptions, s.GrandCancel);

                ssl.UpdateSslSessionInfo();

                await SslAcceptedImplAsync(listener, ssl);
            }
        }
Пример #2
0
    async Task ListenerAcceptNewSocketCallback(NetTcpListenerPort listener, ConnSock newSock)
    {
        using (var connection = new KestrelStackConnection(newSock, this.PipeScheduler))
        {
            // Note:
            // In ASP.NET Core 2.2 or higher, Dispatcher.OnConnection() will return Task.
            // Otherwise, Dispatcher.OnConnection() will return void.
            // Then we need to use the reflection to call the OnConnection() method indirectly.
            Task?middlewareTask = Dispatcher._PrivateInvoke("OnConnection", connection) as Task;

            // Wait for transport to end
            await connection.StartAsync();

            // Wait for middleware to end
            if (middlewareTask != null)
            {
                await middlewareTask;
            }

            connection._DisposeSafe();
        }
    }
Пример #3
0
            protected override async Task SslAcceptedImplAsync(NetTcpListenerPort listener, SslSock sock)
            {
                using (var stream = sock.GetStream())
                    using (var r = new StreamReader(stream))
                        using (var w = new StreamWriter(stream))
                        {
                            while (true)
                            {
                                string?recv = await r.ReadLineAsync();

                                if (recv == null)
                                {
                                    return;
                                }

                                Con.WriteLine(recv);

                                await w.WriteLineAsync("[" + recv + "]\r\n");

                                await w.FlushAsync();
                            }
                        }
            }
Пример #4
0
 async Task ListenerCallbackAsync(NetTcpListenerPort listener, ConnSock newSock)
 {
     await TcpAcceptedImplAsync(listener, newSock);
 }
Пример #5
0
 protected abstract Task TcpAcceptedImplAsync(NetTcpListenerPort listener, ConnSock sock);
Пример #6
0
 protected abstract Task SslAcceptedImplAsync(NetTcpListenerPort listener, SslSock sock);
Пример #7
0
        protected override async Task SslAcceptedImplAsync(NetTcpListenerPort listener, SslSock sock)
        {
            using (PipeStream st = sock.GetStream())
            {
                sock.AttachHandle.SetStreamReceiveTimeout(this.Options.RecvTimeout);

                int magicNumber = await st.ReceiveSInt32Async();

                if (magicNumber != MagicNumber)
                {
                    throw new ApplicationException($"Invalid magicNumber = 0x{magicNumber:X}");
                }

                int clientVersion = await st.ReceiveSInt32Async();

                MemoryBuffer <byte> sendBuffer = new MemoryBuffer <byte>();
                sendBuffer.WriteSInt32(MagicNumber);
                sendBuffer.WriteSInt32(ServerVersion);
                await st.SendAsync(sendBuffer);

                SizedDataQueue <Memory <byte> > standardLogQueue = new SizedDataQueue <Memory <byte> >();
                try
                {
                    while (true)
                    {
                        if (standardLogQueue.CurrentTotalSize >= CoresConfig.LogProtocolSettings.BufferingSizeThresholdPerServer || st.IsReadyToReceive(sizeof(int)) == false)
                        {
                            var list = standardLogQueue.GetList();
                            standardLogQueue.Clear();
                            await LogDataReceivedInternalAsync(sock.EndPointInfo.RemoteIP._NonNullTrim(), list);
                        }

                        LogProtocolDataType type = (LogProtocolDataType)await st.ReceiveSInt32Async();

                        switch (type)
                        {
                        case LogProtocolDataType.StandardLog:
                        {
                            int size = await st.ReceiveSInt32Async();

                            if (size > CoresConfig.LogProtocolSettings.MaxDataSize)
                            {
                                throw new ApplicationException($"size > MaxDataSize. size = {size}");
                            }

                            Memory <byte> data = new byte[size];

                            await st.ReceiveAllAsync(data);

                            standardLogQueue.Add(data, data.Length);

                            break;
                        }

                        case LogProtocolDataType.KeepAlive:
                            break;

                        default:
                            throw new ApplicationException("Invalid LogProtocolDataType");
                        }
                    }
                }
                finally
                {
                    await LogDataReceivedInternalAsync(sock.EndPointInfo.RemoteIP._NonNullTrim(), standardLogQueue.GetList());
                }
            }
        }