예제 #1
0
        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);
            }
        }
예제 #2
0
        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());
        }
예제 #3
0
        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));
        }