예제 #1
0
    private void OnNetWorkStateChange(NetWorkState state)
    {
        Debug.Log("GameServerNetState------------------>" + state.ToString());
        switch (state)
        {
        case NetWorkState.CLOSED:
            break;

        case NetWorkState.CONNECTING:
            break;

        case NetWorkState.CONNECTED:
            break;

        case NetWorkState.TIMEOUT:
            ChangeConnectStatus(ConnectStatus.TIMEOUT);
            break;

        case NetWorkState.DISCONNECTED:
            ChangeConnectStatus(ConnectStatus.DISCONNECT);
            break;

        case NetWorkState.ERROR:
            ChangeConnectStatus(ConnectStatus.ERROR);
            break;

        default:
            break;
        }
    }
예제 #2
0
 // 网络状态变化
 private void NetWorkChanged(NetWorkState state)
 {
     if (this.NetWorkStateChangedEvent != null)
     {
         this.NetWorkStateChangedEvent(state);
     }
 }
예제 #3
0
 public LoginClient()
 {
     _disposed     = false;
     _netWorkState = NetWorkState.Closed;
     _timeoutMSec  = 8000;
     _timeoutEvent = new ManualResetEvent(false);
 }
예제 #4
0
    /// <summary>
    /// Game Server network state event.
    /// </summary>
    /// <returns>The network state event.</returns>
    /// <param name="state">State.</param>
    void GSNetWorkStateEvent(NetWorkState state)
    {
        Debug.Log("================");
        if (state == NetWorkState.CONNECTED)
        {
            Debug.Log("connected");
        }

        if (state == NetWorkState.TIMEOUT)
        {
            Debug.Log("TIMEOUT");
        }

        if (state == NetWorkState.ERROR_CONNECT)
        {
            Debug.Log("ERROR_CONNECT");
        }

        if (state == NetWorkState.DISCONNECTED)
        {
            Debug.Log("disconnected");
            var client = GameClient.Instance;
            client.MahjongGamePlayer.ConnectGameServer("login.dv.7pmigame.com", Port, delegate() {
                client.MahjongGamePlayer.StartAuth(GameClient.Instance.UserId);
            });
        }

        if (state == NetWorkState.CLOSED)
        {
            Debug.Log("closed");
        }
    }
예제 #5
0
        public PomeloClient()
        {
            netWorkState  = NetWorkState.DISCONNECTED;
            _eventManager = new EventManager();

            __receiveMsgQueue = new Queue <Message>();
        }
예제 #6
0
 //   public static
 public Network()
 {
     client   = new PomeloClient();
     requests = new Queue <RequestMessage>();
     client.NetWorkStateChangedEvent += NetWorkStateChange;
     netWorkState = NetWorkState.CLOSED;
 }
예제 #7
0
        ///--------------------
        protected void _onConnectCallback(IAsyncResult result)
        {
            try
            {
                this._socket.EndConnect(result);
                netWorkState = NetWorkState.CONNECTED;

                this._protocol = new Protocol(this, this._socket);

                Message msg = new Message(MessageType.MSG_SYS, SYS_MSG_CONNECTED);
                __receiveMsgQueue.Enqueue(msg);
            }
            catch (SocketException e)
            {
                Message msg = new Message(MessageType.MSG_SYS, SYS_MSG_CONNECT_FAILED);
                msg.jsonObj = new JsonObject();
                msg.jsonObj.Add("reason", e.Message);

                __receiveMsgQueue.Enqueue(msg);

                netWorkState = NetWorkState.DISCONNECTED;

                _socket.Close();
                _socket = null;
            }
        }
예제 #8
0
        public void Disconnect()
        {
            if (netWorkState == NetWorkState.DISCONNECTED)
            {
                return;
            }

            /// Force update to make sure all received messages been dispatched.
            Update();

            // free managed resources
            if (_protocol != null)
            {
                _protocol.close();
            }

            try
            {
                this._socket.Shutdown(SocketShutdown.Both);
                this._socket.Close();
                this._socket = null;
            }
            catch (Exception e)
            {
                _onError(e.Message);
            }

            netWorkState = NetWorkState.DISCONNECTED;

            _eventManager.ClearCallBackMap();
            _eventManager.ClearCallBackMap();

            _reqId = 100;
        }
예제 #9
0
        void OnTransportStateChange()
        {
            NetWorkState state = this.transport.NetworkState();

            switch (state)
            {
            case NetWorkState.CLOSED:
                bDisconnCallBack = true;
                break;

            case NetWorkState.CONNECTING:
                break;

            case NetWorkState.CONNECTED:
                bConnectCallBack = true;
                break;

            case NetWorkState.DISCONNECTED:
                bDisconnCallBack = true;
                break;

            case NetWorkState.TIMEOUT:
                bDisconnCallBack = true;
                break;

            case NetWorkState.ERROR:
                bDisconnCallBack = true;
                break;

            default:
                break;
            }
        }
예제 #10
0
    private void OnGateServerNetWorkStateChange(NetWorkState state)
    {
        Debug.Log("GateServerNetState------------------>" + state.ToString());
        switch (state)
        {
        case NetWorkState.CLOSED:
            break;

        case NetWorkState.CONNECTING:

            break;

        case NetWorkState.CONNECTED:
            break;

        case NetWorkState.DISCONNECTED:
            if (!activeCloseConnect)
            {
                ChangeConnectStatus(ConnectStatus.ERROR);
            }
            break;

        case NetWorkState.TIMEOUT:
            ChangeConnectStatus(ConnectStatus.TIMEOUT);
            break;

        case NetWorkState.ERROR:
            ChangeConnectStatus(ConnectStatus.ERROR);
            break;
        }
    }
예제 #11
0
        public Connection()
        {
            netWorkState  = NetWorkState.DISCONNECTED;
            _eventManager = new EventManager();

            __receiveMsgQueue = new Queue <Message>();
        }
예제 #12
0
        private void NetWorkStateCallBack(NetWorkState state)
        {
            SkynetLogger.Info(Channel.NetDevice, "Gate WS NetWorkStateCallBack:" + state);
            if (state == NetWorkState.Connected)
            {
                //TODO:发送 与 gate 握手消息成功后 开启 心跳操作
                SpObject handshakeRequset = new SpObject();
                handshakeRequset.Insert("uid", _req.uid);
                handshakeRequset.Insert("secret", _req.secret);
                handshakeRequset.Insert("subid", _req.subid);

                _client.Request("handshake", handshakeRequset, (SpObject obj) =>
                {
                    {
                        int role = obj["role"].AsInt();
                        if (role == 0)
                        {
                            SpObject bornRequest = new SpObject();
                            bornRequest.Insert("name", "helloworld");
                            bornRequest.Insert("head", "1111111111");
                            bornRequest.Insert("job", "1");
                            _client.Request("born", bornRequest,
                                            (SpObject bornObj) => { SkynetLogger.Info(Channel.NetDevice, "born resp is ok"); });
                        }
                        else
                        {
                            SkynetLogger.Info(Channel.NetDevice, "is has role");
                        }
                    }
                });
            }
        }
예제 #13
0
        public void Disconnect()
        {
            if (netWorkState == NetWorkState.DISCONNECTED)
            {
                return;
            }

            /// Force update to make sure all received messages been dispatched.
            Update();

            // free managed resources
            if (_protocol != null)
            {
                _protocol.close();
            }

            try
            {
                _socket.Shutdown(SocketShutdown.Both);
                _socket.Close();
                _socket = null;
            }
            catch (Exception)
            {
                //todo : 有待确定这里是否会出现异常,这里是参考之前官方github上pull request。emptyMsg
            }

            netWorkState = NetWorkState.DISCONNECTED;

            _eventManager.ClearCallBackMap();
            _eventManager.ClearCallBackMap();

            _reqId = 100;
        }
예제 #14
0
    public Task <bool> ConnectAsync(string address, int port)
    {
        if (state == NetWorkState.CONNECTED || state == NetWorkState.CONNECTING)
        {
            return(Task.FromResult(true));
        }
        this.address = address;
        this.port    = port;
        Task <bool> ret = Task.Run(async() =>
        {
            IPAddress[] iPHostEntry = await Dns.GetHostAddressesAsync(address);
            client = new RpcClient(new IPEndPoint(iPHostEntry[0], port));
            state  = NetWorkState.CONNECTING;
            await client.ConnectAsync();
            Auth             = client.Import <SimCivil.Contract.IAuth>();
            RoleManager      = client.Import <SimCivil.Contract.IRoleManager>();
            ViewSynchronizer = client.Import <SimCivil.Contract.IViewSynchronizer>();
            PlayerContorller = client.Import <SimCivil.Contract.IPlayerController>();
            return(client.Connected);
        });

        ret.ContinueWith((ret2) =>
        {
            if (ret2.Result == true)
            {
                state = NetWorkState.CONNECTED;
            }
            else
            {
                state = NetWorkState.DISCONNECT;
            }
        });
        return(ret);
    }
예제 #15
0
 public static void LReconnect(string url, int port, int playerid, string route)
 {
     PomeloSocketEntry.Client = new PomeloClient();
     PomeloSocketEntry.Client.NetWorkStateChangedEvent += (NWorkStateChangedEvent)(state =>
     {
         PomeloSocketEntry.networkState = state;
         Debug.Log(PomeloSocketEntry.networkState);
         if (PomeloSocketEntry.NwStateChangeDel == null)
         {
             return;
         }
         PomeloSocketEntry.NwStateChangeDel((int)PomeloSocketEntry.networkState);
     });
     PomeloSocketEntry.Client.initClient(url, port, (Action)(() =>
     {
         JsonObject user = new JsonObject();
         PomeloSocketEntry.Client.connect(user, (Action <JsonObject>)(responseData =>
         {
             Debug.Log(responseData + "handshake call back data");
             if (NetWorkState.CONNECTED != PomeloSocketEntry.networkState)
             {
                 return;
             }
             PomeloSocketEntry.LSocketRequest(new JsonObject()
             {
                 {
                     "playerId",
                     (object)playerid
                 }
             }, route);
         }));
     }));
 }
예제 #16
0
 private void networkSatateChanged(NetWorkState state)
 {
     this.state = state;
     if (NetworkChangedDelegate != null)
     {
         NetworkChangedDelegate(this.state);
     }
 }
예제 #17
0
        void OnClose(object sender, EventArgs e)
        {
            CloseEventArgs ce = e as CloseEventArgs;

            netWorkState = NetWorkState.CLOSING;
            _disReason   = ce.Code;
            _err         = ce.Reason;
        }
예제 #18
0
 protected void NetWorkChanged(NetWorkState state)
 {
     netWorkState = state;
     if (NetWorkStateChangedEvent != null)
     {
         NetWorkStateChangedEvent(state);
     }
 }
예제 #19
0
        private void UpdateInternal(float deltaTime)
        {
            //先处理网络消息
            HandleNetMsgUpdate();
            //
            if (_isWorking == false)
            {
                return;
            }

            //首次连接时候判断网络状态
            if ((_curNetworkState == NetWorkState.CLOSED ||
                 _curNetworkState == NetWorkState.ERROR ||
                 _curNetworkState == NetWorkState.TIMEOUT ||
                 _curNetworkState == NetWorkState.DISCONNECTED) &&
                (_workState != WorkState.ConnectGameSvrSuccessed) && (CurSessionState == SessionState.SessionStart))
            {
                JW.Common.Log.LogD("Pomelo 事务错误");
                //关闭网络
                CloseNet();

                CurSessionState = SessionState.SessionError;
                //回位
                _workState       = WorkState.ConnectGateState;
                _curNetworkState = NetWorkState.CONNECTING;
                return;
            }

            //正常情况判断网络状态
            if ((_curNetworkState == NetWorkState.CLOSED ||
                 _curNetworkState == NetWorkState.ERROR ||
                 _curNetworkState == NetWorkState.TIMEOUT ||
                 _curNetworkState == NetWorkState.DISCONNECTED) &&
                (_workState == WorkState.ConnectGameSvrSuccessed) && (CurSessionState == SessionState.SessionSuccessed)
                )
            {
                //网络断开 等待重试阶段
                JW.Common.Log.LogD("Pomelo 网络错误:准备重试连接");
                Change2Retry();
                return;
            }
            //
            //重试情况判断网络状态
            if ((_curNetworkState == NetWorkState.CLOSED ||
                 _curNetworkState == NetWorkState.ERROR ||
                 _curNetworkState == NetWorkState.TIMEOUT ||
                 _curNetworkState == NetWorkState.DISCONNECTED) &&
                (_workState != WorkState.ConnectGameSvrSuccessed) && (CurSessionState == SessionState.SessionRetry)
                )
            {
                //网络断开 等待重试阶段
                JW.Common.Log.LogD("Pomelo 网络错误:继续重试连接");
                Change2Retry();
                return;
            }
            //
            HandleSendMsgUpdate();
        }
예제 #20
0
        private void NetWorkChanged(NetWorkState state)
        {
            _netWorkState = state;

            if (NetWorkStateChangedEvent != null)
            {
                NetWorkStateChangedEvent(state);
            }
        }
예제 #21
0
 private void NetWorkChanged(NetWorkState state)
 {
     this.netWorkState = state;
     if (this.NetWorkStateChangedEvent == null)
     {
         return;
     }
     this.NetWorkStateChangedEvent(state);
 }
예제 #22
0
 void HandleClosing()
 {
     _handler.OnDisconnect(_disReason, _err);
     netWorkState   = NetWorkState.CLOSED;
     _tryDisconnect = 0;
     if (!this.disposed)
     {
         Dispose();
     }
 }
예제 #23
0
        private void NetWorkChanged(NetWorkState state)
        {
            NetWorkState old = netWorkState;

            netWorkState = state;

            if (old != state && NetWorkStateChangedEvent != null)
            {
                NetWorkStateChangedEvent(state);
            }
        }
예제 #24
0
        public void Connect()
        {
            if (_handler == null || _protocol == null || _mb == null)
            {
                netWorkState = NetWorkState.CLOSED;
                throw new Exception("handler null");
            }

            if (netWorkState != NetWorkState.CLOSED)
            {
                throw new Exception("state err" + netWorkState.ToString());
            }

            _startConnectTime = Time.realtimeSinceStartup;
            netWorkState      = NetWorkState.CONNECTING;
            _mb.StartCoroutine(_AutoUpdate());

            IPAddress ipAddress = null;

            try
            {
                // this implement quickly
                IPAddress[] ips;
                ips = Dns.GetHostAddresses(_host);
                foreach (IPAddress ipa in ips)
                {
                    if (ipa.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        ipAddress = ipa;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                OnDisconnect(1, e.ToString());
                return;
            }

            if (ipAddress == null)
            {
                OnDisconnect(2, "can not parse host : " + _host);
                return;
            }

            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ie = new IPEndPoint(ipAddress, _port);

            socket.BeginConnect(ie, new AsyncCallback(Callback_ConnectOk), this.socket);
            _resvMsgQueue.Clear();
            _sendQueue.Clear();
            _tryDisconnect = 0;
        }
        protected void NetWorkChanged(NetWorkState state)
        {
            if (netWorkState != state)
            {
                netWorkState = state;

                if (networkEventCB != null)
                {
                    networkEventCB();
                }
            }
        }
예제 #26
0
 private void OnDisconnect(int reason, string str)
 {
     if (netWorkState != NetWorkState.CLOSING)
     {
         netWorkState = NetWorkState.CLOSING;
         _disReason   = reason;
         _err         = str;
     }
     else
     {
         ZLog.E(_mb, "OnDisconnect state err {0}", netWorkState);
     }
 }
예제 #27
0
        internal void _onDisconnect(string reason)
        {
            netWorkState = NetWorkState.DISCONNECTED;

            JsonObject jsonObj = new JsonObject();

            jsonObj.Add("reason", reason);
            Message msg = new Message(MessageType.MSG_SYS, DisconnectEvent, jsonObj);

            __receiveMsgQueue.Enqueue(msg);

            _socket.Close();
            _socket = null;
        }
예제 #28
0
        private void Change2Retry()
        {
            CurSessionState  = SessionState.SessionRetry;
            _curNetworkState = NetWorkState.CONNECTING;
            _workState       = WorkState.ConnectGateState;

            if (string.IsNullOrEmpty(_gameSvrIp))
            {
                _workState = WorkState.ConnectGateState;
            }
            else
            {
                _workState = WorkState.ConnectGameSvrState;
            }
        }
예제 #29
0
 public void Callback_ConnectOk(IAsyncResult result)
 {
     try
     {
         this.socket.EndConnect(result);
         netWorkState = NetWorkState.CONNECTED;
     }
     catch (SocketException e)
     {
         OnDisconnect(3, e.ToString());
         Dispose();
     }
     finally
     {
     }
 }
예제 #30
0
        /// 初始化连接
        public void Connect(string host, int port, Action <Message> callback)
        {
            Assert(netWorkState == NetWorkState.DISCONNECTED);

            UnityEngine.Debug.Log("Connect to " + host + " with port " + port);

            netWorkState = NetWorkState.CONNECTING;

            IPAddress ipAddress = null;

            try
            {
                if (!IPAddress.TryParse(host, out ipAddress))
                {
                    IPAddress[] addresses = Dns.GetHostEntry(host).AddressList;
                    foreach (var item in addresses)
                    {
                        if (item.AddressFamily == AddressFamily.InterNetwork)
                        {
                            ipAddress = item;
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _onDisconnect(e.Message);
                return;
            }

            if (ipAddress == null)
            {
                throw new Exception("Cannot parse host : " + host);
            }

            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ie = new IPEndPoint(ipAddress, port);

            _eventManager.RemoveCallback(SYS_MSG_CONNECTED);
            _eventManager.RemoveCallback(SYS_MSG_CONNECT_FAILED);

            _eventManager.AddCallback(SYS_MSG_CONNECTED, callback);
            _eventManager.AddCallback(SYS_MSG_CONNECT_FAILED, callback);

            _socket.BeginConnect(ie, _onConnectCallback, this._socket);
        }
예제 #31
0
        /// <summary>
        /// 网络状态变化
        /// </summary>
        /// <param name="state"></param>
        private void NetWorkChanged(NetWorkState state)
        {
            netWorkState = state;

            if (NetWorkStateChangedEvent != null)
            {
                NetWorkStateChangedEvent(state);
            }
        }