/// <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); } }
/// <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; }