void BroadcastLogin() { System.Threading.Tasks.Task.Factory.StartNew(() => { foreach (var v in lstUser) { var qmu = v.Value; var qc = TryConnectUser(qmu); if (qc != null) { var c = new QcChanel(); qmu.Chanel = c; c.SetChanel(qc, qc); c.ReceivedCmd += serverchanel_ReceivedCmd; qc.DisConnectedServer += (o, e) => { if (this.Loginout != null) { var evtarg = new QcMessagerLoginEventArg(qmu); qmu.Chanel = null; this.Loginout(this, evtarg); } }; } } }); }
public void Close() { if (chanel != null) { chanel.Close(); chanel = null; } }
public virtual void Start(ushort port, string ip = "") { foreach (var v in Users) { var qmu = new QcMsgUser(v); lstUser.TryAdd(qmu.Name, qmu); } server = new QcServer(port); server.Start(); server.ReceiveCmd += new QcNetEvent(server_ReceiveCmd); server.ClientClosed += new EventHandler <System.Net.Sockets.SocketAsyncEventArgs>(server_ClientClosed); serverchanel = new QcChanel(); serverchanel.SetChanel(null, server); serverchanel.ReceivedCmd += serverchanel_ReceivedCmd; BroadcastLogin(); }
void server_ReceiveCmd(object sender, QcCmdEventArgs e) { QcCmd cmd = e.Cmd; string from = e.Cmd.tokens(1); switch (cmd.CmdType) { case QcProtocol.QcCommand.QcUserLogin: bool blLogined = false; if (cmd.tokens(2) == User.Name) { blLogined = true; QcChanel chanel = new QcChanel(); chanel.SetChanel(e.Chanel, server); if (lstUser.ContainsKey(from)) { lstUser[from].Chanel = chanel; if (this.Logined != null) { var userfrom = lstUser[from]; var evtarg = new QcMessagerLoginEventArg(userfrom); this.Logined(this, evtarg); } } } QcClientService qcs = e.Chanel as QcClientService; e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginReplay, blLogined)); if (blLogined && from != User.Name) { e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, User.Name, from)); } break; } }