Esempio n. 1
0
        public void ProcessMessage(SpRpcResult msg)
        {
            if (msg.ud != 0)
            {
                SkynetLogger.Error(Channel.NetDevice, "resp error code is: " + msg.ud);
                _eventManager.RemoveCallBack(msg.Session);
                return;
            }

            switch (msg.Op)
            {
            case SpRpcOp.Request:
                SkynetLogger.Info(Channel.NetDevice, "Recv Request : " + msg.Protocol.Name + ", session : " + msg.Session);
                Utils.Util.DumpObject(msg.Data);

                _eventManager.InvokeOnEvent(msg.Protocol.Name, msg.Data);
                break;

            case SpRpcOp.Response:
                if (msg.Protocol.Name != "heartbeat")
                {
                    SkynetLogger.Info(Channel.NetDevice, "Recv Response : " + msg.Protocol.Name + ", session : " + msg.Session);
                    Utils.Util.DumpObject(msg.Data);
                }

                _eventManager.InvokeCallBack(msg.Session, msg.Data);
                break;

            case SpRpcOp.Unknown:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 2
0
        public void ConfirmCommand(int confirmingPlayerId, int currentLockStepTurn, int confirmedCommandLockStepTurn)
        {
            if (confirmedCommandLockStepTurn == currentLockStepTurn)
            {
                //if current turn, add to the current Turn Confirmation
                _confirmedCurrent[confirmingPlayerId] = true;
                _confirmedCurrentCount++;

                //if we recieved the last confirmation, stop timer
                //this gives us the length of the longest roundtrip message
                if (_confirmedCurrentCount == _lockStepManager.NumberOfPlayers)
                {
                    _currentSw.Stop();
                }
            }
            else if (confirmedCommandLockStepTurn == (currentLockStepTurn - 1))
            {
                //if confirmation for prior turn, add to the prior turn confirmation
                _confirmedPrior[confirmingPlayerId] = true;
                _confirmedPriorCount++;
                //if we recieved the last confirmation, stop timer
                //this gives us the length of the longest roundtrip message
                if (_confirmedPriorCount == _lockStepManager.NumberOfPlayers)
                {
                    _priorSw.Stop();
                }
            }
            else
            {
                SkynetLogger.Error(Channel.LockStep, "Unexpected lockstepID Confirmed : " + confirmedCommandLockStepTurn + " from player: " + confirmingPlayerId);
            }
        }
Esempio n. 3
0
    private void NetWorkStateCallBack(NetWorkState state)
    {
        SkynetLogger.Info(Channel.NetDevice, "Gate Tcp NetWorkStateCallBack:" + state);
        if (state != NetWorkState.Connected)
        {
            return;
        }
        //TODO:发送 与 gate 握手消息成功后 开启 心跳操作
        var handshakeRequset = new SpObject();

        handshakeRequset.Insert("uid", _req.uid);
        handshakeRequset.Insert("secret", _req.secret);
        handshakeRequset.Insert("subid", _req.subid);

        _client.Request("handshake", handshakeRequset, (SpObject obj) =>
        {
            var 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.Error(Channel.NetDevice, "born resp is ok");
                });
            }
            else
            {
                SkynetLogger.Info(Channel.NetDevice, "is has role");
            }
        });
    }
Esempio n. 4
0
    public void Start()
    {
        SkynetLogger.Error(Channel.NetDevice, "++++++Skynet Client Start++++");

        _login = new TestLoginTcp(Protocol);
        _login.Run(ProcessLoginResp);
    }
Esempio n. 5
0
    private void OnDestroy()
    {
        if (_login != null)
        {
            _login.DisConnect();
            _login = null;
        }

        if (_gateTcp != null)
        {
            _gateTcp.DisConnect();
            _gateTcp = null;
        }

        if (_gateWs != null)
        {
            _gateWs.DisConnect();
            _gateWs = null;
        }

        if (_gateUdp != null)
        {
            _gateUdp.DisConnect();
            _gateUdp = null;
        }

        SkynetLogger.Error(Channel.NetDevice, "++++++Skynet Client Destroy++++");
    }
Esempio n. 6
0
        private void Message(object sender, MessageEventArgs e)
        {
            switch (e.Opcode)
            {
            case Opcode.Text:
                _protocol.ProcessMessage(e.Data);
                break;

            case Opcode.Binary:
                _protocol.ProcessMessage(e.RawData);
                break;

            case Opcode.Close:
                SkynetLogger.Error(Channel.NetDevice, "[OnMessage] OpCode Closed");
                break;

            case Opcode.Cont:
                break;

            case Opcode.Ping:
                break;

            case Opcode.Pong:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 7
0
        //private static void ConfigureCertificateValidatation(
        //    bool validateCertificates,
        //    out System.Net.SecurityProtocolType protocolType,
        //    out RemoteCertificateValidationCallback prevValidator)
        //   {
        //       prevValidator = null;
        //       protocolType = (System.Net.SecurityProtocolType)0;

        //       if (!validateCertificates)
        //       {
        //           protocolType = System.Net.ServicePointManager.SecurityProtocol;
        //           System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
        //           prevValidator = System.Net.ServicePointManager.ServerCertificateValidationCallback;
        //           System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
        //       }
        //   }

        public static void DoLoginReqAction(AuthPackageReq cmd, LoginAuthRespCallBack cb)
        {
            respcb = cb;
            LoginwReqPack req = new LoginwReqPack();

            req.parms = JsonMapper.ToJson(cmd);
            string jsonStr = JsonMapper.ToJson(req);

            IHttpContent content = new StringContent(jsonStr, System.Text.Encoding.UTF8, "application/json");

            client.Post(new Uri(url), content, HttpCompletionOption.AllResponseContent, r =>
            {
                if (r.IsSuccessStatusCode)
                {
                    var respStr = r.ReadAsString();
                    try
                    {
                        AuthPackageResp resp = JsonMapper.ToObject <AuthPackageResp>(respStr);
                        respcb(resp);
                    }
                    catch (Exception e)
                    {
                        SkynetLogger.Error(Channel.NetDevice, "Json Deserialize err:" + e.Message.ToString());
                    }
                }
            });
        }
Esempio n. 8
0
        public void ProcessMessage(SpRpcResult msg)
        {
            if (msg.ud != 0)
            {
                SkynetLogger.Error(Channel.Udp, "udp client resp error code is: " + msg.ud);
                return;
            }

            switch (msg.Op)
            {
            case SpRpcOp.Request:
                SkynetLogger.Info(Channel.Udp, "udp client recv Request : " + msg.Protocol.Name + ", session : " + msg.Session);
                Utils.Util.DumpObject(msg.Data);
                break;

            case SpRpcOp.Response:
                if (msg.Protocol.Name != "heartbeat")
                {
                    SkynetLogger.Info(Channel.Udp, "udp client recv Response : " + msg.Protocol.Name + ", session : " + msg.Session);
                    Utils.Util.DumpObject(msg.Data);
                }
                break;

            case SpRpcOp.Unknown:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 9
0
        private void EndReceive(IAsyncResult asr)
        {
            if (_transportState == TransportState.Closed)
            {
                return;
            }
            var state = (StateObject)asr.AsyncState;

            try
            {
                var length = _socket.EndReceive(asr);
                if (length > 0)
                {
                    ProcessBytes(state.buffer, 0, length);
                    Receive();
                }
                else
                {
                    SkynetLogger.Error(Channel.NetDevice, "没有接收到任何数据 远端连接断开");

                    onDisconnect?.Invoke();
                }
            }
            catch (SocketException e)
            {
                onDisconnect?.Invoke();

                SkynetLogger.Error(Channel.NetDevice, "Socket Exception连接断开:" + e.Message);
            }
        }
Esempio n. 10
0
 public void RawSend(byte[] data, int length)
 {
     if (length >= 512)
     {
         SkynetLogger.Error(Channel.Udp, "Send Data overload 1 MTU");
     }
     _udpClient.SendAsync(data, length);
 }
Esempio n. 11
0
        private void Close(object sender, CloseEventArgs e)
        {
            var code = (CloseStatusCode)e.Code;

            if (code != CloseStatusCode.NoStatus && code != CloseStatusCode.Normal)
            {
                SkynetLogger.Error(Channel.NetDevice, "[ERROR] " + e.Reason + " " + e.Code);
            }
            else
            {
                SkynetLogger.Error(Channel.NetDevice, "[INFO] Closed");
            }
            NetWorkChanged(NetWorkState.Error);
        }
Esempio n. 12
0
        private void Connect(IAsyncResult asr)
        {
            try
            {
                _socket.EndConnect(asr);
                _protocol = new Protocol(this, this._socket);
                NetWorkChanged(NetWorkState.Connected);
            }
            catch (Exception e)
            {
                SkynetLogger.Error(Channel.NetDevice, $"连接服务器异步结果错误:{e.Message.ToString()}");

                NetWorkChanged(NetWorkState.Error);
                Dispose();
            }
        }
Esempio n. 13
0
 public void AddCommand(Command cmd, int playerId, int currentLockStepTurn, int cmdsLockStepTurn)
 {
     //add cmd for processing later
     if (cmdsLockStepTurn == currentLockStepTurn + 1)
     {
         //if action is for next turn, add for processing 3 turns away
         if (_nextNextNextCommands[playerId] != null)
         {
             //TODO: Error Handling
             SkynetLogger.Error(Channel.LockStep, "Recieved multiple actions for player " + playerId + " for turn " + cmdsLockStepTurn);
         }
         _nextNextNextCommands[playerId] = cmd;
         _nextNextNextCommandsCount++;
     }
     else if (cmdsLockStepTurn == currentLockStepTurn)
     {
         //if recieved action during our current turn
         //add for processing 2 turns away
         if (_nextNextCommands[playerId] != null)
         {
             //TODO: Error Handling
             SkynetLogger.Error(Channel.LockStep, "Recieved multiple actions for player " + playerId + " for turn " + cmdsLockStepTurn);
         }
         _nextNextCommands[playerId] = cmd;
         _nextNextCommandsCount++;
     }
     else if (cmdsLockStepTurn == currentLockStepTurn - 1)
     {
         //if recieved action for last turn
         //add for processing 1 turn away
         if (_nextCommands[playerId] != null)
         {
             //TODO: Error Handling
             SkynetLogger.Error(Channel.LockStep, "Recieved multiple actions for player " + playerId + " for turn " + cmdsLockStepTurn);
         }
         _nextCommands[playerId] = cmd;
         _nextCommandsCount++;
     }
     else
     {
         //TODO: Error Handling
         SkynetLogger.Error(Channel.LockStep, " Unexpected lockstepID recieved : " + cmdsLockStepTurn);
     }
 }
Esempio n. 14
0
        public void Connect(string host, int port)
        {
            NetWorkChanged(NetWorkState.Connecting);
            IPAddress ipAddress = null;

            try
            {
                var addresses = Dns.GetHostEntry(host).AddressList;
                foreach (var item in addresses)
                {
                    if (item.AddressFamily != AddressFamily.InterNetwork)
                    {
                        continue;
                    }
                    ipAddress = item;
                    break;
                }
            }
            catch (Exception)
            {
                NetWorkChanged(NetWorkState.Error);
                return;
            }

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

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

                _socket.BeginConnect(ie, Connect, null);
            }
            catch (Exception e)
            {
                SkynetLogger.Error(Channel.NetDevice, $"连接服务器失败:{e.Message.ToString()}");
            }
        }
Esempio n. 15
0
 private void Error(object sender, ErrorEventArgs e)
 {
     SkynetLogger.Error(Channel.NetDevice, "WebSocket Has Error" + e.Message);
     NetWorkChanged(NetWorkState.Error);
 }