示例#1
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="nMessageID"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool SendMessage(int nMessageID, Byte[] data)
        {
            if (null == data)
            {
                return(false);
            }

            int bodysize = data.Length;
            ///组包
            SocketNetPacket sendPack = new SocketNetPacket((Int16)nMessageID, bodysize);

            sendPack.SetPackBody(data);

            lock (m_SendObject)
            {
                m_SendPackList.AddLast(sendPack);
                if (1 == m_SendPackList.Count)
                {
                    m_ManualSendEvent.Set();
                }

                Debug.Log("CNetTCPSocketConnect::SendMessage m_SendPackList.Count = " + m_SendPackList.Count);
            }

            return(true);
        }
示例#2
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 发送子线程回调函数
        /// </summary>
        private void __SendThreadFunc()
        {
            while (false == m_IPAddrMsg.m_IsNeedClose && true == m_IPAddrMsg.m_IsConnect && null != m_Socket)
            {
                SocketNetPacket sendpack = null;

                //取出消息缓存中最前一条数据
                lock (m_SendObject)
                {
                    if (m_SendPackList.Count != 0)
                    {
                        sendpack = m_SendPackList.First.Value;
                        m_SendPackList.RemoveFirst();
                    }
                }

                //发包给服务器
                if (null != sendpack)
                {
                    try
                    {
                        int    nBufferSize = 0;
                        Byte[] sendBuffer  = sendpack.GetBuffer(out nBufferSize);

                        int sendsize = m_Socket.Send(sendBuffer, 0, nBufferSize, SocketFlags.None);
                        if (sendsize != nBufferSize)
                        {
                            Debug.LogError("NetTCPSocketConnect::__SendThreadFunc send buffer size error:");
                            m_IPAddrMsg.m_IsNeedClose = true;
                            break;
                        }

                        m_unSendTotalBytes += (uint)sendsize;
                    }
                    catch (System.Exception exception)
                    {
                        Debug.LogError("NetTCPSocketConnect::__SendThreadFunc send buffer size error:" + exception.ToString());
                        m_IPAddrMsg.m_IsNeedClose = true;
                        break;
                    }
                }
                else
                {
                    m_ManualSendEvent.WaitOne();
                }
            }

            if (null == m_Socket)
            {
                Debug.Log("NetTCPSocketConnect::__SendThreadFunc m_Socket is null");
            }
            Debug.Log("NetTCPSocketConnect::__SendThreadFunc m_IPAddrMsg.m_IsNeedClose = " + m_IPAddrMsg.m_IsNeedClose);
            Debug.Log("NetTCPSocketConnect::__SendThreadFunc m_IPAddrMsg.m_IsConnect = " + m_IPAddrMsg.m_IsConnect);
            Debug.Log("NetTCPSocketConnect::__SendThreadFunc send message thread is exit...");
        }
示例#3
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 读取 包头数据
        /// </summary>
        /// <returns></returns>
        private bool __ReadPacketHead()
        {
            if (null == m_Socket || false == m_Socket.Connected)
            {
                Debug.Log("NetTCPSocketConnect::__ReadPacketHead socket is null or connecd = false ");
                return(false);
            }

            try
            {
                Array.Clear(m_ReceiveHead, 0, m_ReceiveHead.Length);
                int receiveSize = m_Socket.Receive(m_ReceiveHead, SNetPacketCommon.PACK_HEAD_SIZE, SocketFlags.None);
                if (receiveSize == 0)
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketHead read pack head 0 data ,will close connect");
                    return(false);
                }

                while (receiveSize < SNetPacketCommon.PACK_HEAD_SIZE)
                {
                    if (m_Socket == null || m_Socket.Connected == false)
                    {
                        Debug.Log("NetTCPSocketConnect::__ReadPacketHead socket is null or connecd = false ");
                        return(false);
                    }

                    int nTemsendcount = m_Socket.Receive(m_ReceiveHead, receiveSize, SNetPacketCommon.PACK_HEAD_SIZE - receiveSize, SocketFlags.None);
                    if (0 == nTemsendcount)
                    {
                        Debug.Log("NetTCPSocketConnect::__ReadPacketHead read pack receive 0 data,will close connect");
                        return(false);
                    }

                    receiveSize += nTemsendcount;
                }

                m_unRecvTotalBytes += (uint)receiveSize;

                if (false == SocketNetPacket.IsPackHead(m_ReceiveHead))
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketHead receive data is not packhead");
                    return(false);
                }

                return(true);
            }

            catch (System.Exception e)
            {
                Debug.LogError("NetTCPSocketConnect::__ReadPacketHead Socket  receive error: " + e.ToString());
                return(false);
            }
        }
示例#4
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 取出消息缓存中排在最前的一条的消息包的数据,并从消息缓存中移除
        /// </summary>
        /// <returns></returns>
        public SocketNetPacket GetReceivePack()
        {
            if (m_ReceiveObject == null)
            {
                return(null);
            }
            SocketNetPacket pack = null;

            // lock
            lock (m_ReceiveObject)
            {
                if (0 < m_ReceivePackList.Count)
                {
                    pack = m_ReceivePackList.First.Value;
                    m_ReceivePackList.RemoveFirst();
                }
            }
            // unlock
            return(pack);
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// 解包处理
        /// </summary>
        /// <param name="client"></param>
        /// <param name="packet"></param>
        public void OnRecvMessage(SocketNetPacket packet)
        {
            if (m_MessageHandlers.ContainsKey((int)packet.GetMessageID()))
            {
                if (m_OnRecvMsgCallBack != null)
                {
                    m_OnRecvMsgCallBack(packet);
                }

                List<NetOnRecvMessageDelegate> handlerList = null;
                m_MessageHandlers.TryGetValue((int)packet.GetMessageID(), out handlerList);

                if (handlerList != null)
                {
                    List<NetOnRecvMessageDelegate> deletes = new List<NetOnRecvMessageDelegate>();
                    List<NetOnRecvMessageDelegate> exeList = new List<NetOnRecvMessageDelegate>(handlerList);
                    foreach (NetOnRecvMessageDelegate del in exeList)
                    {
                        if (del != null)
                        {
                            if (del.Target != null && del.Target.ToString() != SNetCommon.NULL)
                            {
                                int nSize = 0;
                                Byte[] pBuffer = packet.GetBody(out nSize);
                                int nMessageID = packet.GetMessageID();
                                del(nMessageID, pBuffer, nSize);
                            }
                            else
                            {
                                deletes.Add(del);
                            }
                        }
                    }

                    foreach (NetOnRecvMessageDelegate i in deletes)
                    {
                        handlerList.Remove(i);
                    }
                }
            }
        }
        //-------------------------------------------------------------------------
        /// <summary>
        /// 解包处理
        /// </summary>
        /// <param name="client"></param>
        /// <param name="packet"></param>
        public void OnRecvMessage(SocketNetPacket packet)
        {
            if (m_MessageHandlers.ContainsKey((int)packet.GetMessageID()))
            {
                if (m_OnRecvMsgCallBack != null)
                {
                    m_OnRecvMsgCallBack(packet);
                }

                List <NetOnRecvMessageDelegate> handlerList = null;
                m_MessageHandlers.TryGetValue((int)packet.GetMessageID(), out handlerList);

                if (handlerList != null)
                {
                    List <NetOnRecvMessageDelegate> deletes = new List <NetOnRecvMessageDelegate>();
                    List <NetOnRecvMessageDelegate> exeList = new List <NetOnRecvMessageDelegate>(handlerList);
                    foreach (NetOnRecvMessageDelegate del in exeList)
                    {
                        if (del != null)
                        {
                            if (del.Target != null && del.Target.ToString() != SNetCommon.NULL)
                            {
                                int    nSize      = 0;
                                Byte[] pBuffer    = packet.GetBody(out nSize);
                                int    nMessageID = packet.GetMessageID();
                                del(nMessageID, pBuffer, nSize);
                            }
                            else
                            {
                                deletes.Add(del);
                            }
                        }
                    }

                    foreach (NetOnRecvMessageDelegate i in deletes)
                    {
                        handlerList.Remove(i);
                    }
                }
            }
        }
示例#7
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 读取包体数据
        /// </summary>
        /// <returns></returns>
        private bool __ReadPacketBody()
        {
            if (m_ReceiveHead == null)
            {
                Debug.LogError("NetTCPSocketConnect::__ReadPacketBody m_ReceiveHead is null");
                return false;
            }

            // 获取消息头里的消息id和包身上度
            Int16 buffersize = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_LENGTH_OFFSET);
            Int16 messageid = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_MESSSAGEID_OFFSET);

            Int32 bodysize = buffersize - SNetPacketCommon.PACK_HEAD_SIZE;
            if (bodysize <= 0)
            {
                Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive empty pack message id:" + messageid);
                return true;
            }

            SocketNetPacket netPacket = new SocketNetPacket(messageid, bodysize);
            ///设置包头数据
            if (false == netPacket.SetPackHead(m_ReceiveHead))
            {
                Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive headis error");
                return false;
            }

            int nBufferSize = 0;
            Byte[] packBuffer = netPacket.GetBuffer(out nBufferSize);

            try
            {
                if (null == m_Socket || false == m_Socket.Connected)
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==fales ");
                    return false;
                }

                Int32 receiveSize = m_Socket.Receive(packBuffer, SNetPacketCommon.PACK_HEAD_SIZE, bodysize, SocketFlags.None);
                if (receiveSize == 0)
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close  connect");
                    return false;
                }
                // 当要接受的数据超过包的大小时,将消息截断,再接收
                while (receiveSize < bodysize)
                {
                    if (null == m_Socket || false == m_Socket.Connected)
                    {
                        Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==falsewww eeee");
                        return false;
                    }

                    Int32 temsendcount = m_Socket.Receive(packBuffer, receiveSize + SNetPacketCommon.PACK_HEAD_SIZE, bodysize - receiveSize, SocketFlags.None);

                    if (temsendcount == 0)
                    {
                        Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close  connect");
                        return false;
                    }

                    receiveSize += temsendcount;
                }

                m_unRecvTotalBytes += (uint)receiveSize;
                // 加入收包的消息队列
                // lock is begin in here
                lock (m_ReceiveObject)
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketBody Recv Full Data Len = " + nBufferSize);
                    m_ReceivePackList.AddLast(netPacket);
                }
                // unlock is done in here

                return true;

            }
            catch (System.Exception e)
            {
                Debug.LogError("NetTCPSocketConnect::__ReadPacketBody  Socket  receive error: " + e.ToString());
                DisConnection();
                return false;
            }
        }
示例#8
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="nMessageID"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool SendMessage(int nMessageID, Byte[] data)
        {
            if (null == data)
            {
                return false;
            }

            int bodysize = data.Length;
            ///组包
            SocketNetPacket sendPack = new SocketNetPacket((Int16)nMessageID, bodysize);
            sendPack.SetPackBody(data);

            lock (m_SendObject)
            {
                m_SendPackList.AddLast(sendPack);
                if (1 == m_SendPackList.Count)
                {
                    m_ManualSendEvent.Set();
                }

                Debug.Log("CNetTCPSocketConnect::SendMessage m_SendPackList.Count = " + m_SendPackList.Count);
            }

            return true;
        }
示例#9
0
        //-------------------------------------------------------------------------
        /// <summary>
        /// 读取包体数据
        /// </summary>
        /// <returns></returns>
        private bool __ReadPacketBody()
        {
            if (m_ReceiveHead == null)
            {
                Debug.LogError("NetTCPSocketConnect::__ReadPacketBody m_ReceiveHead is null");
                return(false);
            }

            // 获取消息头里的消息id和包身上度
            Int16 buffersize = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_LENGTH_OFFSET);
            Int16 messageid  = BitConverter.ToInt16(m_ReceiveHead, SNetPacketCommon.PACK_MESSSAGEID_OFFSET);

            Int32 bodysize = buffersize - SNetPacketCommon.PACK_HEAD_SIZE;

            if (bodysize <= 0)
            {
                Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive empty pack message id:" + messageid);
                return(true);
            }

            SocketNetPacket netPacket = new SocketNetPacket(messageid, bodysize);

            ///设置包头数据
            if (false == netPacket.SetPackHead(m_ReceiveHead))
            {
                Debug.Log("NetTCPSocketConnect::__ReadPacketBody receive headis error");
                return(false);
            }

            int nBufferSize = 0;

            Byte[] packBuffer = netPacket.GetBuffer(out nBufferSize);

            try
            {
                if (null == m_Socket || false == m_Socket.Connected)
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==fales ");
                    return(false);
                }

                Int32 receiveSize = m_Socket.Receive(packBuffer, SNetPacketCommon.PACK_HEAD_SIZE, bodysize, SocketFlags.None);
                if (receiveSize == 0)
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close  connect");
                    return(false);
                }
                // 当要接受的数据超过包的大小时,将消息截断,再接收
                while (receiveSize < bodysize)
                {
                    if (null == m_Socket || false == m_Socket.Connected)
                    {
                        Debug.Log("NetTCPSocketConnect::__ReadPacketBody m_Socket==null|| m_Socket.Connected==falsewww eeee");
                        return(false);
                    }


                    Int32 temsendcount = m_Socket.Receive(packBuffer, receiveSize + SNetPacketCommon.PACK_HEAD_SIZE, bodysize - receiveSize, SocketFlags.None);

                    if (temsendcount == 0)
                    {
                        Debug.Log("NetTCPSocketConnect::__ReadPacketBody readPackBody read 0 data will close  connect");
                        return(false);
                    }

                    receiveSize += temsendcount;
                }

                m_unRecvTotalBytes += (uint)receiveSize;
                // 加入收包的消息队列
                // lock is begin in here
                lock (m_ReceiveObject)
                {
                    Debug.Log("NetTCPSocketConnect::__ReadPacketBody Recv Full Data Len = " + nBufferSize);
                    m_ReceivePackList.AddLast(netPacket);
                }
                // unlock is done in here

                return(true);
            }
            catch (System.Exception e)
            {
                Debug.LogError("NetTCPSocketConnect::__ReadPacketBody  Socket  receive error: " + e.ToString());
                DisConnection();
                return(false);
            }
        }