示例#1
0
 public static UdpData Prase(byte[] netBuffer)
 {
     if (netBuffer.Length > 8)
     {
         try
         {
             UdpData data = new UdpData();
             data.HEAD = netBuffer[0];
             data.LEN  = (short)(netBuffer[1] * 255 + netBuffer[2]);
             data.CID  = netBuffer[3];
             data.DID  = netBuffer[4];
             data.CMD  = netBuffer[5];
             data.EXP  = netBuffer[6];
             data.DATA = new byte[netBuffer.Length - 8];
             System.Buffer.BlockCopy(netBuffer, 7, data.DATA, 0, netBuffer.Length - 8);
             data.END = netBuffer[netBuffer.Length - 1];
             return(data);
         }
         catch
         {
             return(null);
         }
     }
     else
     {
         return(null);
     }
 }
 private void ReceiveUDP(object sender, byte[] data, IPEndPoint source)
 {
     if (RemoteEndPoint.Port == source.Port)
     {
         UdpData.Write(new DataWrapper <byte[]>(data));
     }
 }
示例#3
0
        private byte[] Serialyse(UdpData udpData)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, udpData);
                return(stream.ToArray());
            }
        }
示例#4
0
    /// <summary>
    /// This method begins listening for packets from the server.
    /// </summary>
    private void listenForMessage()
    {
        UdpData callbackData = new UdpData();

        callbackData.callbackClient   = udpClientReceiver;
        callbackData.callbackEndpoint = RemoteIpEndPoint;

        //UnityEngine.Debug.Log("Listening");
        udpClientReceiver.BeginReceive(new System.AsyncCallback(OnReceive), callbackData);
    }
 public ICodec this[UdpPacket packet]
 {
     get
     {
         UdpData udpData = !(packet == null) ? packet.Payload as UdpData : throw new ArgumentNullException(nameof(packet));
         if (udpData == null || udpData.Data == null || udpData.Data.Length == 0)
         {
             throw new InvalidOperationException("No data");
         }
         return(this[udpData.Data[0]]);
     }
 }
示例#6
0
        private Queue <UdpData> sendDataList = new Queue <UdpData>(); //发送数据缓冲区


        public void SendData()
        {
            try
            {
                udpSend.Connect(remoteEP);
            }
            catch
            {
                MessageBox.Show("连接错误 " + remoteEP.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            connectSuccess = true;
            byte[] writeValue = new byte[16];
            Byte[] sendBytes  = { 1, 1 };

            while (true)
            {
                if (cts.Token.IsCancellationRequested)
                {
                    break;
                }

                bool sendBuffer = false;
                // 上锁线程安全
                lock (sendDataLock)
                {
                    if (sendDataList.Count > 0) //如果队列里面有需要发送的数据 则发送
                    {
                        UdpData getData = sendDataList.Dequeue();
                        sendBuffer = true;
                        udpSend.BeginSend(getData.buffer, getData.length, new AsyncCallback(SendCallback), udpSendState);
                    }
                }

                if (sendBuffer == false) // 发送读取的命令
                {
                    udpSend.BeginSend(readAgvPlcCommand, 18, readCallback, udpSendState);
                    ReceiveMessages();
                }
                if (sendBuffer == false)
                {
                    Thread.Sleep(4000);
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }
示例#7
0
        public void addSendBuffer(ReadWriteType readWriteType, byte[] data)
        {
            UdpData pData = new UdpData();

            if (readWriteType == ReadWriteType.WRITE_AGV_D600)
            {
                pData.length = CmdOmlonWriteData(pData.buffer, byPlcDot, localDot, 601, 1, data, OMLONADDRESS.D);
            }
            Console.WriteLine(pData.buffer);
            //Queue<T>  不是线程安全的
            lock (sendDataLock)
            {
                sendDataList.Enqueue(pData);
            }
        }
        public PackageInterface ReadUDP()
        {
            if (UdpConnection == null)
            {
                throw new ConnectionException("Network connection does not have an UDP connection!");
            }

            // not threadsafe - one thread can set "Read" after the other thread checks flag here --- not an issue here though as it will just evaluate UDP twice which is... fine
            DataWrapper <byte[]> dataWrapper = dataWrapper = UdpData.Read();

            if (dataWrapper != null && !dataWrapper.Read)
            {
                return(Adapter.CreatePackageFromNetworkData(dataWrapper.Data));
            }

            return(null);
        }
 private void ModelCallback(UdpData udpData)
 {
     Debug.Log("udp receive:" + udpData.Text);
     SetViewMessage(udpData.Text);
     if (udpData.Text == "ready")
     {
         if (_isReady == false)
         {
             _isReady = true;
             _viewManager.Ready();
             ModelSend("ready");
         }
     }
     else if (udpData.Text == "quit")
     {
         _isReady = false;
         _viewManager.StandBy();
     }
 }
示例#10
0
        private void GenerateData()
        {
            Console.WriteLine("генерируем и отправляем данные");
            int value1, value2, value3, value4, value5;

            count   = count == long.MaxValue ? 0 : count + 1;
            value1  = random.Next(0, int.MaxValue);
            value2  = random.Next(0, int.MaxValue);
            value3  = random.Next(0, int.MaxValue);
            value4  = random.Next(0, int.MaxValue);
            value5  = random.Next(0, int.MaxValue);
            udpData = new UdpData(count, value1, value2, value3, value4, value5);
            Console.WriteLine(udpData);
            var bytes = Serialyse(udpData);

            byte[] b = Serialyse(udpData);
            s.Send(b, b.Length, SocketFlags.None);
            workTimeout.Stop();
            workTimeout.Start();
        }
示例#11
0
        public void PollServerForPlayers()
        {
            var endpoint  = new IPEndPoint(new IPAddress(BitConverter.GetBytes(Address)), Port + 1);
            var udpClient = new UdpClient();

            try
            {
                var data = new UdpData {
                    EndPoint = endpoint, Client = udpClient
                };
                udpClient.Connect(endpoint);
                udpClient.Send(new byte[] { 00, 00, 00, 01 }, 4);
                udpClient.BeginReceive(ReadReceivedData, data);
            }
            catch (Exception)
            {
                //Log.write(e.ToString());
                udpClient.Close();
                udpClient.Client.Dispose();
            }
        }
        private void Listen(object state)
        {
            UdpData udpData = (UdpData)state;

            while (true)
            {
                if (_stopped)
                {
                    break;
                }

                try
                {
                    IPEndPoint ipe  = null;
                    byte[]     sent = udpData.Client.Receive(ref ipe);
                    base.OnRaisePortRequestReceived(sent, ipe.Address.ToString());
                }
                catch (Exception e)
                {
                    Serilog.Log.Error(e, "Error receiving udp message");
                }
            }
        }
示例#13
0
        private void Listen(object state)
        {
            UdpData udpData = (UdpData)state;

            while (true)
            {
                if (_stopped)
                {
                    break;
                }

                try
                {
                    IPEndPoint ipe         = null;
                    byte[]     sent        = udpData.Client.Receive(ref ipe);
                    var        stringValue = Encoding.UTF8.GetString(sent);
                    OnReceivedMessage(ipe, stringValue, udpData.Port);
                }
                catch (Exception e)
                {
                    Serilog.Log.Error(e, "Error receiving udp message");
                }
            }
        }
示例#14
0
        public void AddUdpMessage(byte[] data, string targetIP, int targetPort)
        {
            UdpData udp = new UdpData(data, targetIP, targetPort);

            UdpMessageList.Add(udp);
        }
 private void UdpCallbackHandler(UdpData udpData)
 {
     _callbacks(udpData);
 }