Skip to content

Commit

Permalink
Wait sleep microseconds for cpu % downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsx0 committed May 28, 2018
1 parent 53407a2 commit 0447c85
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/MTProto/Proxy/SocketSelectReceiverThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void AddConnection(ProxyConnection connection)
Start();
}
}
connection.Socket.ReceiveTimeout = 30000;
}
public void RemoveConnection(ProxyConnection connection)
{
Expand Down Expand Up @@ -147,6 +148,10 @@ private void ThreadLoop(object threadObj)
{
SocketConnections[skt].Close();
}
if (ToRead.Count + ToError.Count != 0)
{
Waiter.SleepMicro(Timeout);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/MTProto/Proxy/SocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public void Stop()
{
return;
}
ServerSocket.Shutdown(SocketShutdown.Both);
ServerSocket.Close();
using (ServerSocket) ;
ServerSocket = null;
Works = false;
}
Expand Down
13 changes: 13 additions & 0 deletions src/MTProto/Waiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Net.Sockets;

namespace MTProto
{
public class Waiter
{
private static readonly Socket skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public static void SleepMicro(int microseconds)
{
skt.Poll(microseconds, SelectMode.SelectRead);
}
}
}
5 changes: 5 additions & 0 deletions src/tgsocks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ static void Main(string[] args)
proxyManager.Connections.OnConnectionRemoved += Connections_OnConnectionRemoved;

Log("OK, Load config complete, starting");
Console.CancelKeyPress += (a, b) => {
Log("Stopping...");
proxyManager.Stop();
Log("Stopped");
};
proxyManager.Start();
Log("Server started");
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
Expand Down

0 comments on commit 0447c85

Please # to comment.