private void Reconnect() { if (!_reconnectable) { return; } _reconnect = true; _reconnectable = false; lock (_sendQueue) { while (_sendQueue.Count > 0) { ProtocolItem protocolItem = _sendQueue.Dequeue(); if (protocolItem.GetType() != typeof(Logic.Protocol.Model.ClientActiveReq)) { _reConnSendQueue.Enqueue(protocolItem); } } } if (_reConnSendQueue.Count > 0)//有协议需要发送才计算次数 { _reConnCount++; Debugger.Log(string.Format("第{0}次尝试重新连接服务器", _reConnCount)); } Connect(_host, _port); //Logic.UI.Mask.Contorller.MaskController.instance.ShowMask(); }
public void SendProtocolByte(int protocolId, byte[] bytes) { ProtocolItem tProtocolItem = new ProtocolItem(protocolId, bytes); lock (_sendQueue) { _sendQueue.Enqueue(tProtocolItem); } _waitSend.Set();//启动被ManualResetEvent阻塞的线程 }
public void SendProtocol(int protocolId, ProtoBuf.IExtensible iProtocol) { ProtocolItem tProtocolItem = new ProtocolItem(protocolId, iProtocol); lock (_sendQueue) { _sendQueue.Enqueue(tProtocolItem); } _waitSend.Set();//启动被ManualResetEvent阻塞的线程 }
private void SendThread() { try { while (true) { if (!Connected) { _sendThread = null; break; } _waitSend.WaitOne(); //阻塞当前线程 _waitSend.Reset(); //线程只处理一次,立即阻塞 lock (_sendQueue) { while (_sendQueue.Count > 0) { _sendProtocolItem = _sendQueue.Peek(); _bytes = _sendProtocolItem.ProtocolItemBytes; #if blowfish _mbw.Clear(); _bytes = _encryptBF.Encrypt_ECB(_bytes); _mbw.Write(_bytes.Length + ProtocolConf.PACKGE_LEN); _mbw.Write(_bytes); _bytes = _mbw.ToArray(); #endif int result = 0; int byteSize = _bytes.Length; while (result != byteSize) { result += _socket.Send(_bytes, result, byteSize - result, SocketFlags.None); } _sendQueue.Dequeue(); Debugger.Log("给服务器发送数据:" + result + ", 协议体:" + _sendProtocolItem.ToString()); } } } } catch (SocketException se) { Debugger.LogError("MSocketException" + se); } catch (Exception e) { Debugger.LogError("MSocketException" + e); } }
private void ParseProtocol() { if (!Connected) { return; } _publicInt = _socket.Receive(_recvBuff, SocketFlags.None); if (_publicInt <= 0) { _gameServerErr = true; return; } _recvStream.Position = _recvStream.Length; _recvStream.Write(_recvBuff, 0, _publicInt); _recvStream.Position = _recvStreamPos; byte[] bytes = new byte[ProtocolConf.PACKGE_LEN]; while (_recvStream.Length - _recvStream.Position >= ProtocolConf.PACKGE_LEN) { //long t1 = Common.Util.TimeUtil.GetTimeStamp(); _recvStreamPos = _recvStream.Position; _recvStream.Read(bytes, 0, ProtocolConf.PACKGE_LEN); _mbr = new MBinaryReader(bytes); _publicInt = _mbr.ReadInt32(); if (_recvStream.Length - _recvStream.Position >= _publicInt) { _recvStreamPos = 0; bytes = new byte[_publicInt]; _recvStream.Read(bytes, 0, _publicInt); if (Game.GameConfig.instance.encrypt) { byte[] decryptBytes = Common.Util.EncryptUtil.AESDecryptBytes(bytes, Game.GameConfig.instance.aesEncryptKey); _publicInt = decryptBytes.Length; _mbr = new MBinaryReader(decryptBytes); } else { _mbr = new MBinaryReader(bytes); } ProtocolProxy.instance.Version = _mbr.ReadInt32(); ProtocolProxy.instance.UserID0 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID1 = _mbr.ReadInt32(); _userID2 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID3 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID4 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID5 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID6 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID7 = _mbr.ReadInt32(); int protocolId = _mbr.ReadInt32(); _publicInt -= ProtocolConf.PROTOCOL_HEAD_LENGTH; byte[] bytes2 = new byte[_publicInt]; _mbr.Read(bytes2, 0, _publicInt); if (ProtocolConf.instance.ExistInLuaProcotols(protocolId)) { if (_recvQueue != null) { lock (_recvQueue) { _recvQueue.Enqueue(new ProtocolItem() { ProtocolId = protocolId, ProtocolBytes = bytes2 }); } } } else { _protocolItem = new ProtocolItem(); _protocolItem.ProtocolId = protocolId; using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes2)) { System.Type t = ProtocolProxy.instance.IsLoginServer ? ProtocolConf.GetLoginServerTypeByID(_protocolItem.ProtocolId) : ProtocolConf.GetTypeByID(_protocolItem.ProtocolId); if (t == null) { Debugger.LogError("未配置ID为" + _protocolItem.ProtocolId + "的协议 , 不含包头长:" + bytes.Length + "字节"); } else { if (ProtocolProxy.instance.IsLoginServer) { _protocolItem.Protocol = Activator.CreateInstance(t) as IProtocol; (_protocolItem.Protocol as IProtocol).FromBytes(new MBinaryReader(ms)); } else { if (t != typeof(EmptyMessage)) { _protocolItem.Protocol = ProtoBuf.Serializer.NonGeneric.Deserialize(t, ms) as ProtoBuf.IExtensible; } } } } if (_recvQueue != null) { lock (_recvQueue) { _recvQueue.Enqueue(_protocolItem); } } } } else { _recvStream.Position = _recvStreamPos; break; } _mbr.Close(); _mbr = null; } if (_recvStream.Position == _recvStream.Length) { _recvStream.SetLength(0); } }
//private long _lastHeartbeatTime; void Update() { if (_recvQueue != null && _recvQueue.Count > 0) { lock (_recvQueue) { int sendCount = 0; while (_recvQueue != null && _recvQueue.Count > 0) { if (sendCount <= MAX_SEND_COUNT) { _protocolItem2 = _recvQueue.Dequeue(); if (ProtocolConf.instance.ExistInLuaProcotols(_protocolItem2.ProtocolId)) { LuaInterface.ToLuaPb.SetFromSocket(_protocolItem2.ProtocolId, _protocolItem2.ProtocolBytes); } else { Debugger.Log(_protocolItem2.ProtocolId.ToString() + "收到服务器的协议:" + _protocolItem2.ToString()); if (_protocolItem2.ProtocolId == (int)Logic.Protocol.Model.MSG.ReconnectResp) { SendCacheMessage();//发送协议缓存 } else if (_protocolItem2.ProtocolId == (int)Logic.Protocol.Model.MSG.ClientActiveReq) { Common.GameTime.Controller.TimeController.instance.ServerTimeTicksMillisecondAfter9 = ProtocolProxy.instance.UserID6; } else { if (_protocolItem2.ProtocolId == (int)Logic.Protocol.Model.MSG.LoginResp) { UserID2 = _userID2;//登陆成功时保存 } //else if (_protocolItem2.ProtocolId == (int)Logic.Protocol.Model.MSG.FightCmdSynResp) //{ // Logic.Protocol.Model.FightCmdSynResp resp = _protocolItem2.Protocol as Logic.Protocol.Model.FightCmdSynResp; // Debugger.Log("update time:{0},release Time:{1}", Common.Util.TimeUtil.GetTimeStamp(), resp.releaseTime); //} Observers.Facade.Instance.SendNotification(_protocolItem2.ProtocolId.ToString(), _protocolItem2.Protocol); } } if (_recvQueue.Count == 0) { Logic.UI.Mask.Contorller.MaskController.instance.HideMask(); } //_beatFailCount = 0; _lastProtocolTime = Time.realtimeSinceStartup; sendCount++; } else { break; } } } } if (Time.realtimeSinceStartup - _lastProtocolTime > BEAT_INTERVAL) { //if (_beatFailCount >= BEAT_MAX_COUNT) //{ // Debugger.Log("断线重连{0}次超时", BEAT_MAX_COUNT); // if (connectedHandler != null) // connectedHandler(false); // return; //} //if (_beatFailCount > 0) //{ // Debugger.Log("断线重连第{0}次", _beatFailCount); // _lastProtocolTime = Time.realtimeSinceStartup + BEAT_INTERVAL / 2; //} //else _lastProtocolTime = Time.realtimeSinceStartup; SendProtocol(new Logic.Protocol.Model.ClientActiveReq()); Debugger.Log("beat connect !"); //_lastHeartbeatTime = Common.Util.TimeUtil.GetTimeStamp(); //_beatFailCount++; } }