Ejemplo n.º 1
0
        private async Task SendAsyncImpl <T>(WebSocketsFrame.OpCodes opCode, T message, WebSocketsFrame.FrameFlags flags) where T : struct, IMessageWriter
        {
            //TODO: come up with a better way of getting ordered access to the socket
            var  writeLock = GetWriteSemaphore();
            bool haveLock  = writeLock.Wait(0);

            if (!haveLock)
            {   // try to acquire asynchronously instead, then
                await writeLock.WaitAsync();
            }
            try
            {
                WebSocketServer.WriteStatus(ConnectionType, $"Writing {opCode} message ({message.GetPayloadLength()} bytes)...");
                await WebSocketProtocol.WriteAsync(this, opCode, flags, ref message);

                if (opCode == WebSocketsFrame.OpCodes.Close)
                {
                    connection.Output.Complete();
                }
            }
            finally
            {
                writeLock.Release();
            }
        }
Ejemplo n.º 2
0
        private async Task SendAsyncImpl <T>(WebSocketsFrame.OpCodes opCode, T message) where T : struct, IMessageWriter
        {
            //TODO: come up with a better way of getting ordered access to the socket
            var  writeLock = GetWriteSemaphore();
            bool haveLock  = writeLock.Wait(0);

            if (!haveLock)
            {   // try to acquire asynchronously instead, then
                await writeLock.WaitAsync();
            }
            try
            {
                WebSocketServer.WriteStatus($"Writing {opCode} message...");
                await WebSocketProtocol.WriteAsync(this, opCode, ref message);

                if (opCode == WebSocketsFrame.OpCodes.Close)
                {
                    Close();
                }
            }
            finally
            {
                writeLock.Release();
            }
        }