public async Task ThrowsWhenUnderlyingStreamClosed() { var pair = GameSocketPair.Create(); var sendBuffer = new byte[] { 0xde, 0xad, 0xbe, 0xef }; await pair.ServerSocket.SendAsync(new ArraySegment <byte>(sendBuffer), GameSocketMessageType.Binary, endOfMessage : true, cancellationToken : CancellationToken.None); var receiveBuffer = new byte[32]; var result = await pair.ClientSocket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), CancellationToken.None); Assert.Equal(GameSocketMessageType.Binary, result.MessageType); // Close the client socket's read end pair.ClientStream.ReadStream.End(); // Assert.Throws doesn't support async :( try { await pair.ClientSocket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), CancellationToken.None); // The exception should prevent this line from running Assert.False(true, "Expected an exception to be thrown!"); } catch (GameSocketException ex) { Assert.Equal(GameSocketError.ConnectionClosedPrematurely, ex.GameSocketErrorCode); } }
public async Task ServerToClientBinaryMessage() { var pair = GameSocketPair.Create(); var sendBuffer = new byte[] { 0xde, 0xad, 0xbe, 0xef }; await pair.ServerSocket.SendAsync(new ArraySegment <byte>(sendBuffer), GameSocketMessageType.Binary, endOfMessage : true, cancellationToken : CancellationToken.None); var receiveBuffer = new byte[32]; var result = await pair.ClientSocket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), CancellationToken.None); Assert.Equal(GameSocketMessageType.Binary, result.MessageType); Assert.Equal(sendBuffer, receiveBuffer.Take(result.Count).ToArray()); }
public async Task ServerToClientTextMessage() { const string message = "Hello, World!"; var pair = GameSocketPair.Create(); var sendBuffer = Encoding.UTF8.GetBytes(message); await pair.ServerSocket.SendAsync(new ArraySegment <byte>(sendBuffer), GameSocketMessageType.Text, endOfMessage : true, cancellationToken : CancellationToken.None); var receiveBuffer = new byte[32]; var result = await pair.ClientSocket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), CancellationToken.None); Assert.Equal(GameSocketMessageType.Text, result.MessageType); Assert.Equal(message, Encoding.UTF8.GetString(receiveBuffer, 0, result.Count)); }