/// <summary> /// 接收消息包 /// </summary> /// <param name="stream"></param> private MessageBag ReceiveMessageBag(NetworkStream stream) { MessageBag bag = null; try { byte[] buffer = Utility.Stream.ReadStream(stream, MessageHead.HeadLength); MessageHead head = MessageHead.Parse(buffer); if (head.BodyLength > 0) { byte[] bytesBody = Utility.Stream.ReadStream(stream, head.BodyLength); bag = new MessageBag(head, bytesBody); } else { bag = new MessageBag(head); } } catch (Exception ex) { ExceptionRecord.Record(ex.Message + ex.Source + ex.TargetSite + ex.StackTrace); throw; } return(bag); }
/// <summary> /// 接收客户端的消息包 /// </summary> /// <param name="stream"></param> private MessageBag ReceiveClientMessage(NetworkStream stream) { LogService.WriteServerRunLog(LogLevel.Info, "接收来自" + _publisherIP + "的不同类型的消息包"); byte[] buffer = Utility.Stream.ReadStream(stream, MessageHead.HeadLength); MessageHead head = MessageHead.Parse(buffer); if (head.BodyLength > 0) { byte[] bytesBody = Utility.Stream.ReadStream(stream, head.BodyLength); MessageType type = (MessageType)head.Type; switch (type) { case MessageType.User: if (head.State == 1) { return(new FirstMessageBag(bytesBody)); } else { //2用户退出时 | //3由CGI发送的UserID return(new MessageBag(head, bytesBody)); } case MessageType.File: //文件是独体,没有其它状态 return(new FileMessageBag(bytesBody)); default: return(new MessageBag(head, bytesBody)); } } return(new MessageBag(head)); }
/// <summary> /// 接收服务器端返回的消息包 /// </summary> public MessageBag ReceiveMessage() { byte[] buffer = Utility.Stream.ReadStream(_stream, MessageHead.HeadLength); if (buffer.Length == 0) { return(null); } MessageHead head = MessageHead.Parse(buffer); if (head.BodyLength > 0) { byte[] bytesBody = Utility.Stream.ReadStream(_stream, head.BodyLength); return(new MessageBag(head, bytesBody)); } return(new MessageBag(head)); }