/// <summary> /// 检查客户端连接 /// </summary> private void CheckCon() { while (true) { if (DTU_ClientManager.Clients != null) { int len = DTU_ClientManager.Clients.Count; for (int i = len - 1; i >= 0; i--) { using (MyLock mylock = new MyLock(DTU_ClientManager.Clients[i], 5000, false)) { if (mylock.IsTimeout == false) { try { int n = DTU_ClientManager.Clients[i].socket.Send(new byte[] { 1 }, SocketFlags.None); } catch (Exception e) { DTU_ClientManager.DeleteClient(DTU_ClientManager.Clients[i]); } } } } } Thread.Sleep(20000);// } }
public static void HandlerData(Socket clientSocket, byte[] content, int conLen, byte type, string tel) { if (type == HD_DTU.DTU_REGISTER_DATA) { DTU_ClientManager.AddClient(tel, clientSocket); ResponseRegister(clientSocket, tel); } }
public static void HandlerData(Socket clientSocket, byte[] content, int conLen, byte type, string tel) { if (type == HD_DTU.DTU_REGISTER_DATA) { if (Response_Reigster(clientSocket, tel)) { DTU_ClientManager.AddClient(tel, clientSocket); } else { LogMg.AddDebug("tel=" + tel + " 响应客户端注册数据失败"); } } }
/// <summary> /// 处理接收到的数据 /// </summary> /// <param name="asyncResult"></param> public static void HandlerData(Socket socket, byte[] content, int conLen, byte type, string tel) { try { if (type == ZG_DTU.HEARTBEAT_DATA) //心跳数据 { HeartbeatDataHandler(socket); } else if (type == ZG_DTU.REGISTER_DATA) //注册数据 貌似这里好像不会出现注册数据了,因为注册数据在第一次注册后就不会再注册了 { DTU_ClientManager.AddClient(tel, socket); } else if (type == ZG_DTU.USER_DATA) { } } catch (Exception ex) { //LogManager.Add("json字符串:" + data + System.Environment.NewLine + ex.ToString()); LogMg.AddError(ex); DEBUG.ThrowException(ex); } }
/// <summary> /// 客户端注册 /// </summary> /// <param name="socket"></param> /// <param name="pack"></param> /// <param name="length"></param> public static void RegisterDataHandler(Socket socket, string tel) { LogMg.AddDebug(string.Format("进入客户端注册方法RegisterDataHandler Tel:{0}", tel)); try { // string stationName = DTU_ClientManager.GetStationNameByTel(tel); // if (string.IsNullOrEmpty(stationName)) // { // MessageQueue.Enqueue_RegAndLogout(string.Format("【{0}】: {1}登录系统", DateTime.Now.ToString(), "未知的客户端")); // } // else // { // MessageQueue.Enqueue_RegAndLogout(string.Format("【{0}】: {1}登录系统", DateTime.Now.ToString(), stationName)); // } DTU_ClientManager.AddClient(tel, socket); } catch (Exception ex) { LogMg.AddError(ex); } LogMg.AddDebug(string.Format("退出客户端注册方法RegisterDataHandler Tel:{0}", tel)); }
/// <summary> /// 拍照 /// </summary> /// <param name="client"></param> /// <param name="timeout"></param> public static bool TakePhoto(DTUClientInfo client, int timeout, ref byte[] imageBytes) { //这个方法主要包含三个步骤, //第一步:发送拍照指令 //第二步:发送“获取照片包数量”的指令 //第三步:循环去读取每一个照片包内容 byte[] rubbish = new byte[JBT_DTU.ReceiveLength]; byte packcount = 0x00; int n = 1; //当发送的命令没接收到,这个变量就会+1,直到他等于5 int commandLen1 = 0; byte[] command1 = GetTakePhotoCommand(ref commandLen1); try { client.socket.Receive(rubbish, JBT_DTU.ReceiveLength, SocketFlags.None); } catch (Exception) { } //第一步:发送拍照指令 JBT_DTU.SendUserData_Assistant(client.socket, command1, commandLen1, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[JBT_DTU.ReceiveLength]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(3000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (conLen != 7) { n++; continue; } break; } catch (SocketException ex) { n++; LogMg.AddError(ex); } catch (Exception ex) { n++; LogMg.AddError(ex); } } DTU_ClientManager.UpdateLastVisitTime(client, DateTime.Now); if (n > 5) { return(false); //如果读取5次都读不到数据,就表示这次拍照失败 } LogMg.AddError("拍照命令 拍照次数=" + n); n = 1; int commandLen2 = 0; //第二步:发送“获取照片包数量”的指令 byte[] command2 = GetPicPackCountCommand(ref commandLen2); JBT_DTU.SendUserData_Assistant(client.socket, command2, commandLen2, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[4096]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(1000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (conLen != 8) { n++; continue; } packcount = content[4]; break; } catch (SocketException ex) { n++; LogMg.AddError(ex); } catch (Exception ex) { n++; LogMg.AddError(ex); } } if (n > 5) { return(false); } LogMg.AddError("获取照片包数量 n=" + n); imageBytes = new byte[packcount * 1000]; int picIndex = 0; //第三步:循环去读取每一个照片包内容 for (byte i = 0x00; i < packcount; i++) { n = 1; int commandLen3 = 0; byte[] command3 = GetReadPictureCommand(i, ref commandLen3); JBT_DTU.SendUserData_Assistant(client.socket, command3, commandLen3, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[4096]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(1000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (content[0] != HEAD || content[1007] != HEAD) // { n++; continue; } if (content[04] != i) //如果包号不对 { n++; continue; } Buffer.BlockCopy(content, 5, imageBytes, picIndex, 1000); picIndex += 1000; break; } catch (SocketException ex) { n++; LogMg.AddError(ex); LogMg.AddError("循环读取包 n=" + n); } catch (Exception ex) { n++; LogMg.AddError(ex); } } if (n > 5) { return(false); } } //ImageUtil.SaveFormBytes(imageBytes, GetImageLen(imageBytes)); //len = GetImageLen(imageBytes); return(true); }