/// <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); } }
public static void InitServer() { m_dataDic = new Dictionary <string, string>(); Binary binary = HttpGetService.GetAsBinary(DataCenter.ServerAddr + "giraservice?func=getall"); if (binary != null) { int dataLength = binary.ReadInt(); for (int i = 0; i < dataLength; i++) { String xmlName = binary.ReadString(); int nextBisLen = binary.ReadInt(); byte[] byts = new byte[nextBisLen]; binary.ReadBytes(byts); String xmlStr = System.Text.Encoding.Default.GetString(byts); m_dataDic[xmlName] = xmlStr; } } }
/// <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); }
/// <summary> /// 收到消息 /// </summary> /// <param name="br">流</param> /// <param name="socketID">套接字ID</param> /// <param name="localSID">本地套接字ID</param> /// <param name="len">长度</param> public virtual void OnCallBack(Binary br, int socketID, int localSID, int len) { int headSize = sizeof(int) * 4 + sizeof(short) * 3 + sizeof(byte) * 2; int functionID = br.ReadShort(); int sessionID = br.ReadInt(); int requestID = br.ReadInt(); int state = br.ReadByte(); int compressType = br.ReadByte(); int bodyLength = br.ReadInt(); byte[] body = new byte[len - headSize]; br.ReadBytes(body); if (compressType == COMPRESSTYPE_GZIP) { using (MemoryStream dms = new MemoryStream()) { using (MemoryStream cms = new MemoryStream(body)) { using (GZipStream gzip = new GZipStream(cms, CompressionMode.Decompress)) { byte[] buffer = new byte[1024]; int size = 0; while ((size = gzip.Read(buffer, 0, buffer.Length)) > 0) { dms.Write(buffer, 0, size); } body = dms.ToArray(); } } } } CMessage message = new CMessage(GroupID, ServiceID, functionID, sessionID, requestID, socketID, state, compressType, bodyLength, body); OnReceive(message); OnWaitMessageHandle(message); body = null; }
/// <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); }