ManageTcp.cs 6.07 KB
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;
            }
        }
    }
}