Exemplo n.º 1
0
        private bool closeConnection()
        {
            _readyState = WsState.CLOSED;

            try
            {
                if (!_httpContext.IsNull())
                {
                    _httpContext.Response.Close();
                    _wsStream    = null;
                    _httpContext = null;
                }

                if (!_wsStream.IsNull())
                {
                    _wsStream.Dispose();
                    _wsStream = null;
                }

                if (!_tcpClient.IsNull())
                {
                    _tcpClient.Close();
                    _tcpClient = null;
                }

                return(true);
            }
            catch (Exception ex)
            {
                onError(ex.Message);
                return(false);
            }
        }
Exemplo n.º 2
0
    private void Awake()
    {
        _idle          = new WsState("idle");
        _idle.OnEnter += (IState state) =>
        {
            print("进入idle状态");
        };
        _idle.OnUpdate += (float f) => { print("idle中"); };

        _move           = new WsState("move");
        _move.OnEnter  += (IState state) => { print("进入move状态"); };
        _move.OnUpdate += (float f) =>
        {
            transform.position += transform.forward * f * speed;
            print("move 了一下");
        };

        _idleMove          = new WsTransition("idelMove", _idle, _move);
        _idleMove.OnCheck += () =>
        {
            return(_isMove);
        };
        _idle.AddTransition(_idleMove);

        _moveIdle          = new WsTransition("moveIdle", _move, _idle);
        _moveIdle.OnCheck += () =>
        {
            return(!_isMove);
        };
        _move.AddTransition(_moveIdle);

        _fsm = new WsStateMachine("root", _idle);
        _fsm.AddState(_move);
    }
Exemplo n.º 3
0
 private WebSocket()
 {
     _extensions = String.Empty;
     _forClose   = new Object();
     _forSend    = new Object();
     _protocol   = String.Empty;
     _readyState = WsState.CONNECTING;
 }
 internal virtual void next(WsState nextState, string @event)
 {
     if (outerInstance.log.IsDebugEnabled)
     {
         outerInstance.log.Debug("SessionWS state change (" + outerInstance.objectId + ") (" + @event + "): " + state + (state != nextState ? " -> " + nextState : ""));
     }
     state = nextState;
 }
Exemplo n.º 5
0
 private WebSocket()
 {
     _extensions          = String.Empty;
     _forClose            = new Object();
     _forSend             = new Object();
     _protocol            = String.Empty;
     _readyState          = WsState.CONNECTING;
     _unTransmittedBuffer = new SynchronizedCollection <WsFrame>();
 }
Exemplo n.º 6
0
        private void close(PayloadData data)
        {
      #if DEBUG
            Console.WriteLine("WS: Info@close: Current thread IsBackground ?: {0}", Thread.CurrentThread.IsBackground);
      #endif
            lock (_forClose)
            {
                // Whether the closing handshake has been started already ?
                if (_readyState == WsState.CLOSING ||
                    _readyState == WsState.CLOSED)
                {
                    return;
                }

                // Whether the closing handshake as server is started before the connection has been established ?
                if (_readyState == WsState.CONNECTING && !_client)
                {
                    sendResponseHandshake(HttpStatusCode.BadRequest);
                    onClose(new CloseEventArgs(data));

                    return;
                }

                _readyState = WsState.CLOSING;
            }

            // Whether a close status code that must not be set for send is used ?
            if (!canSendAsCloseFrame(data))
            {
                onClose(new CloseEventArgs(data));
                return;
            }

            closeHandshake(data);
      #if DEBUG
            Console.WriteLine("WS: Info@close: Exits close method.");
      #endif
        }
Exemplo n.º 7
0
        private bool closeResources()
        {
            _readyState = WsState.CLOSED;

            try
            {
                if (_client)
                {
                    closeResourcesAsClient();
                }
                else
                {
                    closeResourcesAsServer();
                }

                return(true);
            }
            catch (Exception ex)
            {
                onError(ex.Message);
                return(false);
            }
        }
Exemplo n.º 8
0
 private WebSocket()
 {
     _extensions = String.Empty;
     _forClose = new Object();
     _forSend = new Object();
     _protocol = String.Empty;
     _readyState = WsState.CONNECTING;
     _unTransmittedBuffer = new SynchronizedCollection<WsFrame>();
 }
Exemplo n.º 9
0
 private void onOpen()
 {
     _readyState = WsState.OPEN;
     startMessageLoop();
     Ext.Emit(OnOpen, this, EventArgs.Empty);
 }
Exemplo n.º 10
0
        private bool closeResources()
        {
            _readyState = WsState.CLOSED;

              try
              {
            if (_client)
              closeResourcesAsClient();
            else
              closeResourcesAsServer();

            return true;
              }
              catch (Exception ex)
              {
            onError(ex.Message);
            return false;
              }
        }
Exemplo n.º 11
0
 private WebSocket()
 {
     _compression = CompressionMethod.NONE;
       _cookies = new CookieCollection();
       _extensions = String.Empty;
       _forClose = new object();
       _forFrame = new object();
       _forSend = new object();
       _origin = String.Empty;
       _preAuth = false;
       _protocol = String.Empty;
       _readyState = WsState.CONNECTING;
 }
Exemplo n.º 12
0
        public WebSocket(string url, string protocol)
        {
            this.uri = new Uri(url);
              string scheme = uri.Scheme;

              if (scheme != "ws" && scheme != "wss")
              {
            throw new ArgumentException("Unsupported scheme: " + scheme);
              }

              this.readyState = WsState.CONNECTING;
              this.unTransmittedBuffer = new StringBuilder();
              this.bufferedAmount = 0;
              this.protocol = protocol;
        }
Exemplo n.º 13
0
        private void close(WsState state)
        {
            #if DEBUG
              Console.WriteLine("WS: Info @close: Current thread IsBackground: {0}", Thread.CurrentThread.IsBackground);
            #endif
              if (readyState == WsState.CLOSING ||
              readyState == WsState.CLOSED)
              {
            return;
              }

              readyState = state;

              if (wsStream != null)
              {
            wsStream.Close();
            wsStream = null;
              }

              if (tcpClient != null)
              {
            tcpClient.Close();
            tcpClient = null;
              }

              if (OnClose != null)
              {
            OnClose(this, EventArgs.Empty);
              }
            #if DEBUG
              Console.WriteLine("WS: Info @close: Exit close method.");
            #endif
        }
Exemplo n.º 14
0
 private WebSocket()
 {
     _cookies    = new CookieCollection();
       _extensions = String.Empty;
       _forClose   = new Object();
       _forSend    = new Object();
       _protocol   = String.Empty;
       _readyState = WsState.CONNECTING;
 }
Exemplo n.º 15
0
 private void onOpen()
 {
     _readyState = WsState.OPEN;
     startReceiving();
     OnOpen.Emit(this, EventArgs.Empty);
 }
Exemplo n.º 16
0
 private void onOpen()
 {
     _readyState = WsState.OPEN;
       startReceiving();
       OnOpen.Emit(this, EventArgs.Empty);
 }
Exemplo n.º 17
0
 // As server
 internal void Close(HttpStatusCode code)
 {
     _readyState = WsState.CLOSING;
       close (code);
 }
Exemplo n.º 18
0
        private void close(PayloadData data)
        {
            #if DEBUG
              Console.WriteLine("WS: Info@close: Current thread IsBackground?: {0}", Thread.CurrentThread.IsBackground);
              #endif
              lock(_forClose)
              {
            // Whether the closing handshake has been started already?
            if (_readyState == WsState.CLOSING || _readyState == WsState.CLOSED)
              return;

            // Whether the closing handshake on server is started before the connection has been established?
            if (_readyState == WsState.CONNECTING && !_client)
            {
              sendResponseHandshake(HttpStatusCode.BadRequest);
              onClose(new CloseEventArgs(data));

              return;
            }

            _readyState = WsState.CLOSING;
              }

              // Whether a payload data contains the close status code which must not be set for send?
              if (data.ContainsReservedCloseStatusCode)
              {
            onClose(new CloseEventArgs(data));
            return;
              }

              closeHandshake(data);
              #if DEBUG
              Console.WriteLine("WS: Info@close: Exit close method.");
              #endif
        }
Exemplo n.º 19
0
        private void close(CloseEventArgs eventArgs)
        {
            if (!Thread.CurrentThread.IsBackground && _exitReceiving != null)
            if (!_exitReceiving.WaitOne (5 * 1000))
              eventArgs.WasClean = false;

              if (!closeResources ())
            eventArgs.WasClean = false;

              _readyState = WsState.CLOSED;
              OnClose.Emit (this, eventArgs);
        }
Exemplo n.º 20
0
        private void close(PayloadData data)
        {
            _logger.Debug ("Is this thread background?: " + Thread.CurrentThread.IsBackground);
              CloseEventArgs args = null;
              lock (_forClose)
              {
            if (_readyState == WsState.CLOSING || _readyState == WsState.CLOSED)
              return;

            var state = _readyState;
            _readyState = WsState.CLOSING;
            args = new CloseEventArgs (data);
            if (state == WsState.CONNECTING)
            {
              if (!_client)
              {
            close (HttpStatusCode.BadRequest);
            return;
              }
            }
            else
            {
              if (!data.ContainsReservedCloseStatusCode)
            args.WasClean = send (createControlFrame (Opcode.CLOSE, data, _client));
            }
              }

              close (args);
              _logger.Trace ("Exit close method.");
        }
Exemplo n.º 21
0
 // As server
 private void close(HttpStatusCode code)
 {
     send (createHandshakeResponse (code));
       closeResources ();
       _readyState = WsState.CLOSED;
 }
Exemplo n.º 22
0
 private void onOpen()
 {
     _readyState = WsState.OPEN;
     startMessageLoop();
     OnOpen.Emit(this, EventArgs.Empty);
 }
Exemplo n.º 23
0
        private bool closeConnection()
        {
            _readyState = WsState.CLOSED;

            try
            {
                if (_httpContext != null)
                {
                    _httpContext.Response.Close();
                    _wsStream = null;
                    _httpContext = null;
                }

                if (_wsStream != null)
                {
                    _wsStream.Dispose();
                    _wsStream = null;
                }

                if (_tcpClient != null)
                {
                    _tcpClient.Close();
                    _tcpClient = null;
                }

                return true;
            }
            catch (Exception ex)
            {
                onError(ex.Message);
                return false;
            }
        }
Exemplo n.º 24
0
        private void InitFsm()
        {
            #region 资源初始化
            _preparingState = new WsState("preparing");
            //_preparingState.OnUpdate += InitGame;
            #endregion

            #region 洗牌中
            _shufflingState           = new WsState("shuffling");
            _shufflingState.OnEnter  += GetShufTime;
            _shufflingState.OnUpdate += Shuffle;
            _shufflingState.OnExit   += (IState state) => { _isShuffled = false; };

            _bettingState           = new WsState("betting");
            _bettingState.OnEnter  += Start1Round;
            _bettingState.OnUpdate += Bet;
            _bettingState.OnExit   += RoundOrSessionOver;

            _prepareShuffle = new WsTransition("preShuffle", _preparingState, _shufflingState);
            _prepareShuffle.OnTransition += InitGame;
            _prepareShuffle.OnCheck      += GameInited;
            _preparingState.AddTransition(_prepareShuffle);

            _shuffleBet          = new WsTransition("shuffleBet", _shufflingState, _bettingState);
            _shuffleBet.OnCheck += Shuffled;
            _shufflingState.AddTransition(_shuffleBet);
            #endregion


            #region 开始押注

            //开下一局
            _betNextBet          = new WsTransition("betNextBet", _bettingState, _bettingState);
            _betNextBet.OnCheck += CanGoNextBet;
            //_betNextBet.OnTransition += DealCard;	//开牌动画,结算筹码
            _bettingState.AddTransition(_betNextBet);

            //本场结束,验单,重新洗牌
            _betShuffle               = new WsTransition("betShuffle", _bettingState, _shufflingState);
            _betShuffle.OnCheck      += SessionOver;
            _betShuffle.OnTransition += ExamineWaybill;
            _bettingState.AddTransition(_betShuffle);
            #endregion

            #region 押注结束后亮牌结算
            //_accountingState = new WsState("accounting");
            //_accountingState.OnEnter += GetAccountTime;
            //_accountingState.OnUpdate += Account;
            //_accountingState.OnExit += (IState s) => { _isAccounted = false; };

            ////直接开下局押注
            //_accountBet = new WsTransition("accountBet", _accountingState, _bettingState);
            //_accountBet.OnCheck += RoundAccounted;
            //_accountingState.AddTransition(_accountBet);

            ////每场最后局结束,要重新洗牌
            //_accountShuffle = new WsTransition("accountShuffle", _accountingState, _shufflingState);
            //_accountShuffle.OnCheck += SessionAccounted;
            //_accountingState.AddTransition(_accountShuffle);
            #endregion

            _fsm = new WsStateMachine("baccarat", _preparingState);
            _fsm.AddState(_preparingState);
            _fsm.AddState(_shufflingState);
            _fsm.AddState(_bettingState);
            //_fsm.AddState(_accountingState);
            print("init fsm over");
        }
Exemplo n.º 25
0
        private void InitFsm()
        {
            _preparingState = new WsState("preparing");
            _shufflingState = new WsState("shuffling");
            _printingState  = new WsState("printing");
            _bettingState   = new WsState("betting");
            _dealingState   = new WsState("dealing");
            _examineState   = new WsState("examine");

            //初始化切换到洗牌
            _prepareShuffle          = new WsTransition("preShuffle", _preparingState, _shufflingState);
            _prepareShuffle.OnCheck += () => { return(true); };
            _preparingState.AddTransition(_prepareShuffle);

            //洗牌切换到打印
            _shufflingState.OnEnter  += OnShuffleEnter;
            _shufflingState.OnUpdate += OnShufflling;
            _shufflingState.OnExit   += OnShuffleExit;
            _shuffleBet          = new WsTransition("shufflePrint", _shufflingState, _printingState);
            _shuffleBet.OnCheck += IsShuffled;
            _shufflingState.AddTransition(_shuffleBet);

            //打印切换到押注
            _printingState.OnEnter  += _printingState_OnEnter;
            _printingState.OnUpdate += _printingState_OnUpdate;
            _printingState.OnExit   += _printingState_OnExit;
            _printBet          = new WsTransition("printBet", _printingState, _bettingState);
            _printBet.OnCheck += () => { return(_isPrinted); };
            _printingState.AddTransition(_printBet);

            //押注切换到开牌
            _bettingState.OnEnter  += OnBetEnter;
            _bettingState.OnUpdate += Betting;
            _bettingState.OnExit   += OnBetExit;
            _betDeal          = new WsTransition("betDeal", _bettingState, _dealingState);
            _betDeal.OnCheck += IsBetted;
            _bettingState.AddTransition(_betDeal);

            //开牌后切换到下一轮
            _dealingState.OnEnter     += OnDealEnter;
            _dealingState.OnUpdate    += Dealing;
            _dealingState.OnExit      += OnDealExit;
            _dealNextBet               = new WsTransition("dealNextBet", _dealingState, _bettingState);
            _dealNextBet.OnCheck      += CanGoNextBet;
            _dealNextBet.OnTransition += GotoNextBet;

            //开牌后本局结束,验单,重新洗牌,切换到下一局
            _dealExamine          = new WsTransition("dealExamine", _dealingState, _examineState);
            _dealExamine.OnCheck += IsSessionOver;
            _dealingState.AddTransition(_dealNextBet);
            _dealingState.AddTransition(_dealExamine);

            //验单
            _examineState.OnEnter   += OnExamineEnter;
            _examineState.OnUpdate  += OnExamining;
            _examineState.OnExit    += OnExamineExit;
            _examineShuffle          = new WsTransition("examineShuffle", _examineState, _shufflingState);
            _examineShuffle.OnCheck += IsExamineOver;
            _examineState.AddTransition(_examineShuffle);

            _fsm = new WsStateMachine("baccarat", _preparingState);
            _fsm.AddState(_preparingState);
            _fsm.AddState(_shufflingState);
            _fsm.AddState(_bettingState);
            _fsm.AddState(_dealingState);
            _fsm.AddState(_examineState);
        }