in SimpleJsonRpc/SimpleRpcServer.cs [73:117]
public async Task Start(int serverPort = 8000, IPAddress ip = null, int? broadcastPort = null)
{
ip = ip ?? IPAddress.Any;
serverListener = new TcpListener(ip, serverPort);
serverListener.Start();
Starting?.Invoke();
logger.Info("RPC Server started.");
logger.Info("Ready for client connection");
if (broadcastPort != null)
{
var token = broadcastTaskCancelation.Token;
var broadcastTask = BroadcastResponder.StartBroadcastResponder(serverPort,
broadcastPort.Value, token);
}
// this is our core loop - we need to:
// 1. read the request, and parse it.
// 2. call the requested method
// 3. return the response
try
{
while (true)
{
// 1a. get our connection and our instruction
var client = await serverListener.AcceptTcpClientAsync();
logger.Info($"Client connected: {((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()}");
// 1b-3. Handle our client in a seperate thread, so we don't block inbound
// connections while doing work.
// Also, suppress compiler warning that there isn't an await here. This is a fire-and-forget method.
#pragma warning disable 4014
Task.Factory.StartNew(() => HandleClient(client));
#pragma warning restore 4014
}
}
catch (ObjectDisposedException e)
{
// this happens when we stop the running server. This is normal.
logger.Info("Server stopped.");
}
}