예제 #1
0
        public async Task <RocketResult> ReceiveStreamAsync(MemoryStream stream, CancellationToken token)
        {
            var result = new RocketResult();

            try {
                await mReceiveLock.WaitAsync(token);

                stream.Seek(0, SeekOrigin.Begin);
                var buffer = new ArraySegment <byte>(new byte[8192]);
                IWebSocketReceiveResult socketResult = null;
                do
                {
                    socketResult = await mSocket.ReceiveAsync(buffer, token);

                    if (socketResult.MessageType == WebSocketMessageType.Close)
                    {
                        await mSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "normal closure", token);

                        return(result.SetClosed());
                    }

                    stream.Write(buffer.Array, buffer.Offset, socketResult.Count);
                } while (!socketResult.EndOfMessage);

                stream.Seek(0, SeekOrigin.Begin);
                return(result);
            } catch (WebSocketException ex) {
                return(result.SetException(ex)
                       .SetClosed());
            } finally {
                try {
                    mReceiveLock.Release();
                } catch (SemaphoreFullException) { }
            }
        }
예제 #2
0
        public async Task <RocketResult> SendStreamAsync(MemoryStream stream, CancellationToken token)
        {
            var result = new RocketResult();

            try {
                await mSendLock.WaitAsync(token);

                await mSocket.SendAsync(GetBuffer(stream),
                                        WebSocketMessageType.Binary,
                                        true,
                                        token);

                return(result);
            } catch (WebSocketException ex) {
                return(result.SetException(ex)
                       .SetClosed());
            } finally {
                try {
                    mSendLock.Release();
                } catch (SemaphoreFullException) { }
            }
        }