public void EchoTest() { int port = EchoServerFixture.EchoServerPort; string data = "testData"; string message = ""; EasyClient client = new EasyClient(); CountdownEvent countdownEvent = new CountdownEvent(1); client.ConnectHandler(socket => { byte[] sendData = Encoding.UTF8.GetBytes(data); socket.Send(sendData, sendSize => { _output.WriteLine("complete send - size : " + sendSize); }); socket.Receive(receivedData => { _output.WriteLine("complete receive - size : " + receivedData.Length); message = Encoding.UTF8.GetString(receivedData); countdownEvent.Signal(); }); }); client.ExceptionHandler(exception => { _output.WriteLine("received socket exception" + exception); }); client.Connect("127.0.0.1", port); countdownEvent.Wait(); Assert.Equal(data, message); }
public void ServerIdleTimeoutTest() { int port = 16001; bool timeout = false; CountdownEvent countdownEvent = new CountdownEvent(1); EasyServer server = new EasyServer(); Task.Run(() => { server.ConnectHandler(socket => { socket.SocketConfiguration.IdleTimeout = 2000; string socketId = socket.SocketId; socket.IdleTimeoutHandler(() => { _output.WriteLine($"[{socketId}] server idle timeout handler"); timeout = true; countdownEvent.Signal(); }); socket.CloseHandler(() => { _output.WriteLine($"[{socketId}] server socket close handler"); }); socket.ExceptionHandler(exception => { _output.WriteLine($"[{socketId}] server socket exception handler - {exception}"); }); socket.Receive(receivedData => { string stringData = Encoding.UTF8.GetString(receivedData); _output.WriteLine($"[{socketId}] server socket receive - {stringData}:{stringData.Length}"); }); }); server.ExceptionHandler(exception => { _output.WriteLine($"server start exception handler - {exception}"); }); server.Start("127.0.0.1", port); }).Wait(1000); EasyClient client = new EasyClient(); client.ConnectHandler(socket => { // skip }); client.ExceptionHandler(exception => { _output.WriteLine($"client connect exception handler - {exception}"); }); client.Connect("127.0.0.1", port); countdownEvent.Wait(5000); server.Stop(); Assert.True(timeout); }
public void EchoCuttedSendPacketTest() { int port = EchoServerFixture.EchoServerPort; string firstData = "test"; string secondData = "Data"; string response = ""; EasyClient client = new EasyClient(); CountdownEvent countdownEvent = new CountdownEvent(1); client.ConnectHandler(socket => { byte[] firstSendData = Encoding.UTF8.GetBytes(firstData); socket.Send(firstSendData, sendSize => { _output.WriteLine("complete send - data:{0}, size:{1}", Encoding.UTF8.GetString(firstSendData), sendSize); }); byte[] secondSendData = Encoding.UTF8.GetBytes(secondData); socket.Send(secondSendData, sendSize => { _output.WriteLine("complete send - data:{0}, size:{1}", Encoding.UTF8.GetString(secondSendData), sendSize); }); socket.Receive(receivedData => { string data = Encoding.UTF8.GetString(receivedData); _output.WriteLine("complete receive - data:{0}, size:{1}", data, data.Length); response += data; if (response.Equals(firstData + secondData)) { countdownEvent.Signal(); } }); }); client.ExceptionHandler(exception => { _output.WriteLine("received socket exception" + exception); }); client.Connect("127.0.0.1", port); countdownEvent.Wait(); Assert.Equal(firstData + secondData, response); }
public void Run(string addr, int port) { _client.ConnectHandler(socket => { string socketId = socket.SocketId; _logger.LogInformation($"[{socketId}] Connected"); socket.ExceptionHandler(exception => { _logger.LogError($"[{socketId}] Exception - {exception.Message}"); }); socket.ReadTimeoutHandler(() => { _logger.LogInformation($"[{socketId}] Read Timeout"); }); socket.CloseHandler(() => { _logger.LogInformation($"[{socketId}] Closed"); }); byte[] sendData = Encoding.UTF8.GetBytes("testData"); socket.Send(sendData, size => { string sendStringData = Encoding.UTF8.GetString(sendData); _logger.LogInformation($"[{socketId}] Send data - {sendStringData}, size:{size}"); }); socket.Receive(receiveData => { string recvStringData = Encoding.UTF8.GetString(receiveData); _logger.LogInformation($"[{socketId}] Receive data - {recvStringData}, size:{receiveData.Length}"); }); }); _client.ExceptionHandler(exception => { _logger.LogError($"Exception - {exception.Message}"); }); _client.Connect(addr, port); _logger.LogInformation("Press any key to terminate the client process...."); Console.ReadKey(); }
public void ClosedSocketFromServer() { int port = 15001; string data = "testData"; bool closed = false; EasyServer server = new EasyServer(); Task.Run(() => { server.ConnectHandler(socket => { string socketId = socket.SocketId; socket.CloseHandler(() => { _output.WriteLine($"[{socketId}] server socket close handler"); }); socket.ExceptionHandler(exception => { _output.WriteLine($"[{socketId}] server socket exception handler - {exception}"); }); socket.Receive(receivedData => { string stringData = Encoding.UTF8.GetString(receivedData); _output.WriteLine($"[{socketId}] server socket receive - {stringData}:{stringData.Length}"); socket.Close(); }); }); server.ExceptionHandler(exception => { _output.WriteLine($"server start exception handler - {exception}"); }); server.Start("127.0.0.1", port); }).Wait(1500); EasyClient client = new EasyClient(); CountdownEvent countdownEvent = new CountdownEvent(1); client.ConnectHandler(socket => { string socketId = socket.SocketId; socket.CloseHandler(() => { _output.WriteLine($"[{socketId}] client socket close handler"); closed = true; countdownEvent.Signal(); }); socket.ExceptionHandler(exception => { _output.WriteLine($"[{socketId}] client socket close handler"); }); byte[] sendData = Encoding.UTF8.GetBytes(data); socket.Send(sendData, sendSize => { _output.WriteLine($"[{socketId}] client socket send - {data}:{data.Length}"); }); socket.Receive(receivedData => { string stringData = Encoding.UTF8.GetString(receivedData); _output.WriteLine($"[{socketId}] client socket receive - {stringData}:{stringData.Length}"); }); }); client.ExceptionHandler(exception => { _output.WriteLine($"client connect exception handler - {exception}"); }); client.Connect("127.0.0.1", port); countdownEvent.Wait(); server.Stop(); Assert.True(closed); }