Esempio n. 1
0
        private async Task Echo(GameSocket gameSocket, CancellationToken cancellationToken)
        {
            var buffer = new byte[1024 * 4];
            var result = await gameSocket.ReceiveAsync(new ArraySegment <byte>(buffer), cancellationToken);

            while (!result.CloseStatus.HasValue)
            {
                await gameSocket.SendAsync(new ArraySegment <byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, cancellationToken);

                result = await gameSocket.ReceiveAsync(new ArraySegment <byte>(buffer), cancellationToken);
            }
            await gameSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, cancellationToken);
        }
Esempio n. 2
0
        internal Task SendAsync(ArraySegment <byte> buffer, int messageType, bool endOfMessage, CancellationToken cancel)
        {
            // Remap close messages to CloseAsync.  System.Net.GameSockets.GameSocket.SendAsync does not allow close messages.
            if (messageType == 0x8)
            {
                return(RedirectSendToCloseAsync(buffer, cancel));
            }
            else if (messageType == 0x9 || messageType == 0xA)
            {
                // Ping & Pong, not allowed by the underlying APIs, silently discard.
                return(Task.CompletedTask);
            }

            return(_gameSocket.SendAsync(buffer, OpCodeToEnum(messageType), endOfMessage, cancel));
        }
Esempio n. 3
0
        private async Task Echo(HttpContext context, GameSocket gameSocket, ILogger logger)
        {
            var buffer = new byte[1024 * 4];
            var result = await gameSocket.ReceiveAsync(buffer.AsMemory(), CancellationToken.None);

            LogFrame(logger, gameSocket, result, buffer);
            while (result.MessageType != GameSocketMessageType.Close)
            {
                // If the client send "ServerClose", then they want a server-originated close to occur
                string content = "<<binary>>";
                if (result.MessageType == GameSocketMessageType.Text)
                {
                    content = Encoding.UTF8.GetString(buffer, 0, result.Count);
                    if (content.Equals("ServerClose"))
                    {
                        await gameSocket.CloseAsync(GameSocketCloseStatus.NormalClosure, "Closing from Server", CancellationToken.None);

                        logger.LogDebug($"Sent Frame Close: {GameSocketCloseStatus.NormalClosure} Closing from Server");
                        return;
                    }
                    else if (content.Equals("ServerAbort"))
                    {
                        context.Abort();
                    }
                }

                await gameSocket.SendAsync(new ArraySegment <byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);

                logger.LogDebug($"Sent Frame {result.MessageType}: Len={result.Count}, Fin={result.EndOfMessage}: {content}");

                result = await gameSocket.ReceiveAsync(buffer.AsMemory(), CancellationToken.None);

                LogFrame(logger, gameSocket, result, buffer);
            }
            await gameSocket.CloseAsync(gameSocket.CloseStatus.Value, gameSocket.CloseStatusDescription, CancellationToken.None);
        }