Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public ProtocolItem(int protocolId, byte[] bytes)
        {
            ProtocolId = protocolId;
            byte[]        enryptByteArray;
            MBinaryWriter mbw = new MBinaryWriter();

            if (!Game.GameConfig.instance.encrypt)
            {
                if (bytes == null)
                {
                    mbw.Write(ProtocolConf.PROTOCOL_HEAD_LENGTH);
                }
                else
                {
                    mbw.Write(bytes.Length + ProtocolConf.PROTOCOL_HEAD_LENGTH);
                }
            }
            mbw.Write(ProtocolProxy.instance.Version);
            mbw.Write(ProtocolProxy.instance.UserID0);
            mbw.Write(ProtocolProxy.instance.UserID1);
            mbw.Write(ProtocolProxy.instance.UserID2);
            mbw.Write(ProtocolProxy.instance.UserID3);
            mbw.Write(ProtocolProxy.instance.UserID4);
            mbw.Write(ProtocolProxy.instance.UserID5);
            mbw.Write(TimeController.instance.ServerTimeTicksMillisecondAfter9);
            mbw.Write(ProtocolProxy.instance.UserID7);
            mbw.Write(ProtocolId);
            if (bytes != null)
            {
                mbw.Write(bytes);
            }
            if (Game.GameConfig.instance.encrypt)
            {
                enryptByteArray = Common.Util.EncryptUtil.AESEncryptBytes(mbw.ToArray(), Game.GameConfig.instance.aesEncryptKey);
                mbw.Clear();
                mbw.Write(enryptByteArray.Length);
                mbw.Write(enryptByteArray);
            }
            ProtocolItemBytes = mbw.ToArray();
            mbw.Close();
            mbw = null;
            if (ProtocolConf.NeedShowMask(ProtocolId))
            {
                Logic.UI.Mask.Contorller.MaskController.instance.ShowMask();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 向服务器发送协议时封装协议
        /// </summary>
        /// <returns></returns>
        private byte[] ParseItem()
        {
            if (Protocol == null)
            {
                return(null);
            }

            byte[]        tByteArray, enryptByteArray;
            MBinaryWriter mbw = new MBinaryWriter();

            if (ProtocolProxy.instance.IsLoginServer)
            {
                tByteArray = (Protocol as IProtocol).ToBytes();
                mbw.Write((ushort)(tByteArray.Length + ProtocolConf.PROTOCOL_HEAD_LENGTH));
                mbw.Write((ushort)ProtocolId);
            }
            else
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    ProtoBuf.Serializer.NonGeneric.Serialize(ms, Protocol);
                    tByteArray = ms.ToArray();
                }
                if (!Game.GameConfig.instance.encrypt)
                {
                    mbw.Write(tByteArray.Length + ProtocolConf.PROTOCOL_HEAD_LENGTH);
                }
                mbw.Write(ProtocolProxy.instance.Version);
                mbw.Write(ProtocolProxy.instance.UserID0);
                mbw.Write(ProtocolProxy.instance.UserID1);
                mbw.Write(ProtocolProxy.instance.UserID2);
                mbw.Write(ProtocolProxy.instance.UserID3);
                mbw.Write(ProtocolProxy.instance.UserID4);
                mbw.Write(ProtocolProxy.instance.UserID5);
                mbw.Write(TimeController.instance.ServerTimeTicksMillisecondAfter9);
                mbw.Write(ProtocolProxy.instance.UserID7);
                mbw.Write(ProtocolId);
            }
            mbw.Write(tByteArray);
            if (Game.GameConfig.instance.encrypt)
            {
                enryptByteArray = Common.Util.EncryptUtil.AESEncryptBytes(mbw.ToArray(), Game.GameConfig.instance.aesEncryptKey);
                mbw.Clear();
                mbw.Write(enryptByteArray.Length);
                mbw.Write(enryptByteArray);
            }
            tByteArray = mbw.ToArray();
            mbw.Close();
            mbw = null;
            return(tByteArray);
        }