//处理服务器数据的线程 public static void HandleMessage() { while (socketRun) { string jsonStr = ""; lock (receiveMessageQueue) { if (receiveMessageQueue.Count > 0) { jsonStr = receiveMessageQueue.Dequeue(); } } //将收到的数据分配给各个线程进行处理 if (jsonStr.Equals("") == false) { RSData receiveRsData = Newtonsoft.Json.JsonConvert.DeserializeObject <RSData>(jsonStr); /* if (receiveRsData.CommType.Equals(CommUtil.COMM_TYPE_DOWN_ORDER)) * { * // * } * } * else Thread.Sleep(2000);*/ } } }
public static void sendRSDataProc(RSData data) { //listenSocket.Connect(new IPEndPoint(IPAddress.Parse(svr_ip), svr_port)); //Form1 tmp = (Form1)f; while (GlobalVarForApp.networkStatusBool) // listenSocket is volid ?? { /* messageCount = listenSocket.Receive(messageBuf); * message += u8.GetString(messageBuf); * int a = message.IndexOf("DataEnd"); * if (a != -1) * { * try * { * GlobalVarForApp.receiveMessageQueue.Enqueue(JsonConvert.DeserializeObject<RSData>(message.Substring(0, a))); * message = message.Substring(a + 7); * tmp.messageHandle(); * } * catch (Exception e) * { * MessageBox.Show(e.Message); * } * }*/ } return; }
//发送客户端数据的线程 public static void SendMessage() { while (socketRun) { RSData rsData = null; lock (sendMessageQueue) { if (sendMessageQueue.Count > 0) { rsData = sendMessageQueue.Dequeue(); } } if (rsData != null) { string sendStr = Newtonsoft.Json.JsonConvert.SerializeObject(rsData) + "DataEnd"; //获取字符长度 int m_length = sendStr.Length; byte[] data = new byte[m_length]; //转为字节流 data = Encoding.UTF8.GetBytes(sendStr); try { //将字节流发给服务端 int i = client.Send(data); } catch (Exception ex) { if (client != null && client.Connected) { client.Shutdown(SocketShutdown.Both); client.Close(); Console.WriteLine("与服务器断开连接"); break; } Console.WriteLine(ex.Message); } } else { Thread.Sleep(2000); } } if (socketRun && connectThread.ThreadState != ThreadState.Running) { connectThread.Resume();//启动连接服务器的线程 } }
//消息处理函数 public void messageHandle() { RSData tmp = new RSData(); while (GlobalVarForApp.receiveMessageQueue.Count > 0) //队列中有消息进行处理 { //foreach( RSData rsdData in receiveQueue) //{ // //"LOGIN_REPLY" "ADD_USER_REPLY" "DELETE_USER_REPLY" //"DOWN_ORDER" "QUERY_ORDER_REPLY" "NEW_MESSAGE" tmp = GlobalVarForApp.receiveMessageQueue.Dequeue(); switch (tmp.CommType.Trim()) { case "LOGIN_REPLY": break; case "ADD_USER_REPLY": break; case "DELETE_USER_REPLY": break; case "DOWN_ORDER": OrderInfo tmpOrInfo = new OrderInfo(); //调度令信息 //提取调度令信息 tmpOrInfo.orInfo = tmp.order; tmpOrInfo.orRc = tmp.orderRecordList; tmpOrInfo.orOp = tmp.orderOpList; tmpOrInfo.orderStatus_enum = orderStatus.unconfirmed; //设置调度令状态信息 未接收确认状态 GlobalVarForApp.tbdOrdersInfo.Add(tmpOrInfo); //添加到调度令信息 //对orInfo全局变量按调度令号进行排序 if (GlobalVarForApp.tbdOrdersInfo.Count > 1) { GlobalVarForApp.tbdOrdersInfo.Sort(CompareOrderByOrderID); } //od_dis.od_dis_show(); //新调度令显示 tbd_lb.BeginUpdate(); tbd_lb.Items.Clear(); int count = 0; foreach (OrderInfo tmpOrderInfo in GlobalVarForApp.tbdOrdersInfo) { count++; tbd_lb.Items.Add("#" + count.ToString() + "广无调单字【" + tmpOrderInfo.orInfo.ORDER_YEAR + "】" + tmpOrderInfo.orInfo.ORDER_CODE); } tbd_lb.EndUpdate(); // //接收到新调度语音提示 // break; case "QUERY_ORDER_REPLY": break; case "NEW_MESSAGE": break; default: /* 可选的 */ break; } // } } }