static void RunLoop(ServerType serverType) { loop = new Loop(); IDisposable handle; var localEndPoint = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort); var remoteEndPoint = new IPEndPoint(IPAddress.Loopback, Port); switch (serverType) { case ServerType.Udp: Udp udp = loop .CreateUdp() .ReceiveStart(OnReceive); WritableBuffer buffer = CreateMessage(); udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted); handle = udp; break; case ServerType.Pipe: string name = GetPipeName(); handle = loop .CreatePipe() .ConnectTo(name, OnConnected); break; default: // Default tcp handle = loop .CreateTcp() .NoDelay(true) .ConnectTo(localEndPoint, remoteEndPoint, OnConnected); break; } Console.WriteLine($"{serverType}:Echo client loop starting."); loop.RunDefault(); Console.WriteLine($"{serverType}:Echo client loop dropped out"); handle?.Dispose(); }
static void StartServer(ServerType serverType) { loop = new Loop(); switch (serverType) { case ServerType.Udp: var endPoint = new IPEndPoint(IPAddress.Any, Port); server = loop .CreateUdp() .Bind(endPoint) .MulticastLoopback(true) .ReceiveStart(OnReceive); Console.WriteLine($"{serverType}:Echo server receive started."); break; case ServerType.Pipe: string name = GetPipeName(); server = loop .CreatePipe() .Listen(name, OnConnection); Console.WriteLine($"{serverType}:Echo server started on {name}."); break; default: // Default to tcp server = loop .CreateTcp() .SimultaneousAccepts(true) .Listen(EndPoint, OnConnection); Console.WriteLine($"{serverType}:Echo server started on {EndPoint}."); break; } loop.RunDefault(); Console.WriteLine($"{serverType}Echo server loop completed."); }