void server_ReceiveCmd(object sender, QcCmdEventArgs e) { lock (obj) { QcCmd cmd = e.Cmd; string from = e.Cmd.tokens(1); string to = e.Cmd.tokens(2); if (lstUser.ContainsKey(from)) { lstUser[from].LastOnline = DateTime.Now; } switch (cmd.CmdType) { case QcProtocol.QcCommand.QcCheckLicense: try { LicenseRetCode ret = LicenseRetCode.未授权; //CONTINUE: // if (cmd == null) goto CONTINUE; string type = cmd.tokens(1); string arg = cmd.tokens(2); switch (type) { case "Feature": int id = 0; int.TryParse(arg, out id); ret = License.CheckFeature(id); break; case "Module": ret = License.CheckModule(arg); break; case "Product": ret = License.CheckProduct(arg); break; case "Regsiter": ret = License.RegsisterUser(arg); break; case "Release": License.ReleaseUser(arg); break; } e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcCheckLicense, ret.ToString())); e.Chanel.CloseLink(); } catch (Exception ex) { QcLog.LogString("QcCheckLicense " + cmd.ToString() + ":" + ex.Message); } break; case QcProtocol.QcCommand.QcUserLogin: bool blLogined = false; blLogined = true; if (lstUser.ContainsKey(from) == false) { var user = QcUser.RefreshUser(from); if (user != null) { var qmu = new QcMsgUser(user); lstUser.TryAdd(qmu.Name, qmu); } } if (lstUser.ContainsKey(from)) { //这里判断一下是否可以登录 var msguser = lstUser[from]; var user = msguser.User as QcUser; string sql = "select * from " + QcUser.TableName + " where " + user.CodeField + "='" + user.Code + "' and 状态='启用'"; if (DbHelper.Exists(sql)) { QcChanel chanel = new QcChanel(); chanel.SetChanel(e.Chanel, this); lstUser[from].Chanel = chanel; if (this.Logined != null) { var userfrom = lstUser[from]; var evtarg = new QcMessagerLoginEventArg(userfrom); this.Logined(this, evtarg); } //this.BroadcastMsg(e.Cmd.ToString()); this.BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, from, "*")); blLogined = true; } else { blLogined = false; } QcClientService qcs = e.Chanel as QcClientService; e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginReplay, blLogined)); } break; case QcProtocol.QcCommand.QcListUser: var strusers = ""; foreach (var u in OnlineUsers) { strusers += u.Name + ","; } e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcListUser, strusers)); break; case QcProtocol.QcCommand.QcLoginOut: if (lstUser.ContainsKey(from)) { var userfrom = lstUser[from]; if (this.Loginout != null) { var evt = new QcMessagerLoginEventArg(userfrom); this.Loginout(this, evt); } this.BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, from, "*")); userfrom.Close(); } break; case QcProtocol.QcCommand.QcDataUpdate: this.BroadcastMsg(e.Cmd.ToString()); break; default: if (to == "*") { this.BroadcastMsg(e.Cmd.ToString()); return; } else { if (lstUser.ContainsKey(to)) { var user = lstUser[to]; user.Send(e.Cmd.ToString()); } } break; } } }
void QcMessagner_ReciveCmd(object sender, QcCmdEventArgs e) { try { if (e.Cmd.CmdType == QcProtocol.QcCommand.Undefine) { return; } QcMsgUser userfrom = null; if (lstUser.ContainsKey(e.Cmd.From)) { userfrom = lstUser[e.Cmd.From]; } if (this.ReciveCmd != null) { var cmdevt = new QcMessagerCmdEventArg() { cmd = e.Cmd, user = userfrom }; this.ReciveCmd(this, cmdevt); } switch (e.Cmd.CmdType) { case QcProtocol.QcCommand.QcLoginReplay: client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcListUser)); break; case QcProtocol.QcCommand.QcLoginOut: if (userfrom != null) { userfrom.Chanel = null; userfrom.IsLinked = false; } if (this.Loginout != null) { if (userfrom != null) { this.Loginout(this, new QcMessagerLoginEventArg(userfrom)); } } break; case QcProtocol.QcCommand.QcMsg: if (this.ReciveMsg != null) { var msgevt = new QcMessagerMsgEventArg(userfrom.Name, e.Cmd.tokens(3), e.Cmd.tokens(4)); this.ReciveMsg(this, msgevt); } break; case QcProtocol.QcCommand.QcSystemMsg: if (this.ReciveSystemMsg != null) { var systemevt = new QcSystemMsgEventArg(e.Cmd.tokens(5), e.Cmd.From, e.Cmd.tokens(3), e.Cmd.tokens(4)); this.ReciveSystemMsg(this, systemevt); } break; case QcProtocol.QcCommand.QcUserLogin: foreach (QcMsgUser u in lstUser.Values) { if (u.Name == userfrom.Name) { u.IsLinked = true; break; } } if (this.Logined != null) { this.Logined(this, new QcMessagerLoginEventArg(userfrom)); } break; case QcProtocol.QcCommand.QcListUser: var v = e.Cmd.tokens(1).Split(','); foreach (var u in v) { if (lstUser.ContainsKey(u)) { lstUser[u].Chanel = new QcChanel(); } } break; case QcProtocol.QcCommand.QcUpdateMsg: if (ReciveUpdateMsg != null) { var msg = QcMsg.GetMsg(e.Cmd.tokens(4)); ReciveUpdateMsg(this, new QcMessagerUpdateMsgEventArg(msg)); } break; case QcProtocol.QcCommand.QcDataUpdate: string index = e.Cmd.tokens(2) + "|" + e.Cmd.tokens(1); //收到数据更新消息,进行更新 // QcLog.LogString(e.Cmd.ToString()); NodeChangeType type = NodeChangeType.Create; if (Enum.TryParse <NodeChangeType>(e.Cmd.tokens(3), out type)) { switch (type) { case NodeChangeType.Create: break; case NodeChangeType.Update: if (QcNode.lstNode.ContainsKey(index)) { QcNode.lstNode[index].Refresh(); } break; case NodeChangeType.Delete: if (QcNode.lstNode.ContainsKey(index)) { QcNode.lstNode[index].RiseDelete(); } break; } if (DataUpdate != null) { if (QcNode.lstNode.ContainsKey(index)) { DataUpdate(this, new QcMessagerDataUpdateEventArg(QcNode.lstNode[index], type)); } else { string tablename = e.Cmd.tokens(2); string code = e.Cmd.tokens(1); string codefield = e.Cmd.tokens(4); QcNode node = new QcNewNode(tablename, code, codefield); node.InitFromString(e.Cmd.tokens(5)); DataUpdate(this, new QcMessagerDataUpdateEventArg(node, type)); } } } break; } } catch (Exception ex) { QcLog.LogException("QcMessagner:", ex); } }