Пример #1
0
        void BeginAccept()
        {
            bool completed = false;

            try {
                completed = m_Socket.AcceptAsync(m_AcceptEvent);
            } catch (System.Exception ex) {
                logger.error("Accept出现错误 : " + ex.ToString());
            }
            if (!completed)
            {
                ScorpioThreadPool.CreateThread(() => { BeginAccept(); });
            }
        }
Пример #2
0
        //发送协议
        public void Send(byte type, short msgId, byte[] data, int offset, int length)
        {
            if (!m_Socket.Connected)
            {
                return;
            }
            int count = length + 7;                                             //协议头长度  数据长度int(4个字节) + 数据类型byte(1个字节) + 协议IDshort(2个字节)

            byte[] buffer = new byte[count];
            Array.Copy(BitConverter.GetBytes(m_LengthIncludesLengthFieldLength ? count : count - 4), buffer, 4); //写入数据长度
            buffer[4] = type;                                                                                    //写入数据类型
            Array.Copy(BitConverter.GetBytes(msgId), 0, buffer, 5, 2);                                           //写入数据ID
            if (data != null)
            {
                Array.Copy(data, offset, buffer, 7, length);                    //写入数据内容
            }
            lock (m_SendQueue) { m_SendQueue.Enqueue(buffer); }
            ScorpioThreadPool.CreateThread(() => { BeginSend(); });
        }