/// <summary> /// 收到消息 /// </summary> /// <param name="br">流</param> /// <param name="socketID">套接字ID</param> /// <param name="localSID">本地套接字ID</param> /// <param name="len">长度</param> public virtual void onCallBack(FCBinary 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(); } } } } FCMessage message = new FCMessage(GroupID, ServiceID, functionID, sessionID, requestID, socketID, state, compressType, bodyLength, body); onReceive(message); onWaitMessageHandle(message); message.m_body = null; body = null; }
/// <summary> /// 保持活跃 /// </summary> /// <param name="socketID">套接字ID</param> /// <returns>状态</returns> public virtual int keepAlive(int socketID) { FCBinary bw = new FCBinary(); bw.writeInt((int)4); byte[] bytes = bw.getBytes(); int length = bytes.Length; int ret = FCServerSockets.send(m_socketID, socketID, bytes, length); bw.close(); return(ret); }
/// <summary> /// 发送消息 /// </summary> /// <param name="message">消息</param> public override int send(FCMessage message) { FCBinary bw = new FCBinary(); byte[] body = message.m_body; int bodyLength = message.m_bodyLength; int uncBodyLength = bodyLength; lock (m_compressTypes) { if (m_compressTypes.ContainsKey(message.m_socketID)) { message.m_compressType = m_compressTypes.get(message.m_socketID); } } 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(); lock (FCHttpMonitor.MainMonitor.m_httpDatas) { FCHttpMonitor.MainMonitor.m_httpDatas.get(message.m_socketID).m_resBytes = bytes; } int ret = bytes.Length; UpFlow += ret; bw.close(); onClientClose(message.m_socketID, 0); return(ret); }
/// <summary> /// 回调函数 /// </summary> /// <param name="socketID">连接ID</param> /// <param name="localSID">本地连接ID</param> /// <param name="bytes">数据</param> /// <param name="len">长度</param> public static void callBack(int socketID, int localSID, byte[] bytes, int len) { m_downFlow += len; try { if (len > 4) { FCBinary br = new FCBinary(); br.write(bytes, len); int head = br.readInt(); int groupID = br.readShort(); int serviceID = br.readShort(); if (m_services.containsKey(serviceID)) { m_services.get(serviceID).onCallBack(br, socketID, localSID, len); } br.close(); } } catch (Exception ex) { Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace); } }
/// <summary> /// 发送消息 /// </summary> /// <param name="message">消息</param> public virtual int send(FCMessage message) { FCBinary bw = new FCBinary(); 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; int ret = FCClientSockets.send(message.m_socketID, bytes, length); m_upFlow += ret; bw.close(); return(ret); }
/// <summary> /// 发送POST数据 /// </summary> /// <param name="message">消息</param> /// <returns>返回消息</returns> public int sendRequest(FCMessage message) { FCBinary bw = new FCBinary(); 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; FCClientService.callBack(message.m_socketID, 0, dataArray, ret); return(ret); }