Esempio n. 1
0
 /// <summary>
 /// 回调函数
 /// </summary>
 /// <param name="socketID">连接ID</param>
 /// <param name="localSID">本地连接ID</param>
 /// <param name="str">数据</param>
 /// <param name="len">长度</param>
 public static void CallBack(int socketID, int localSID, IntPtr str, int len)
 {
     m_downFlow += len;
     try
     {
         if (len > 4)
         {
             byte[] bytes = GetValueChar(str, len);
             Binary br    = new Binary();
             br.Write(bytes, len);
             int           head      = br.ReadInt();
             int           groupID   = br.ReadShort();
             int           serviceID = br.ReadShort();
             BaseService[] services  = null;
             lock (m_services)
             {
                 services = m_services.ToArray();
             }
             foreach (BaseService service in services)
             {
                 if (service.SocketID == localSID)
                 {
                     service.OnCallBack(br, socketID, localSID, len);
                 }
             }
             br.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 进入区块链
        /// </summary>
        /// <returns>状态</returns>
        public int Enter()
        {
            Binary bw = new Binary();

            bw.WriteInt(DataCenter.ServerChatService.Port);
            bw.WriteInt(DataCenter.IsFull ? 1 : 0);
            bw.WriteString(DataCenter.UserID);
            bw.WriteString(DataCenter.UserName);
            byte[] bytes = bw.GetBytes();
            int    ret   = Send(new CMessage(GroupID, ServiceID, FUNCTIONID_ENTER, SessionID, DataCenter.ChatRequestID, SocketID, 0, CompressType, bytes.Length, bytes));

            bw.Close();
            return(ret);
        }
Esempio n. 3
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message">消息</param>
        public virtual int Send(CMessage message)
        {
            Binary bw = new Binary();

            byte[] body          = message.m_body;
            int    bodyLength    = message.m_bodyLength;
            int    uncBodyLength = bodyLength;

            if (message.m_compressType == COMPRESSTYPE_GZIP)
            {
                using (MemoryStream cms = new MemoryStream())
                {
                    using (GZipStream gzip = new GZipStream(cms, CompressionMode.Compress))
                    {
                        gzip.Write(body, 0, body.Length);
                    }
                    body       = cms.ToArray();
                    bodyLength = body.Length;
                }
            }
            int len = sizeof(int) * 4 + bodyLength + sizeof(short) * 3 + sizeof(byte) * 2;

            bw.WriteInt(len);
            bw.WriteShort((short)message.m_groupID);
            bw.WriteShort((short)message.m_serviceID);
            bw.WriteShort((short)message.m_functionID);
            bw.WriteInt(message.m_sessionID);
            bw.WriteInt(message.m_requestID);
            bw.WriteByte((byte)message.m_state);
            bw.WriteByte((byte)message.m_compressType);
            bw.WriteInt(uncBodyLength);
            bw.WriteBytes(body);
            byte[] bytes  = bw.GetBytes();
            int    length = bytes.Length;
            IntPtr ptr    = Marshal.AllocHGlobal(sizeof(byte) * length);

            for (int i = 0; i < length; i++)
            {
                IntPtr iptr = (IntPtr)((int)ptr + i);
                Marshal.WriteByte(iptr, bytes[i]);
            }
            int ret = SendByClient(message.m_socketID, ptr, length);

            m_upFlow += ret;
            Marshal.FreeHGlobal(ptr);
            bw.Close();
            return(ret);
        }
Esempio n. 4
0
        /// <summary>
        /// 获取弹幕信息
        /// </summary>
        /// <param name="chatData">聊天信息</param>
        /// <param name="body">包体</param>
        /// <param name="bodyLength">包体长度</param>
        /// <returns></returns>
        public static int GetChatData(ChatData chatData, byte[] body, int bodyLength)
        {
            Binary br = new Binary();

            br.Write(body, bodyLength);
            chatData.m_aes        = br.ReadString();
            chatData.m_tokens     = br.ReadString();
            chatData.m_from       = br.ReadString();
            chatData.m_to         = br.ReadString();
            chatData.m_content    = br.ReadString();
            chatData.m_bodyLength = br.ReadInt();
            if (chatData.m_bodyLength > 0)
            {
                chatData.m_body = new byte[chatData.m_bodyLength];
                br.ReadBytes(chatData.m_body);
            }
            br.Close();
            return(1);
        }
Esempio n. 5
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="userID">方法ID</param>
        /// <param name="tokens">请求ID</param>
        /// <param name="chatData">发送字符</param>
        public int Send(int functionID, int requestID, ChatData chatData)
        {
            Binary bw = new Binary();

            bw.WriteString(chatData.m_aes);
            bw.WriteString(chatData.m_tokens);
            bw.WriteString(chatData.m_from);
            bw.WriteString(chatData.m_to);
            bw.WriteString(chatData.m_content);
            bw.WriteInt(chatData.m_bodyLength);
            if (chatData.m_bodyLength > 0)
            {
                bw.WriteBytes(chatData.m_body);
            }
            byte[] bytes = bw.GetBytes();
            int    ret   = Send(new CMessage(GroupID, ServiceID, functionID, SessionID, requestID, SocketID, 0, CompressType, bytes.Length, bytes));

            bw.Close();
            return(ret);
        }
Esempio n. 6
0
        /// <summary>
        /// 保持活跃
        /// </summary>
        /// <param name="socketID">套接字ID</param>
        /// <returns>状态</returns>
        public virtual int KeepAlive(int socketID)
        {
            Binary bw = new Binary();

            bw.WriteInt((int)4);
            byte[] bytes  = bw.GetBytes();
            int    length = bytes.Length;
            IntPtr ptr    = Marshal.AllocHGlobal(sizeof(byte) * length);

            for (int i = 0; i < length; i++)
            {
                IntPtr iptr = (IntPtr)((int)ptr + i);
                Marshal.WriteByte(iptr, bytes[i]);
            }
            int ret = SendByClient(socketID, ptr, length);

            Marshal.FreeHGlobal(ptr);
            bw.Close();
            return(ret);
        }
Esempio n. 7
0
        /// <summary>
        /// 获取主机信息
        /// </summary>
        /// <param name="body">包体</param>
        /// <param name="bodyLength">包体长度</param>
        /// <returns></returns>
        public static int GetHostInfos(List <ChatHostInfo> datas, ref int type, byte[] body, int bodyLength)
        {
            Binary br = new Binary();

            br.Write(body, bodyLength);
            int size = br.ReadInt();

            type = br.ReadInt();
            for (int i = 0; i < size; i++)
            {
                ChatHostInfo data = new ChatHostInfo();
                data.m_ip         = br.ReadString();
                data.m_serverPort = br.ReadInt();
                data.m_type       = br.ReadInt();
                data.m_userID     = br.ReadString();
                data.m_userName   = br.ReadString();
                datas.Add(data);
            }
            br.Close();
            return(1);
        }
Esempio n. 8
0
        /// <summary>
        /// 发送POST数据
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns>返回消息</returns>
        public int SendRequest(CMessage message)
        {
            Binary bw = new Binary();

            byte[] body          = message.m_body;
            int    bodyLength    = message.m_bodyLength;
            int    uncBodyLength = bodyLength;

            if (message.m_compressType == COMPRESSTYPE_GZIP)
            {
                using (MemoryStream cms = new MemoryStream())
                {
                    using (GZipStream gzip = new GZipStream(cms, CompressionMode.Compress))
                    {
                        gzip.Write(body, 0, body.Length);
                    }
                    body       = cms.ToArray();
                    bodyLength = body.Length;
                }
            }
            int len = sizeof(int) * 4 + bodyLength + sizeof(short) * 3 + sizeof(byte) * 2;

            bw.WriteInt(len);
            bw.WriteShort((short)message.m_groupID);
            bw.WriteShort((short)message.m_serviceID);
            bw.WriteShort((short)message.m_functionID);
            bw.WriteInt(message.m_sessionID);
            bw.WriteInt(message.m_requestID);
            bw.WriteByte((byte)message.m_state);
            bw.WriteByte((byte)message.m_compressType);
            bw.WriteInt(uncBodyLength);
            bw.WriteBytes(body);
            byte[]         bytes  = bw.GetBytes();
            int            length = bytes.Length;
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(m_url);

            webReq.Method        = "POST";
            webReq.ContentType   = "application/x-www-form-urlencoded";
            webReq.ContentLength = bytes.Length;
            if (bytes != null)
            {
                Stream writer = webReq.GetRequestStream();
                writer.Write(bytes, 0, bytes.Length);
                writer.Close();
            }
            HttpWebResponse response      = (HttpWebResponse)webReq.GetResponse();
            Stream          reader        = response.GetResponseStream();
            long            contentLength = response.ContentLength;

            byte[] dataArray = new byte[contentLength];
            for (int i = 0; i < contentLength; i++)
            {
                dataArray[i] = (byte)reader.ReadByte();
            }
            response.Close();
            reader.Dispose();
            bw.Close();
            int ret = dataArray.Length;

            UpFlow += ret;
            IntPtr ptr = Marshal.AllocHGlobal(sizeof(byte) * ret);

            for (int i = 0; i < ret; i++)
            {
                IntPtr iptr = (IntPtr)((int)ptr + i);
                Marshal.WriteByte(iptr, dataArray[i]);
            }
            BaseService.CallBack(message.m_socketID, 0, ptr, ret);
            Marshal.FreeHGlobal(ptr);
            return(ret);
        }