ManageTcp.cs
6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace XingYe_ACS.Common
{
public class ManageTcp
{
/// <summary>
/// 服务侦听
/// </summary>
/// <param name="backlog">挂起的连接请求</param>
/// <param name="svrSocket">服务套接字</param>
public static void ServerListen(int backlog, Socket svrSocket)
{
svrSocket.Listen(backlog);//开始监听
while (true)
{
//服务器监听,建立socket
Socket ClientSocket = svrSocket.Accept();
ClientSocket.ReceiveTimeout = 3000;
//创建线程
Thread thSocket = new Thread(new ParameterizedThreadStart(DealAgvMsg));
////线程启动,参数为socket
thSocket.Start(ClientSocket);
}
}
/// <summary>
/// 处理AGV消息
/// </summary>
/// <param name="clientSocket">客户端对象</param>
public static void DealAgvMsg(Object clientSocket)
{
Socket socket = clientSocket as Socket;
string address = "";
Config_ClientControl clientControl = null;
try
{
byte[] ReceiveData = new byte[1024];
socket.ReceiveTimeout = 3000;
if (socket.Poll(3000000, SelectMode.SelectRead))
{
int DataLength = socket.Receive(ReceiveData);
if (DataLength > 0)
{
SocketOpt socketOpt = new SocketOpt(socket, ReceiveData);
if (socketOpt != null)
{
//判断该客户端是否还在执行操作
address = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
clientControl = App.ClientWork.Find(a => a.strClientIp == address);
if (clientControl == null)
{
Config_ClientControl client = new Config_ClientControl();
client.strClientIp = address;
client.isDoing = false;
App.ClientWork.Add(client);
}
clientControl = App.ClientWork.Find(a => a.strClientIp == address);
if (!clientControl.isDoing)
{
clientControl.isDoing = true;
MsgRecevie.TcpTranslate(socketOpt);
if (socketOpt.sendData != null)
Send(socketOpt);
clientControl.isDoing = false;
}
}
Thread.Sleep(20);
}
}
}
catch (Exception Ex)
{
App.ExFile.MessageError("DealAgvMsg", "==" + address + "==" + Ex.StackTrace);
if (clientControl != null)
clientControl.isDoing = false;
}
finally
{
socket.Close();
}
}
/// <summary>
/// Tcp指令发送
/// </summary>
/// <param name="clientSocket">自定义Socket对象</param>
/// <returns>发送结果</returns>
public static void Send(SocketOpt clientSocket)
{
Socket getSocket = clientSocket.clientSocket;
string address = ((IPEndPoint)getSocket.RemoteEndPoint).Address.ToString();
try
{
byte[] dataRecv = clientSocket.recData;
string strAddress = Encoding.ASCII.GetString(dataRecv, 3, 10).Replace("\0", "");
getSocket.Send(clientSocket.sendData, clientSocket.sendData.Length, 0);
getSocket.Close();
if (App.offAgvNo.Count > 0 && App.offAgvNo.Contains(strAddress) && clientSocket.sendData[5] == 21)
{
Agv agv = App.AgvList.Find(a => a.strAgvNo == strAddress);
agv.isOnline = false;
App.offAgvNo.Remove(strAddress);
}
}
catch (Exception Ex)
{
throw new Exception(address + "==Send:" + clientSocket.sendData.Length + Ex.StackTrace);
}
}
static object obj = new object();
/// <summary>
/// 发送接收电梯数据
/// </summary>
/// <param name="sendDate">发送数据</param>
/// <param name="IP">电梯Ip地址</param>
/// <returns></returns>
public static LiftControl SendLift(byte[] sendDate, string IP)
{
byte[] receiveDate = new byte[11];
LiftControl liftControl = new LiftControl();
try
{
//建立socket短连接
Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(IP);
IPEndPoint point = new IPEndPoint(ip, 888);
socketSend.Connect(point);
//发送数据
socketSend.Send(sendDate);
//接收数据
socketSend.Receive(receiveDate);
liftControl.address = ((IPEndPoint)socketSend.RemoteEndPoint).Address.ToString();
liftControl.receiveDate = receiveDate;
//关闭连接
socketSend.Close();
//返回数据
return liftControl;
}
catch (Exception Ex)
{
App.ExFile.MessageError("BusinessException", "连接电梯异常:" + Ex.StackTrace);
return null;
}
}
}
}