Exemplo n.º 1
0
        public TcpServerBase(TcpServerOptions options)
        {
            try
            {
                this.Options = options;

                Listener = this.Options.TcpIp.CreateListener(new TcpListenParam(ListenerCallbackAsync, options.RateLimiterConfigName, this.Options.EndPoints.ToArray()));
            }
            catch
            {
                this._DisposeSafe();
                throw;
            }
        }
        public TelnetStreamWatcherBase(TelnetStreamWatcherOptions options)
        {
            this.Options = options;

            Listener = TcpIp.CreateListener(new TcpListenParam(async(listener, sock) =>
            {
                try
                {
                    Con.WriteDebug($"TelnetStreamWatcher({this.ToString()}: Connected: {sock.EndPointInfo._GetObjectDump()}");

                    using (var destStream = sock.GetStream())
                    {
                        if (sock.Info.Ip.RemoteIPAddress == null || Options.IPAccessFilter(sock.Info.Ip.RemoteIPAddress) == false)
                        {
                            Con.WriteDebug($"TelnetStreamWatcher({this.ToString()}: Access denied: {sock.EndPointInfo._GetObjectDump()}");

                            await destStream.WriteAsync("You are not allowed to access to this service.\r\n\r\n"._GetBytes_Ascii());

                            await Task.Delay(100);

                            return;
                        }

                        using (var pipePoint = await SubscribeImplAsync())
                        {
                            // ソケットから Enter キー入力を待機する
                            Task keyInputTask = TaskUtil.StartAsyncTaskAsync(async() =>
                            {
                                using StreamReader lineReader = new StreamReader(destStream);

                                while (true)
                                {
                                    string?line = await lineReader.ReadLineAsync();

                                    if (line == null)
                                    {
                                        break;
                                    }

                                    line = line.Trim();

                                    if (line._IsSamei("s"))
                                    {
                                        // Socket リストの表示
                                        var list = LocalNet.GetSockList().OrderBy(x => x.Connected);

                                        StringWriter w = new StringWriter();

                                        w.WriteLine();

                                        foreach (var sock in list)
                                        {
                                            string tmp = sock._GetObjectDump();
                                            w.WriteLine(tmp);
                                        }

                                        w.WriteLine();

                                        w.WriteLine($"Total sockets: {list.Count()}");

                                        w.WriteLine();

                                        byte[] data = w.ToString()._GetBytes_Ascii();

                                        var pipe = pipePoint;
                                        if (pipe.CounterPart != null)
                                        {
                                            lock (pipe.CounterPart.StreamWriter.LockObj)
                                            {
                                                if (pipe.CounterPart.StreamWriter.NonStopWriteWithLock(data, false, FastStreamNonStopWriteMode.DiscardExistingData) != 0)
                                                {
                                                    // To avoid deadlock, CompleteWrite() must be called from other thread.
                                                    // (CompleteWrite() ==> Disconnect ==> Socket Log will recorded ==> ReceiveLog() ==> this function will be called!)
                                                    TaskUtil.StartSyncTaskAsync(() => pipe.CounterPart.StreamWriter.CompleteWrite(false), false, false)._LaissezFaire(true);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // GC
                                        Dbg.WriteLine($"Manual GC is called by the administrator.");

                                        long start = FastTick64.Now;
                                        Dbg.GcCollect();
                                        long end = FastTick64.Now;

                                        long spentTime = end - start;

                                        Dbg.WriteLine($"Manual GC Took Time: {spentTime} msecs.");
                                    }
                                }
                            });

                            try
                            {
                                // ソケットに対して、pipePoint のストリームをそのまま非同期で流し込む
                                using (var pipeStub = pipePoint.GetNetAppProtocolStub())
                                    using (var srcStream = pipeStub.GetStream())
                                    {
                                        await srcStream.CopyToAsync(destStream, sock.GrandCancel);
                                    }
                            }
                            finally
                            {
                                await UnsubscribeImplAsync(pipePoint);

                                await pipePoint.CleanupAsync(new DisconnectedException());

                                await keyInputTask._TryAwait(noDebugMessage: true);
                            }
                        }
                    }
                }
                finally
                {
                    Con.WriteDebug($"TelnetStreamWatcher({this.ToString()}: Disconnected: {sock.EndPointInfo._GetObjectDump()}");
                }
            },
                                                               "TelnetStreamWatcher",
                                                               this.Options.EndPoints.ToArray()));

            this.AddIndirectDisposeLink(this.Listener);
        }
Exemplo n.º 3
0
        static void Net_Test12_AcceptLoop2(int port = 80)
        {
            new ThreadObj(param =>
            {
                ThreadObj.Current.Thread.Priority = System.Threading.ThreadPriority.Highest;
                int last = 0;
                while (true)
                {
                    int value = Environment.TickCount;
                    int span  = value - last;
                    last      = value;
                    long mem  = mem = GC.GetTotalMemory(false);
                    try
                    {
                        Console.WriteLine("tick: " + span + "   mem = " + mem / 1024 / 1024 + "    sock = " + LocalNet.GetOpenedSockCount());
                    }
                    catch { }
                    ThreadObj.Sleep(100);
                }
            });

            if (true)
            {
                NetTcpListener listener = LocalNet.CreateListener(new TcpListenParam(
                                                                      async(listener2, sock) =>
                {
                    while (true)
                    {
                        var stream     = sock.GetStream();
                        StreamReader r = new StreamReader(stream);

                        while (true)
                        {
                            string?line = await r.ReadLineAsync();

                            if (line._IsEmpty())
                            {
                                break;
                            }
                        }
                        int segmentSize = 400;
                        int numSegments = 1000;
                        int totalSize   = segmentSize * numSegments;

                        string ret =
                            $@"HTTP/1.1 200 OK
Content-Length: {totalSize}

";

                        await stream.WriteAsync(ret._GetBytes_Ascii());

                        byte[] buf = Util.Rand(numSegments);
                        for (int i = 0; i < numSegments; i++)
                        {
                            await stream.WriteAsync(buf);
                        }
                    }
                },
                                                                      null,
                                                                      port));

                listener.HideAcceptProcError = true;

                ThreadObj.Sleep(-1);
            }
        }