Browse Source

Add Global IO processor.

justin.xing 4 years ago
parent
commit
ec24cd3a1e

+ 5 - 0
DotnetRtmpServer/Net/Config.cs

@@ -20,6 +20,11 @@ namespace DotnetRtmpServer.Net
         /// </summary>
         public static string DefaultApp = "live";
 
+        /// <summary>
+        /// If enable this parameter, will reduce the cpu usage, but the performance will also be reduced.
+        /// </summary>
+        public static bool EnableLowPowerMode = true;
+
         /// <summary>
         /// This will increase the memory usage, be careful.
         /// </summary>

+ 28 - 23
DotnetRtmpServer/Net/RtmpConnection.cs

@@ -101,44 +101,49 @@ namespace DotnetRtmpServer.Net
                 IsClosed = true;
                 await OnClosedAsync().ConfigureAwait(false);
                 DoDispose();
-                Logger.WriteLineInfo($"[{ConnectionType}] Connection closed, ConnectionId={Id}");
+                Logger.WriteLineInfo($"[{ConnectionType}] Connection {Id} closed.");
             }
         }
 
 
-        public void StartProcess()
+        public void StartIoProcess()
         {
-            Task.Run(DoProcessAsync);
+            Task.Run(DoStartIoProcessAsync);
         }
 
 
-        private async void DoProcessAsync()
+        public async Task ProcessOnceAsync()
         {
-            while (!IsClosed)
+            try
             {
-                try
+                var hw = await WriteAsync().ConfigureAwait(false);
+                var hr = await ReadAsync().ConfigureAwait(false);
+                if (hw || hr)
                 {
-                   var hw = await WriteAsync().ConfigureAwait(false);
-                   var hr = await ReadAsync().ConfigureAwait(false);
-                   if (hw || hr)
-                   {
-                       LastPing = DateTime.UtcNow;
-                   }
-                   else
-                   {
-                       if (DateTime.UtcNow - LastPing > IoTimeout)
-                       {
-                           await CloseAsync().ConfigureAwait(false);
-                           Logger.WriteLineWarn($"[{ConnectionType}]Connection closed because it has no writing or reading over {IoTimeout.Seconds} seconds.");
-                       }
-                   }
+                    LastPing = DateTime.UtcNow;
                 }
-                catch
+                else
                 {
-                    await CloseAsync().ConfigureAwait(false);
-                    Logger.WriteLineWarn($"Connection {Id} closed.");
+                    if (DateTime.UtcNow - LastPing > IoTimeout)
+                    {
+                        await CloseAsync().ConfigureAwait(false);
+                        Logger.WriteLineWarn($"[{ConnectionType}]Connection closed because it has no writing or reading over {IoTimeout.Seconds} seconds.");
+                    }
                 }
             }
+            catch
+            {
+                await CloseAsync().ConfigureAwait(false);
+                Logger.WriteLineWarn($"Connection {Id} closed.");
+            }
+        }
+
+        private async void DoStartIoProcessAsync()
+        {
+            while (!IsClosed)
+            {
+               await ProcessOnceAsync().ConfigureAwait(false);
+            }
         }
 
 

+ 36 - 2
DotnetRtmpServer/Net/RtmpServer.cs

@@ -57,6 +57,19 @@ namespace DotnetRtmpServer.Net
         {
             _listener.Start();
             Started = true;
+            StartListener();
+            if (Config.EnableLowPowerMode)
+            {
+                StartIoProcess();
+                Logger.WriteLineInfo("Global IO processor started.");
+            }
+
+            Logger.WriteLineInfo("RTMP server started.");
+        }
+
+
+        private void StartListener()
+        {
             Task.Run(async () =>
             {
                 try
@@ -74,7 +87,24 @@ namespace DotnetRtmpServer.Net
                     Logger.WriteLineError(e.ToString());
                 }
             });
-            Logger.WriteLineInfo("RTMP server started.");
+        }
+
+        private void StartIoProcess()
+        {
+            Task.Run(async () =>
+            {
+                try
+                {
+                    while (Started)
+                    {
+                        await Task.WhenAll(_connections.Select(x => x.Value.ProcessOnceAsync()));
+                    }
+                }
+                catch (Exception e)
+                {
+                    Logger.WriteLineError(e.ToString());
+                }
+            });
         }
 
 
@@ -119,7 +149,11 @@ namespace DotnetRtmpServer.Net
                  exists.DisposeAsync().AsTask().Wait();
                  return connection;
              });
-            connection.StartProcess();
+            if (!Config.EnableLowPowerMode)
+            {
+                connection.StartIoProcess();
+                Logger.WriteLineInfo($"IO processor for connection {connection.Id} started.");
+            }
         }