/// <summary> /// 处理服务器信息 /// </summary> private void ReceiveDataThread() { string msg = null; while (isExit == false) { Protocol.CMD_TYPE msg_type = CMD_TYPE.NULL; string auth_code = string.Empty; string to_user = string.Empty; string msg_temp = string.Empty; try { bool isOK = Protocol.Protocol.ParseProtocolMsg(br, out msg_type, out auth_code, out to_user, out msg_temp); if (isOK) { msg = msg_temp; } else { msg = string.Empty; } } catch (Exception e) { if (isExit == false) { new NotifyForm("与服务器失去连接", true); } break; } if (!string.IsNullOrEmpty(msg) && msg_type != CMD_TYPE.BEAT && msg_type != CMD_TYPE.NULL && msg_type != CMD_TYPE.EXIT) { if (CMD_TYPE.QUERY == msg_type) { //怎么暴露给别人调用结果,回调? } else { new NotifyForm(msg, false, this); } } } }
/// <summary> /// 处理接收的客户端信息 /// </summary> /// <param name="userState">客户端信息</param> private void ReceiveDataThread(object userState) { User user = (User)userState; TcpClient client = user.client; SocketQueryHandler handler = new SocketQueryHandler(); string query_template = "发起一次查询。\r\n类别:{0}\r\n关键词:{1}\r\n结果:{2}\r\n"; while (isNormalExit == false) { string msg = null; try { Protocol.CMD_TYPE msg_type = CMD_TYPE.NULL; string auth_code = string.Empty; string to_user = string.Empty; string msg_temp = string.Empty; bool isOK = Protocol.Protocol.ParseProtocolMsg(user.br, out msg_type, out auth_code, out to_user, out msg_temp); if (isOK) { msg = msg_temp; //解析用户信息 try { var parser = new System.Web.Script.Serialization.JavaScriptSerializer(); User user_tmp = parser.Deserialize(auth_code.Replace('\0', ' '), typeof(User)) as User; if (null != user_tmp) { user.name = user_tmp.name; user.pwd = user_tmp.pwd; user.role = user_tmp.role; user.id = user_tmp.id; user.auth = user.auth; RefreshUserList(); } } catch (Exception e) { AddMsgHouseNewMsg(new User(), string.Format("用户信息解析异常:{0}", e.Message)); } } else { msg_temp = auth_code; //log 无法解析的信息 } if (msg_type != CMD_TYPE.NULL) { if (msg_type == CMD_TYPE.EXIT) { AddMsgHouseNewMsg(user, "离线."); user.bw.Close(); user.br.Close(); } else if (msg_type == CMD_TYPE.QUERY) { string query_type = string.Empty; string query_kw = string.Empty; string query_rlt = handler.Query(msg, out query_type, out query_kw); AddMsgHouseNewMsg(user, string.Format(query_template, query_type, query_kw, query_rlt)); //发送查询结果 SendToClient(CMD_TYPE.QUERY, new User(), user, query_rlt); } else { AddMsgHouseNewMsg(user, msg); } } } catch (Exception) { break; } } }