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; } } }
LicenseRetCode PostLicenseMessage(string type, string arg) { int count = 3; QcMsgClient client = new QcMsgClient(null, null); AutoResetEvent are = new AutoResetEvent(false); QcCmd retcmd = null; string cmd = QcCmd.MakeCmd(QcProtocol.QcCommand.QcCheckLicense, type, arg); try { //client =; client.ConnectedServer += (o, e) => { client.Send(cmd); }; client.ReceiveCmd += (o, e) => { retcmd = e.Cmd; are.Set(); //接收消息 }; CONTINUE: client.Connect(QcMessagner.IP, QcMessagner.Port); if (are.WaitOne(5000))//3秒钟超时连接 { LicenseRetCode ret = LicenseRetCode.授权未知错误01; //if (retcmd == null) //{ // Thread.Sleep(500);//有可能消息接收 事件还没有给retcmd赋值,因此延迟500毫秒 //} if (retcmd != null) { string t_Test = retcmd.tokens(1); ret = (LicenseRetCode)Enum.Parse(typeof(LicenseRetCode), t_Test); } return(ret); } else { if (count > 0) { count--; goto CONTINUE; } else { return(LicenseRetCode.授权服务器连接超时); } } } catch (Exception e) { QcLog.LogString(e.Message + e.StackTrace); //System.Windows.Forms.MessageBox.Show(e.Message + e.StackTrace); return(LicenseRetCode.授权未知错误); } finally { //这里该怎样断开服务器上的连接呢? if (client != null) { client.Close(); } are.Set(); } }