/// <summary>
 /// 推送检测
 /// </summary>
 /// <param name="token">需要检测的客户端票据</param>
 internal void PushCheck(RemoteConnection token)
 {
     remoteUserTokens.Enqueue(token);
 }
Exemple #2
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="sessionId">会话ID</param>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        internal bool SendAsyncEvent(RemoteConnection client, ushort sessionId, byte[] buffer, int offset, int count)
        {
            if (client == null || client.socket == null)
            {
                return(false);
            }
            // 重置超时标记
            if (monitorSocketStatusTask != null)
            {
                Interlocked.Exchange(ref client.timeoutCount, 0);
            }

            // 数据打包
            byte[] data = null;
            if (offset == 0 && buffer.Length == count)
            {
                data = buffer;
            }
            else if (offset > 0)
            {
                data = new byte[count];
                Array.Copy(buffer, offset, data, 0, count);
            }

            // 数据发送
            var SEAE = client.sendEventArgs.Pop();

            if (client.socket.socketType == SocketType.Stream)
            {
                data = client.rBuffer.Encode(data);
                SEAE.SetBuffer(data, 0, data.Length);
                bool willRaiseEvent = client.socket.SendAsync(SEAE);
                if (!willRaiseEvent)
                {
                    return(ProcessSend(SEAE));
                }
                else
                {
                    return(true);
                }
            }
            else if (client.socket.socketType == SocketType.Dgram)
            {
                byte[] sendBuffer = new byte[3 + buffer.Length];
                /* 会话ID 高位 */
                // 1 byte
                sendBuffer[0] = (byte)((sessionId >> 8) & 0xFF);
                /* 会话ID 低位 */
                // 1 byte
                sendBuffer[1] = (byte)((sessionId) & 0xFF);
                /* 数据长度标识 指令验签(0x66混淆参数)*/
                // 1 byte
                sendBuffer[2] = (byte)(sendBuffer[0] + sendBuffer[1] + 0x66);
                Buffer.BlockCopy(data, 0, sendBuffer, 3, data.Length);

                SEAE.SetBuffer(sendBuffer, 0, sendBuffer.Length);
                bool willRaiseEvent = serverSocket.SendToAsync(SEAE);
                if (!willRaiseEvent)
                {
                    return(ProcessSend(SEAE));
                }
                else
                {
                    return(true);
                }
            }
            return(false);
        }