示例#1
0
        // 发送
        public void Send(wmPacket oPacket)
        {
            // 判断socket连接
            if (null == m_socketClient)
            {
                return;
            }
            // 判断包
            if (null == oPacket)
            {
                return;
            }

            try
            {
                // 生成完整的数据包: 加密 压缩 计算包头
                oPacket.Build(m_pCryp);
                // 包头byte
                oPacket.GetPacketHeader();
                // 数据byte
                oPacket.GetData();
                // 创建新的byte[]
                byte[] curData = new byte[oPacket.GetPacketSize()];
                // 初始化
                Array.Clear(curData, 0, curData.Length);
                // 拷贝包头部分
                Array.Copy(oPacket.GetPacketHeader(), 0, curData, 0, oPacket.GetCurPacketHeaderSize());
                // 拷贝数据部分
                Array.Copy(oPacket.GetData(), 0, curData, oPacket.GetCurPacketHeaderSize(), oPacket.GetCurDataSize());
                //// 转换为网络数据
                //Array.Reverse(curData);
                m_socketClient.Send(curData);
            }
            catch (SocketException se)
            {
                throw (se);
            }
            catch (ObjectDisposedException ode)
            {
                throw (ode);
            }
            catch (Exception ce)
            {
                Debug.Log(string.Format("Exception : {0}", ce.ToString()));
            }
        }
示例#2
0
        private void BuildPacket(byte[] pData, int nDataSize)
        {
            /// 还需要的数据长度
            int nCurRecvSize = 0;
            /// 当前读取位置
            int nCurRead = 0;

            // 包头
            byte[] pPacketHeader;
            // 数据
            byte[] pPacketData;

            for (; ;)
            {
                pPacketHeader = m_pPacket.GetPacketHeader();
                pPacketData   = m_pPacket.GetData();

                if (m_pPacket.GetPacketSize() == 0)
                {
                    /** 先收包头 **/
                    nCurRecvSize = (int)(SPacketHeader.PACKET_HEADER_SIZE - m_pPacket.GetCurPacketHeaderSize());

                    if (0 != nCurRecvSize)
                    {
                        if (nDataSize < nCurRecvSize)
                        {
                            Array.Copy(pData, nCurRead, pPacketHeader, m_pPacket.GetCurPacketHeaderSize(), nDataSize);
                            m_pPacket.m_wRecvLen += nDataSize;
                            return;
                        }
                        Array.Copy(pData, nCurRead, pPacketHeader, m_pPacket.GetCurPacketHeaderSize(), nCurRecvSize);
                        nDataSize            -= nCurRecvSize;
                        nCurRead             += nCurRecvSize;
                        m_pPacket.m_wRecvLen += nCurRecvSize;

                        // 包头OK了, 校验包头
                        if (!m_pPacket.IsValid())
                        {
                            m_pPacket = new wmPacket();
                            /// 包异常
                            return;
                        }
                    }
                }

                /// 包数据
                nCurRecvSize = (int)(m_pPacket.GetPacketSize() - m_pPacket.m_wRecvLen);
                if (nDataSize < nCurRecvSize)
                {
                    Array.Copy(pData, nCurRead, pPacketData, m_pPacket.m_wRecvLen - SPacketHeader.PACKET_HEADER_SIZE, nDataSize);
                    m_pPacket.m_wRecvLen += nDataSize;
                    return;
                }
                Array.Copy(pData, nCurRead, pPacketData, m_pPacket.m_wRecvLen - SPacketHeader.PACKET_HEADER_SIZE, nCurRecvSize);
                nDataSize            -= nCurRecvSize;
                nCurRead             += nCurRecvSize;
                m_pPacket.m_wRecvLen += nCurRecvSize;

                /// 一个完整的包
                m_pPacket.Parse(m_pCryp);
                //pEvent = new CWormEvent(CWormEvent.WORM_EVENT);
                //pEvent.m_nEventId = 1;
                //pEvent.m_pPacket = m_pPacketBuf;
                //m_pCallback.dispatchEvent(pEvent);
                // 将包压入队列
                Update();
                // New出新包
                NewPacket();

                if (0 == nDataSize)
                {
                    return;
                }
            }
        }