Beispiel #1
0
        public void SendTcp(Packet pkg)
        {
            if (pkg == null || pkg.Length <= 0)
            {
                return;
            }

            pkg.WriteHeader();
            pkg.WriteData();
            pkg.Pack();
            pkg.Position = 0;

            if (m_socket.Socket.Connected == false)
            {
                return;
            }

            try
            {
                lock (m_pkgQueue.SyncRoot)
                {
                    m_pkgQueue.Enqueue(pkg);

                    if (m_sendingTcp)
                    {
                        return;
                    }

                    m_sendingTcp = true;
                }

                if (m_socket.EnableAsyncSend)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(sendAsyncImp), this);
                }
                else
                {
                    sendAsyncEvent_Completed(this, m_sendAsyncEvent);
                }
            }
            catch (Exception ex)
            {
                log.Error("数据包发送失败.", ex);
                m_socket.Disconnect();
            }
        }
Beispiel #2
0
        public void SendTcp(Packet pkg)
        {
            if (pkg == null || pkg.Length <= 0) return;

            pkg.WriteHeader();
            pkg.WriteData();
            pkg.Pack();
            pkg.Position = 0;

            if (m_socket.Socket.Connected == false)
            {
                return;
            }

            try
            {
                lock (m_pkgQueue.SyncRoot)
                {
                    m_pkgQueue.Enqueue(pkg);

                    if (m_sendingTcp)
                    {
                        return;
                    }

                    m_sendingTcp = true;
                }

                if (m_socket.EnableAsyncSend)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(sendAsyncImp), this);
                }
                else
                {
                    sendAsyncEvent_Completed(this, m_sendAsyncEvent);
                }
            }
            catch (Exception ex)
            {
                log.Error("数据包发送失败.", ex);
                m_socket.Disconnect();
            }
        }