private void btnResult_Click(object sender, EventArgs e) { #region 客户端发送结果 ShowResult("发送结果"); //发送ready int msgType = MessageType.ClientSendResult; MsgRequestContract contract = new MsgRequestContract("", ""); contract.Key = Guid.NewGuid().ToString(); byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 this.tcpPassiveEngine.PostMessageToServer(reqMessage); #endregion }
private void btnReadOne_Click(object sender, EventArgs e) { //发送readdy请求 string code = tbCode.Text.Trim(); if (string.IsNullOrWhiteSpace(code)) { MessageBox.Show("请输入编码"); return; } ShowResult("发送Ready请求"); //客户端发送ready命令 int msgType = NewMessageType.ClientSendReady; MsgRequestContract contract = new MsgRequestContract("", ""); contract.Key = Guid.NewGuid().ToString(); contract.DeviceCode = code;//设备编码 byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 this.tcpPassiveEngine.PostMessageToServer(reqMessage); }
private void button1_Click_2(object sender, EventArgs e) { if (lvIps.SelectedItems.Count == 0) { return; } string ipInfo = lvIps.SelectedItems[0].SubItems[2].Text; string ip = ipInfo.Split(':')[0]; string curPort = ipInfo.Split(':')[1]; IPEndPoint client = new IPEndPoint(IPAddress.Parse(ip), int.Parse(curPort));; ClientSendInfo sendInfo = new ClientSendInfo(); sendInfo.IP = ip; sendInfo.SendBarCodeTime = DateTime.Now; sendInfo.IpPoint = client; //var curDevInfoResult = AppInfo.ClientInfoList.Where(p => p.IP == sendInfo.IP); //if (curDevInfoResult != null && curDevInfoResult.FirstOrDefault() != null) //{ // sendInfo.Name = curDevInfoResult.FirstOrDefault().Name; // sendInfo.Code = curDevInfoResult.FirstOrDefault().Code; //} //sendInfo.BarCode = "12345678"; //clientSendList.Add(sendInfo); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = ""; response.Msg = "12345678"; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ServerPlaceDevice); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); }
void tcpPassiveEngine_MessageReceived(System.Net.IPEndPoint serverIPE, byte[] bMsg) { //获取消息类型 int msgType = BitConverter.ToInt32(bMsg, 4);//消息类型是 从offset=4处开始 的一个整数 if (msgType == MessageType.ServerResponseBarCode) { #region 收到服务端返回的条码 MsgResponseContract response = (MsgResponseContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string result = "收到服务端条码:" + response.Msg; this.ShowResult(result); //客户端发送给服务端收到请求 result = "发送收到请求给服务端"; this.ShowResult(result); msgType = MessageType.ClientSendReceive; MsgRequestContract contract = new MsgRequestContract("", ""); contract.Key = Guid.NewGuid().ToString(); byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 this.tcpPassiveEngine.PostMessageToServer(reqMessage); #endregion } if (msgType == MessageType.SeverReceiveResult) { #region 收到服务端返回的收到结果请求 string result = "客户端收到服务端返回结果"; this.ShowResult(result); #endregion } }
/// <summary> /// 抓走并放置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnZhuanAndPlace_Click(object sender, EventArgs e) { if (lvIps.SelectedItems.Count == 0) { return; } string ipInfo = lvIps.SelectedItems[0].SubItems[2].Text; string ip = ipInfo.Split(':')[0]; string curPort = ipInfo.Split(':')[1]; IPEndPoint client = new IPEndPoint(IPAddress.Parse(ip), int.Parse(curPort));; ClientSendInfo sendInfo = new ClientSendInfo(); sendInfo.IP = ip; sendInfo.SendBarCodeTime = DateTime.Now; sendInfo.IpPoint = client; //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = ""; response.Msg = ""; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ServerTakeDeviceAndPlaceDevice); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); }
private void button1_Click(object sender, EventArgs e) { ShowResult("发送Ready请求"); //发送ready int msgType = MessageType.ClientSendReady; MsgRequestContract contract = new MsgRequestContract("", "MC9K-TR-FCT-1,M2MB999999990,1,M009287,MC9K-TR,,OK,"); contract.Key = Guid.NewGuid().ToString(); byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 }
private string u1_SendMessageEvent(int type, string barCode, string ip) { if (SystemInfo.ClientInfoList.Count <= 0) { return(""); } IPEndPoint client = SystemInfo.ClientInfoList.Where(p => p.Ip == ip).FirstOrDefault().IpPoint; // ShowMsghomeControl("向client" + client.Address.ToString() + "发送:" + type + "--" + barCode); if (type == 8) { #region 机械手放置设备 ShowMsghomeControl("向client" + client.Address.ToString() + "发送放置:ServerPlaceDevice" + type + "--" + barCode); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = ""; response.Msg = barCode; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ServerPlaceDevice); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); foreach (var item in SystemInfo.ClientInfoList) { if (item.Ip == client.Address.ToString()) { item.State = 2; } } #endregion } if (type == 11) { #region 取走设备 ShowMsghomeControl("向client" + client.Address.ToString() + "发送取走:ServerTakeDevice" + type + "--" + barCode); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = ""; response.Msg = ""; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ServerTakeDevice); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.SendMessageToClient(client, resMessage); #endregion } if (type == 12) { #region 取走设备 ShowMsghomeControl("向client" + client.Address.ToString() + "发送取走并放置:ServerTakeDeviceAndPlaceDevice" + type + "--" + barCode); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = ""; response.Msg = barCode; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ServerTakeDeviceAndPlaceDevice); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.SendMessageToClient(client, resMessage); #endregion } return(""); }
void tcpServerEngine_MessageReceived(IPEndPoint client, byte[] bMsg) { //获取消息类型 int msgType = BitConverter.ToInt32(bMsg, 4);//消息类型是 从offset=4处开始 的一个整数 if (msgType == NewMessageType.ClientSendReady) { #region 客服端发送ready请求,服务端收到ready请求 //改变客户端状态 var clientResult = SystemInfo.ClientInfoList.Where(p => p.Ip == client.Address.ToString()); foreach (var item in SystemInfo.ClientInfoList) { if (item.Ip == client.Address.ToString()) { item.State = 1; } } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendReady的请求"); MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ReceiveClientReady); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); ShowMsghomeControl("向客户端回复" + client.Address.ToString() + "ReceiveClientReady的请求"); #endregion } if (msgType == NewMessageType.ClientSendTestFinished) { #region 客户端发送测试完成 MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendTestFinished的请求," + request.Msg); foreach (var item in SystemInfo.ClientInfoList) { if (item.Ip == client.Address.ToString()) { item.State = 3; } } ClientRquestInfo requestInfo = new ClientRquestInfo(); requestInfo.endPoint = client; requestInfo.messageType = msgType; SystemInfo.clientRequestCQ.Enqueue(requestInfo); //服务端收到客户端测试完成请求 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ServerReceiveTestFinished); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); MessageResult messageResult = new MessageResult(); if (request.Msg.Split(',')[1].ToUpper() == "PASS") { //放置到good区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } else { //放置到Ng区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } var clientResult = SystemInfo.ClientInfoList.Where(p => p.Ip == client.Address.ToString()); if (clientResult != null && clientResult.FirstOrDefault() != null) { clientResult.FirstOrDefault().MsgResult = messageResult; } ShowMsghomeControl("向客户端回复" + client.Address.ToString() + "ServerReceiveTestFinished的请求"); #endregion } if (msgType == NewMessageType.ClientSendResult) { #region 务端收到客户端发送结果 MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendResult的请求," + request.Msg); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendResult的请求," + request.Msg); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.SeverReceiveResult); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); // ShowMsg("收到客户端测试结果:ClientSendResult:" + request.Msg); MessageResult messageResult = new MessageResult(); if (request.Msg.Split(',')[1].ToUpper() == "PASS") { //放置到good区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } else { //放置到Ng区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } var clientResult = SystemInfo.ClientInfoList.Where(p => p.Ip == client.Address.ToString()); if (clientResult != null && clientResult.FirstOrDefault() != null) { clientResult.FirstOrDefault().MsgResult = messageResult; } ShowMsghomeControl("向客户端回复:" + client.Address.ToString() + "SeverReceiveResult的请求,"); #endregion } }
void tcpServerEngine_MessageReceived(IPEndPoint client, byte[] bMsg) { //获取消息类型 int msgType = BitConverter.ToInt32(bMsg, 4);//消息类型是 从offset=4处开始 的一个整数 if (msgType == MessageType.ClientSendReady) { #region 收到客户端发送的ready请求,返回条码给客户端 var curDevInfo = clientSendList.Where(p => p.IP == client.Address.ToString()); if (curDevInfo != null && curDevInfo.FirstOrDefault() != null) { clientSendList.Remove(curDevInfo.FirstOrDefault()); } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string record = "收到客户端ready请求:"; this.ShowClientMsg(client, record); record = "返回条码12345678到客户端"; this.ShowClientMsg(client, record); ClientSendInfo sendInfo = new ClientSendInfo(); sendInfo.IP = client.Address.ToString(); sendInfo.SendBarCodeTime = DateTime.Now; sendInfo.IpPoint = client; var curDevInfoResult = AppInfo.ClientInfoList.Where(p => p.IP == sendInfo.IP); if (curDevInfoResult != null && curDevInfoResult.FirstOrDefault() != null) { sendInfo.Name = curDevInfoResult.FirstOrDefault().Name; sendInfo.Code = curDevInfoResult.FirstOrDefault().Code; } sendInfo.BarCode = "12345678"; clientSendList.Add(sendInfo); //回复消息体 MsgResponseContract response = new MsgResponseContract("", "12345678"); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, MessageType.ServerResponseBarCode); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); #endregion } if (msgType == MessageType.ClientSendReceive) { #region 务端收到客户已经接受的请求 var curDevInfo = clientSendList.Where(p => p.IP == client.Address.ToString()); if (curDevInfo != null && curDevInfo.FirstOrDefault() != null) { clientSendList.Remove(curDevInfo.FirstOrDefault()); } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string record = "收到客户端receive请求:"; this.ShowClientMsg(client, record); ClientSendInfo sendInfo = new ClientSendInfo(); sendInfo.IP = client.Address.ToString(); sendInfo.ReceiveTime = DateTime.Now; sendInfo.IpPoint = client; var curDevInfoResult = AppInfo.ClientInfoList.Where(p => p.IP == sendInfo.IP); if (curDevInfoResult != null && curDevInfoResult.FirstOrDefault() != null) { sendInfo.Name = curDevInfoResult.FirstOrDefault().Name; sendInfo.Code = curDevInfoResult.FirstOrDefault().Code; } sendInfo.BarCode = ""; clientSendList.Add(sendInfo); #endregion } if (msgType == MessageType.ClientSendResult) { #region 务端收到客户端发送结果 var curDevInfo = clientSendList.Where(p => p.IP == client.Address.ToString()); if (curDevInfo != null && curDevInfo.FirstOrDefault() != null) { clientSendList.Remove(curDevInfo.FirstOrDefault()); } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string record = "收到客户端发送结果:"; this.ShowClientMsg(client, record); record = "发送收到结果消息给客户端:"; this.ShowClientMsg(client, record); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, MessageType.SeverReceiveResult); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); #endregion } }
private void _timerSendBarCode_Tick(object sender, ElapsedEventArgs e) { try { var curList = clientSendList.Where(p => p.BarCode != "").ToList(); if (curList.Count > 0) { foreach (var item in curList) { int second = ExecDateDiff(DateTime.Now, item.SendBarCodeTime); if (item.TotalSendCount >= 3) { var curResult = clientSendList.Where(p => p.IP == item.IP); if (curResult != null && curResult.FirstOrDefault() != null) { clientSendList.Remove(curResult.FirstOrDefault()); } ClientLogModel logModel = new ClientLogModel(); logModel.Content = "发送次数超超过3次,请检查客户端"; logModel.IP = item.IP; InsertLog(logModel); this.ShowClientMsg(item.IpPoint, logModel.Content); continue; } if (second > 5) { //重新发送条码 string record = "重新发送条码" + item.BarCode + "到客户端"; this.ShowClientMsg(item.IpPoint, record); ClientLogModel logModel = new ClientLogModel(); logModel.Content = record; logModel.IP = item.IP; InsertLog(logModel); //回复消息体 MsgResponseContract response = new MsgResponseContract("", item.BarCode); // response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, MessageType.ServerResponseBarCode); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(item.IpPoint, resMessage); item.SendBarCodeTime = DateTime.Now; item.TotalSendCount += 1; } } } } catch (Exception ex) { LogHelper.Log("_timerSendBarCode_Tick" + ex.Message); } }