sokect硬件对接协议

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using UnityEngine;
using Frameworks;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public class ISxSinglechipPacket
{
    public byte Head;
    public byte Order;
    public int length;
    public Int16 syringeType;
    public Int16 value;
    public byte HeadEnd;
};




public struct Packet
{
    public byte Head;
    public byte Order;
    public int length;
    public Int16 syringeType;
    public Int16 value;
    public byte HeadEnd;
};


namespace MySocket
{
    public class SimpleSocketClient 
    {
        static SimpleSocketClient _instance;
        public static SimpleSocketClient Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new SimpleSocketClient();
                }
                return _instance;
            }
        }


        string _ip = "192.168.80.7";
        int _port = 20108;


        bool IsConnection { get { return _socket != null && _socket.Connected; } }


        protected int _status = 0;
        protected Socket _socket = null;
        protected int _visteTime = 0;
        protected ISxSinglechipPacket _pak = null;
        protected byte[] _buff = null;
        protected byte[] Data_buff = null;
        protected int _len = 0;
        protected int Data_len = 0;
        protected IntPtr _ptr = IntPtr.Zero;
    


        public void InitiateSocket(string ip, int port)
        {
            _len = Marshal.SizeOf(typeof(ISxSinglechipPacket));
            Data_len = _len * 2;
            _buff = new byte[_len];
            Data_buff = new byte[Data_len];
            _pak = new ISxSinglechipPacket();
            _ptr = Marshal.AllocHGlobal(_len);


            HardwareMsgProxy.instance.RegisterMsg();
        }


        public void ClearSocket()
        {
            if (_socket != null)
            {
                _socket.Close();
                _socket = null;
            }
            if (_ptr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(_ptr);
                _ptr = IntPtr.Zero;
            }
            HardwareMsgProxy.instance.RemoveMsg();
        }






        public void Update()
        {
            
            
            switch (_status)
            {
                case 0:
                    try
                    {
                        _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        SocketAsyncEventArgs saea = new SocketAsyncEventArgs();


                        saea.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(_ip), _port);
                        saea.Completed += new EventHandler<SocketAsyncEventArgs>(SocketCompleted);
                        saea.UserToken = this;


                        _socket.ConnectAsync(saea);
                    }
                    catch
                    {
                        _socket.Close();
                        _socket = null;
                    }
                    _status = 1;
                    _visteTime = Environment.TickCount;
                    break;
                case 1:
                    if (IsConnection)
                    {
                        _status = 2;
                        Debug.Log("SimpleSocketClient 连接成功");
                    }
                    /*
                    else if (Environment.TickCount - _visteTime > _relinkTime)
                    {
                        Debug.LogWarning("SimpleSocketClient 连接超时");
                        _socket.Close();
                        _socket = null;
                        _status = 0;
                    }*/
                    break;
                case 2:
                    if (IsConnection)
                    {
                        try
                        {
                            // 可接收
                            while (_socket.Available >= _len)
                            {
                                int curReceiveLen = _socket.Receive(Data_buff);
                                //Debug.Log(Data_buff.Length);
                                //Debug.Log(curReceiveLen + " [0]" + Data_buff[0] + " [2]" + Data_buff[2]);
                                //return;
                                if (curReceiveLen == _len)
                                {
                                    Array.Copy(Data_buff, 0, _buff, 0, _len);
                                    Marshal.Copy(_buff, 0, _ptr, _len);
                                    Marshal.PtrToStructure(_ptr, _pak);
                                    if (_pak != null)
                                    {
                                        UpdateSinglechip(_pak);
                                    }


                                    _visteTime = Environment.TickCount;
                                }
                                /*
                                else
                                {
                                    Debug.LogError("SimpleSocketClient 数据出错");
                                    _socket.Close();
                                    _socket = null;
                                    _status = 0;
                                    break;
                                }
                                */
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError("SimpleSocketClient 网络出错:" + ex);
                            _socket.Close();
                            _socket = null;
                            _status = 0;
                        }
                    }
                    /*
                    if (Environment.TickCount - _visteTime > _relinkTime)
                    {
                        Debug.LogError("SimpleSocketClient 数据超时");
                        _socket.Close();
                        _socket = null;
                        _status = 0;
                    }
                    */
                    break;
                default:
                    break;
            }


        
        }


        void SocketCompleted(object sender, SocketAsyncEventArgs e)
        {


        }


        Packet packet = new Packet();
        void UpdateSinglechip(ISxSinglechipPacket sp)
        {
            packet.Head = sp.Head;
            packet.Order = sp.Order;
            packet.length = sp.length;
            packet.syringeType = sp.syringeType ;
            packet.value = sp.value;




            //Debug.Log(packet.Head + " " + packet.length  + " " + packet.syringeType + " " + packet.value);
            BaseEvent baseEvt = new BaseEvent(CommonEvent.HardwareState);
            baseEvt.Target = packet;
            HardwareMsgProxy.instance.SendEvents(baseEvt);




        }

猜你喜欢

转载自blog.csdn.net/w87580575/article/details/80620693