Пример #1
0
        private void SendThread(object o)
        {
            List <byte[]> workList = new List <byte[]>(10);

            while (mSendWork)
            {
                if (!mSendWork)
                {
                    break;
                }

                if (mClientSocket == null || !mClientSocket.Connected)
                {
                    continue;
                }

                mSendSemaphore.WaitResource();
                if (mSendMsgQueue.Empty())
                {
                    continue;
                }

                mSendMsgQueue.MoveTo(workList);
                try
                {
                    for (int k = 0; k < workList.Count; ++k)
                    {
                        var msgObj = workList[k];
                        if (mSendWork)
                        {
                            mClientSocket.Send(msgObj, msgObj.Length, SocketFlags.None);
                        }
                    }
                }
                catch (ObjectDisposedException e)
                {
                    ReportSocketClosed(ESocketError.ERROR_1, e.Message);
                    break;
                }
                catch (Exception e)
                {
                    ReportSocketClosed(ESocketError.ERROR_2, e.Message);
                    break;
                }
                finally
                {
                    for (int k = 0; k < workList.Count; ++k)
                    {
                        var msgObj = workList[k];
                        StreamBufferPool.RecycleBuffer(msgObj);
                    }
                    workList.Clear();
                }
            }

            if (mStatus == SOCKSTAT.CONNECTED)
            {
                mStatus = SOCKSTAT.CLOSED;
            }
        }
Пример #2
0
        private void UpdatePacket()
        {
            if (!mReceiveMsgQueue.Empty())
            {
                mReceiveMsgQueue.MoveTo(mTempMsgList);

                try
                {
                    for (int i = 0; i < mTempMsgList.Count; ++i)
                    {
                        var objMsg = mTempMsgList[i];
                        if (ReceivePkgHandle != null)
                        {
                            ReceivePkgHandle(objMsg);
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError("Got the f*****g exception :" + e.Message);
                }
                finally
                {
                    for (int i = 0; i < mTempMsgList.Count; ++i)
                    {
                        StreamBufferPool.RecycleBuffer(mTempMsgList[i]);
                    }
                    mTempMsgList.Clear();
                }
            }
        }
Пример #3
0
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing && !Empty())
         {
             lock (mMutex)
             {
                 for (int i = 0; i < mMessageList.Count; i++)
                 {
                     StreamBufferPool.RecycleBuffer(mMessageList[i]);
                 }
                 mMessageList.Clear();
             }
         }
         disposedValue = true;
     }
 }
Пример #4
0
    private void TestCSEncodeAndLuaDeconde()
    {
#if !FOR_GC_TEST
        Logger.Log("=========================NewRound=========================");
#endif
        msSend.ResetStream();

        ntf_battle_frame_data                dataTmp      = ProtoFactory.Get <ntf_battle_frame_data>();
        ntf_battle_frame_data.one_slot       oneSlot      = ProtoFactory.Get <ntf_battle_frame_data.one_slot>();
        ntf_battle_frame_data.cmd_with_frame cmdWithFrame = ProtoFactory.Get <ntf_battle_frame_data.cmd_with_frame>();
        one_cmd oneCmd = ProtoFactory.Get <one_cmd>();
        cmdWithFrame.cmd = oneCmd;
        oneSlot.cmd_list.Add(cmdWithFrame);
        dataTmp.slot_list.Add(oneSlot);
        DeepCopyData(data, dataTmp);
        ProtoBufSerializer.Serialize(msSend.memStream, dataTmp);
        ProtoFactory.Recycle(dataTmp);//*************回收,很重要

        byte[] sendBytes = StreamBufferPool.GetBuffer(msSend, 0, (int)msSend.Position());

#if !FOR_GC_TEST
        // 打印字节流和数据
        Debug.Log("CS send to Lua =================>>>" + sendBytes.Length + " bytes : ");
        var sb = new StringBuilder();
        for (int i = 0; i < sendBytes.Length; i++)
        {
            sb.AppendFormat("{0}\t", sendBytes[i]);
        }
        Logger.Log(sb.ToString());
        PrintData(data);
#endif

        ForCSCallLua(sendBytes);

        IncreaseData();
        StreamBufferPool.RecycleBuffer(sendBytes);
    }
Пример #5
0
 protected override void RecycleChildren(one_cmd netData)
 {
     StreamBufferPool.RecycleBuffer(netData.cmd_data);
 }
Пример #6
0
        protected override void DoReceive(StreamBuffer streamBuffer, ref int bufferCurLen)
        {
            try
            {
                /**  for test
                 * String plainStr = "中国";
                 * byte[] encryptBys = Tea.Tea.encrypt2(System.Text.Encoding.Default.GetBytes(plainStr), Tea.Tea.KEY);
                 * int length = Tea.Tea.byteToShort(encryptBys, 0);
                 * byte[] decodeBys = Tea.Tea.decrypt2(encryptBys, length, 2, Tea.Tea.KEY);
                 * string str = System.Text.Encoding.Default.GetString(decodeBys);
                 * Logger.Log("=================================" + str);
                 **/


                // 组包、拆包
                byte[] data = streamBuffer.GetBuffer();
                int    lastStartBufferIndex = 0; // 上一次读取的开始位置
                streamBuffer.ResetStream();
                while (true)
                {
                    // 总长度
                    if (bufferCurLen - lastStartBufferIndex < PACKAGE_HEAD_SIZE)
                    {
                        break;
                    }

                    //short msgLen = BitConverter.ToInt16(data, start);
                    int msgLen    = Tea.Tea.byteToShort(data, lastStartBufferIndex); // 消息真正长度
                    int encodeLen = (msgLen + 7) / 8 * 8;                            // 消息长度补足8位
                    if (bufferCurLen - lastStartBufferIndex - PACKAGE_HEAD_SIZE < encodeLen)
                    {
                        break;
                    }

                    // 提取字节流,去掉开头表示长度的4字节
                    //start += PACKAGE_HEAD_SIZE;
                    // 获取的是缓存
                    byte[] bytes = streamBuffer.ToArray(lastStartBufferIndex + PACKAGE_HEAD_SIZE, encodeLen); // 当前一条协议的长度
                    //解密
                    byte[] decryptContent = Tea.Tea.decryptCopy3(bytes, msgLen, 0, netSecretKey);

                    StreamBufferPool.RecycleBuffer(bytes);
                    //streamBuffer.
                    //将decrpy加入缓存
                    //StreamBufferPool.
                    //int msgId = Tea.Tea.byteToInt(decryptContent,4);
                    // Logger.Log("C# get msgId : "+ msgId);
#if LOG_RECEIVE_BYTES
                    var sb = new System.Text.StringBuilder();
                    for (int i = 0; i < decryptContent.Length; i++)
                    {
                        sb.AppendFormat("{0}\t", decryptContent[i]);
                    }
                    Logger.Log("HjTcpNetwork receive bytes : " + sb.ToString());
#endif
                    mReceiveMsgQueue.Add(decryptContent);

                    // 下一次组包
                    lastStartBufferIndex = lastStartBufferIndex + PACKAGE_HEAD_SIZE + encodeLen;
                }

                if (lastStartBufferIndex > 0)
                {
                    bufferCurLen -= lastStartBufferIndex;
                    streamBuffer.CopyFrom(data, lastStartBufferIndex, 0, bufferCurLen);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(string.Format("Tcp receive package err : {0}\n {1}", ex.Message, ex.StackTrace));
            }
        }