public static T RequestSendReceive <T>(this RequestSocket socket, IdKeyData ikd) { try { lock (socket) { socket.SendMoreFrame(ikd.Id).SendMoreFrame(ikd.Key).SendFrame(ikd.Data); NetMQMessage msg = null; #if DEBUG1 msg = socket.ReceiveMultipartMessage(); // debug模式打开,不会超时 #else socket.TryReceiveMultipartMessage(TimeSpan.FromSeconds(10), ref msg); #endif if (msg != null) { var result = JsonUtil.Deserialize <T>(msg[0].Buffer); return(result); } else { throw new Exception("中心服务器请求失败!"); } } } catch (Exception ex) { socket = new RequestSocket(); socket.Connect(MQConfig.ResponseServer); LogUtil.Warn("RequestSendReceive 异常:" + ex.StackTrace); return(default(T)); } }
/// <summary> /// Router模式,会自动路由到Controler /// </summary> public void ThreadMQRouterReceive() { try { using (RouterSocket routerSocket = new RouterSocket()) { routerSocket.Bind(MQConfig.RouterReceiveServer); while (!_cancellationTokenSource.IsCancellationRequested) { try { IdKeyData receiveData = routerSocket.RouterReceive(); Task.Factory.StartNew(() => { RouteConfig.Instance.ExecCmd(receiveData.Id, receiveData.Key, receiveData.Data); }); } catch (TerminatingException) { } catch (Exception ex) { MQThreadExceptionHandler?.Invoke(ex); } } } } catch (Exception ex) { MQThreadExceptionHandler?.Invoke(ex); } }
/// <summary> /// 连接中心服务器 /// </summary> /// <returns></returns> private bool ConnectCenterServer() { try { PublicData.RequestSocket = new RequestSocket(); PublicData.RequestSocket.Connect(MQConfig.ResponseServer); MQActionResult <string> result = PublicData.RequestSocket.RequestSendReceive <MQActionResult <string> >( IdKeyData.Create("", "Session/Connect", new ValueTypeParam <ClientTypeEnum>(ClientTypeEnum.Bet) { Platform = BetPlatformEnum.QXDFV168 })); if (null != result && result.IsOK) { PublicData.SessionId = result.Data; Log("连接中心服务器成功。"); return(true); } else { MessageBoxEx.Alert("中心服务器连接失败,信息:" + result.ErrorMsg); Log("中心服务器连接失败。"); return(false); } } catch (Exception ex) { MessageBoxEx.Alert(ex.Message); return(false); } }
public static IdKeyData ResponseReceive(this ResponseSocket socket) { IdKeyData kd = new IdKeyData(); NetMQMessage msg = socket.ReceiveMultipartMessage(); kd.Id = msg[0].ConvertToString(); kd.Key = msg[1].ConvertToString(); kd.Data = msg[2].Buffer; return(kd); }
public static IdKeyData RouterReceive(this RouterSocket socket) { IdKeyData ikd = new IdKeyData(); NetMQMessage msg = socket.ReceiveMultipartMessage(); ikd.Id = msg[0].ConvertToString(); //socket.Options.Identity ikd.Key = msg[2].ConvertToString(); byte[] zipData = msg[3].Buffer; ikd.Data = GZipUtil.Decompress(zipData); return(ikd); }
/// <summary> /// 获取出货信息 /// </summary> private void ThreadBetInformation() { while (!PublicData.CancellationToken.IsCancellationRequested) { try { MQActionResult <BetPoolItem> result = PublicData.RequestSocket.RequestSendReceive <MQActionResult <BetPoolItem> >( IdKeyData.Create(PublicData.SessionId, "BetPool/GetBetInPool", PublicData.SessionId)); if (result.IsOK) { BetPoolItem betResult = result.Data; //添加到处理池 Task.Run(() => { BetProcess bet = new BetProcess(); bet.Id = betResult.Id; bet.Locked = false; bet.Money = betResult.Money; bet.Number = betResult.Number; bet.OperateID = betResult.OperateID; bet.Order = betResult.Order; bet.BuyerSessionID = PublicData.SessionId; bet.ClientSessionID = betResult.ClientSessionID; BetProcessPool.AddBetInPool(bet); }); } else { LogError(Environment.NewLine + "获取下注信息失败:" + result.ErrorMsg); } } catch (Exception ex) { LogError(Environment.NewLine + "获取下注信息异常:" + Environment.NewLine + ex.StackTrace); } Thread.Sleep(200); //200毫秒获取一次 } }
/// <summary> /// 发送心跳线程 /// </summary> private void ThreadHeartbeat() { int heartbeatErrorCount = 0; while (!PublicData.CancellationToken.IsCancellationRequested && heartbeatErrorCount < 5) { try { MQActionVoidResult result = PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>( IdKeyData.Create(PublicData.SessionId, "Session/Heartbeat", new HeartbeatParam() { ClientType = ClientTypeEnum.Bet })); if (result.IsOK) { heartbeatErrorCount = 0; } else { heartbeatErrorCount++; LogError(Environment.NewLine + "投注服务端心跳失败,错误信息:" + result.ErrorMsg); } } catch (Exception ex) { heartbeatErrorCount++; LogError(Environment.NewLine + "Request发送心跳异常,错误信息:" + Environment.NewLine + ex.StackTrace); } Thread.Sleep(TimeSpan.FromSeconds(2)); } if (heartbeatErrorCount >= 5) { _mustClose = true; this.Close(); } }
/// <summary> /// 发送心跳线程,保持与中心服务器连接 /// </summary> private void ThreadHeartbeat() { int heartbeatErrorCount = 0; while (!PublicData.CancellationToken.IsCancellationRequested && heartbeatErrorCount < 5) { try { MQActionVoidResult result = PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>( IdKeyData.Create(PublicData.SessionId, "Session/Heartbeat", new HeartbeatParam() { ClientType = ClientTypeEnum.Client })); if (result.IsOK) { heartbeatErrorCount = 0; } else { heartbeatErrorCount++; LogUtil.Warn(Environment.NewLine + "控盘端心跳失败,错误信息:" + result.ErrorMsg); } } catch (Exception ex) { heartbeatErrorCount++; LogUtil.Warn(Environment.NewLine + "控盘端Request发送心跳异常,错误信息:" + Environment.NewLine + ex.StackTrace); } Thread.Sleep(TimeSpan.FromSeconds(2)); } if (heartbeatErrorCount >= 5) { MessageBoxEx.Alert("您已掉线,请关闭重新启动!"); } }
/// <summary> /// 开始执行扫水操作 /// </summary> /// <param name="betNo"></param> private void SendScanWaterToCenterServer(string betNo) { //扫水 //PublicData.RequestSocket = new RequestSocket(); //PublicData.RequestSocket.Connect(MQConfig.ResponseServer); var data = new Collect() { Number = betNo, ClientSessionID = PublicData.SessionId, }; var result = PublicData.RequestSocket.RequestSendReceive <MQActionResult <string> >( IdKeyData.Create(PublicData.SessionId, "Collect/SendCollect", data)); if (result == null) { MessageBoxEx.Alert("连接服务器失败,请检查网络或联系管理员!"); return; } else if (result.IsOK) { //命令发送成功 } }
/// <summary> /// Response模式,会自动路由到Controler /// </summary> public void ThreadMQResponse() { try { using (ResponseSocket responseSocket = new ResponseSocket()) { responseSocket.Bind(MQConfig.ResponseServer); while (!_cancellationTokenSource.IsCancellationRequested) { try { IdKeyData receiveData = responseSocket.ResponseReceive(); // request模式也需要异步处理,因为有很多个系统用户,会产生并发 Task.Factory.StartNew(() => { string result = RouteConfig.Instance.ExecCmd(receiveData.Id, receiveData.Key, receiveData.Data); responseSocket.ResonseSend(result); }); } catch (TerminatingException) { } catch (Exception ex) { MQThreadExceptionHandler?.Invoke(ex); } } } } catch (Exception ex) { MQThreadExceptionHandler?.Invoke(ex); } }
private void Uninstall() { PublicData.CancellationToken.Cancel(); UnbindMqEvents(); while (PublicData.DealerSendQueue.Count > 0) { Thread.Sleep(500); } // 断开服务器 try { if (!string.IsNullOrEmpty(PublicData.SessionId)) { PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>( IdKeyData.Create(PublicData.SessionId, "Session/Disconnect", new ValueTypeParam <ClientTypeEnum>(ClientTypeEnum.Bet))); } } catch { // ignored } }
/// <summary> /// 扫水线程 /// </summary> private void BetCollect() { var betProc = BetProcessPool.GetBetProcess(); if (null != betProc) { Collect col = new Collect(); col.Id = betProc.Id; col.BuyerSessionID = PublicData.SessionId; col.ClientSessionID = betProc.ClientSessionID; col.Number = betProc.Number; MQActionVoidResult result = PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>(IdKeyData.Create(PublicData.SessionId, "Collect/PushCollect", col)); if (null != result && result.IsOK) { //记录命令发送成功 } else { //记录命令发送失败 } } }
/// <summary> /// 处理下单结果 /// </summary> /// <param name="content"></param> private void BetContentEvent(BetContentResult content) { //收到下注结果 if (null != content) { BetInformation bet = new BetInformation(); bet.BetAccount = content.BetAccount; bet.BetMoney = content.BetMoney; bet.BetPlatform = content.BetPlatformEnum; bet.BetSessionID = content.BetSessionID; bet.DeleteOrderID = content.DeleteOrderID; bet.Number = content.Number; bet.Odds = content.Odds; bet.OperateTime = content.BetTime; bet.OrderID = content.OrderID; bet.Money = content.Money; //符合赔率 if (bet.Odds >= 9000) { bet.Status = ResultStatus.Success; } else { //退码 bet.Status = ResultStatus.Deleting; content.ResultStatus = ResultStatus.Deleting; MQActionVoidResult result = PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>(IdKeyData.Create(PublicData.SessionId, "Bet/DeleteBet", content)); if (null != result && result.IsOK) { //记录命令发送成功 } else { //记录命令发送失败 } } var count = BetProcessPool.SetBetProcessBetContent(content.Id, bet); if (count == 0) { //下单完成 可能存在已经退水了的 } } }
/// <summary> /// 收到扫水结果 /// </summary> /// <param name="content">扫水结果</param> private void CollectResultEvent(CollectResult content) { CollectInformation ci = new CollectInformation(); ci.CollectPlatform = content.Platform; ci.CollectSessionID = content.CollectSessionID; ci.MaxBetMoney = content.MaxBetMoney; ci.Number = content.Number; var count = BetProcessPool.SetBetProcessCollect(content.Id, ci); //如果扫水完成 if (count == 0) { var colls = BetProcessPool.GetCollectByID(content.Id); if (null != colls) { //分析扫水结果 //->暂缺 //开始下注 foreach (CollectInformation item in colls) { //设置分配好的下注平台 BetProcessPool.AddBetListItemInBetProcess(content.Id, item.CollectPlatform); Task.Run(() => { BetContent bet = new BetContent(); bet.Id = content.Id; bet.BetPlatformEnum = item.CollectPlatform; bet.BuyerSessionID = content.BuyerSessionID; bet.ClientSessionID = content.ClientSessionID; bet.InputType = 1; //号码类型 bet.Money = 1; // 分配的金额 bet.Number = item.Number; MQActionVoidResult result = PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>(IdKeyData.Create(PublicData.SessionId, "Bet/AddBet", bet)); if (null != result && result.IsOK) { //记录命令发送成功 } else { //记录命令发送失败 } }); } } } }
/// <summary> /// 发送登录请求 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, EventArgs e) { // 校验 if (string.IsNullOrWhiteSpace(txtAccount.Text)) { MessageBox.Show("请输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(txtPwd.Text)) { MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var loginParam = new SysMemberLoginParam() { UserName = txtAccount.Text.Trim(), Password = txtPwd.Text.Trim() }; //启动与中心服务器的连接 PublicData.RequestSocket = new RequestSocket(); PublicData.RequestSocket.Connect(MQConfig.ResponseServer); var result = PublicData.RequestSocket.RequestSendReceive <MQActionResult <string> >( IdKeyData.Create(PublicData.SessionId, "SysMember/Login", loginParam)); if (result == null) { MessageBoxEx.Alert("连接服务器失败,请检查网络或联系管理员!"); return; } else if (result.IsOK) { PublicData.SessionId = result.Data; PublicData.SysmemberAccount = loginParam.UserName; Visible = false; using (var formLogin = new MainForm() { StartPosition = FormStartPosition.CenterScreen, TopMost = true }) { //暂时采用模式调试 formLogin.Show(); //var diaResult = formLogin.ShowDialog(); //if (diaResult == DialogResult.Cancel) //{ // Close(); // return; //} //else if (formLogin.ShowDialog() == DialogResult.Ignore)//重新登录 //{ // Visible = true; // return; //} } } else { MessageBoxEx.Alert(result.ErrorMsg); txtPwd.Focus(); return; } }
private void CollectProcess(Collect collect, bool isClient) { CollectResult coll = new CollectResult(); coll.ErrorMessage = ""; coll.MaxBetMoney = 300; coll.Odds = 7000; coll.ResultState = ResultStatus.Success; //var result = adp.SeekWater(content.Number); //if (result != null) //{ // coll.ErrorMessage = ""; // coll.MaxBetMoney = result.Amount; // coll.Odds = result.Odds; // coll.ResultState = ResultStatus.Success; //} //else //{ // coll.ErrorMessage = "查询赔率失败"; // coll.MaxBetMoney = 0; // coll.Odds = 0; // coll.ResultState = ResultStatus.Failure; //} coll.Id = collect.Id; coll.BuyerSessionID = collect.BuyerSessionID; coll.ClientSessionID = collect.ClientSessionID; coll.Domain = "www.dfv168.com"; coll.Number = collect.Number; var rs = PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>(IdKeyData.Create(PublicData.SessionId, "Collect/CollectCompletedToClient", coll)); //IList<Task> taskList = new List<Task>(); //foreach (var item in PublicData.CurrentPlatforms) //{ // var task = Task.Run(() => // { // if (item.Value != null) // { // //初始化登录 // if (item.Value.LoginToken == null) // { // item.Value.Login(new Model.Web.UserLogin() { Domain = "www.dfv168.com", LoginName = "kf001", Password = "******" }); // // .Initialize(new LoginModel() { Loginname = "kf001", Password = "******" }, false); // } // var query = item.Value.CollectNumber(collect); // PublicData.RequestSocket.RequestSendReceive<MQActionVoidResult>(IdKeyData.Create(PublicData.SessionId, "Collect/CollectCompletedToClient", query)); // if (query != null) // { // query.CollectSessionID = PublicData.SessionId; // MQActionVoidResult result; // if (isClient) // { // result = PublicData.RequestSocket.RequestSendReceive<MQActionVoidResult>(IdKeyData.Create(PublicData.SessionId, "Collect/CollectCompletedToClient", query)); // } // else // { // result = PublicData.RequestSocket.RequestSendReceive<MQActionVoidResult>(IdKeyData.Create(PublicData.SessionId, "Collect/CollectCompleted", query)); // } // if (result != null && result.IsOK) // { // //记录成功 // } // else // { // //记录失败 // } // } // } // }); // taskList.Add(task); //} //Task.WaitAll(taskList.ToArray()); //string xxx = ""; }
public static void RouterSend(this RouterSocket socket, IdKeyData ikd) { socket.SendMoreFrame(ikd.Id).SendMoreFrameEmpty() .SendMoreFrame(ikd.Key).SendFrame(ikd.Data); ; }