Ejemplo n.º 1
0
 private void InvokeConnectState(GameSocketState state)
 {
     if (this.GameSocketStateChangeEvent != null)
     {
         GameSocketStateChangeEvent.Invoke(this, state);
     }
 }
Ejemplo n.º 2
0
 private TestGameSocket(string subProtocol, ReceiverSenderBuffer readBuffer, ReceiverSenderBuffer writeBuffer)
 {
     _state         = GameSocketState.Open;
     _subProtocol   = subProtocol;
     _receiveBuffer = readBuffer;
     _sendBuffer    = writeBuffer;
 }
Ejemplo n.º 3
0
 public OwinGameSocketAdapter(IDictionary <string, object> gamesocketContext, string subProtocol)
 {
     _gamesocketContext = gamesocketContext;
     _sendAsync         = (GameSocketSendAsync)gamesocketContext[OwinConstants.GameSocket.SendAsync];
     _receiveAsync      = (GameSocketReceiveAsync)gamesocketContext[OwinConstants.GameSocket.ReceiveAsync];
     _closeAsync        = (GameSocketCloseAsync)gamesocketContext[OwinConstants.GameSocket.CloseAsync];
     _state             = GameSocketState.Open;
     _subProtocol       = subProtocol;
 }
Ejemplo n.º 4
0
        public override void Abort()
        {
            if (_state >= GameSocketState.Closed) // or Aborted
            {
                return;
            }

            _state = GameSocketState.Aborted;
            Close();
        }
Ejemplo n.º 5
0
 public override Task CloseOutputAsync(GameSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
 {
     // TODO: Validate state
     if (State == GameSocketState.Open)
     {
         _state = GameSocketState.CloseSent;
     }
     else if (State == GameSocketState.CloseReceived)
     {
         _state = GameSocketState.Closed;
     }
     return(_closeAsync((int)closeStatus, statusDescription, cancellationToken));
 }
Ejemplo n.º 6
0
        public async override Task CloseOutputAsync(GameSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            ThrowIfOutputClosed();

            var message = new Message(closeStatus, statusDescription);
            await _sendBuffer.SendAsync(message, cancellationToken);

            if (State == GameSocketState.Open)
            {
                _state = GameSocketState.CloseSent;
            }
            else if (State == GameSocketState.CloseReceived)
            {
                _state = GameSocketState.Closed;
                Close();
            }
        }
Ejemplo n.º 7
0
        public override async Task <GameSocketReceiveResult> ReceiveAsync(ArraySegment <byte> buffer, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            ThrowIfInputClosed();
            ValidateSegment(buffer);
            // TODO: InvalidOperationException if any receives are currently in progress.

            Message receiveMessage = _receiveMessage;

            _receiveMessage = null;
            if (receiveMessage == null)
            {
                receiveMessage = await _receiveBuffer.ReceiveAsync(cancellationToken);
            }
            if (receiveMessage.MessageType == GameSocketMessageType.Close)
            {
                _closeStatus            = receiveMessage.CloseStatus;
                _closeStatusDescription = receiveMessage.CloseStatusDescription ?? string.Empty;
                var result = new GameSocketReceiveResult(0, GameSocketMessageType.Close, true, _closeStatus, _closeStatusDescription);
                if (_state == GameSocketState.Open)
                {
                    _state = GameSocketState.CloseReceived;
                }
                else if (_state == GameSocketState.CloseSent)
                {
                    _state = GameSocketState.Closed;
                    Close();
                }
                return(result);
            }
            else
            {
                int  count        = Math.Min(buffer.Count, receiveMessage.Buffer.Count);
                bool endOfMessage = count == receiveMessage.Buffer.Count;
                Array.Copy(receiveMessage.Buffer.Array, receiveMessage.Buffer.Offset, buffer.Array, buffer.Offset, count);
                if (!endOfMessage)
                {
                    receiveMessage.Buffer = new ArraySegment <byte>(receiveMessage.Buffer.Array, receiveMessage.Buffer.Offset + count, receiveMessage.Buffer.Count - count);
                    _receiveMessage       = receiveMessage;
                }
                endOfMessage = endOfMessage && receiveMessage.EndOfMessage;
                return(new GameSocketReceiveResult(count, receiveMessage.MessageType, endOfMessage));
            }
        }
Ejemplo n.º 8
0
        public override async Task <GameSocketReceiveResult> ReceiveAsync(ArraySegment <byte> buffer, CancellationToken cancellationToken)
        {
            var rawResult = await _receiveAsync(buffer, cancellationToken);

            var messageType = OpCodeToEnum(rawResult.Item1);

            if (messageType == GameSocketMessageType.Close)
            {
                if (State == GameSocketState.Open)
                {
                    _state = GameSocketState.CloseReceived;
                }
                else if (State == GameSocketState.CloseSent)
                {
                    _state = GameSocketState.Closed;
                }
                return(new GameSocketReceiveResult(rawResult.Item3, messageType, rawResult.Item2, CloseStatus, CloseStatusDescription));
            }
            else
            {
                return(new GameSocketReceiveResult(rawResult.Item3, messageType, rawResult.Item2));
            }
        }
Ejemplo n.º 9
0
 public override void Dispose()
 {
     _state = GameSocketState.Closed;
 }
Ejemplo n.º 10
0
 public override void Abort()
 {
     _state = GameSocketState.Aborted;
 }