Пример #1
0
 private void Read()
 {
     if (receivingClient.Available > 0 && receivingClient != null)
     {
         byte[] receiveBytes = receivingClient.Receive(ref receiveAddress);
         // Decode packet
         string decodedString = Encoding.ASCII.GetString(receiveBytes);
         OnPacketRecieved?.Invoke(decodedString);
     }
 }
Пример #2
0
        /// <summary>
        /// Async callback for data receiving
        /// </summary>
        /// <param name="asyncResult"></param>
        private void ReceiveCallback(IAsyncResult asyncResult)
        {
            // Invokes when packet is received
            // End receiving packet
            byte[] receiveBytes = receivingClient.EndReceive(asyncResult, ref address);
            // Decode packet
            string decodedString = Encoding.ASCII.GetString(receiveBytes);

            Debug.Log(decodedString);
            OnPacketRecieved?.Invoke(decodedString);
            // Recursion
            receivingClient.BeginReceive(new AsyncCallback(ReceiveCallback), receivingClient);
        }
Пример #3
0
        private void RecieveCallback(IAsyncResult result)
        {
            int length = stream.EndRead(result);

            if (length <= 0)
            {
                return;
            }
            byte[] data = new byte[length];
            Array.Copy(recieveBuffer, data, length);
            int    encodedMessage = BitConverter.ToInt32(data, 0);
            string decoded        = Encoding.ASCII.GetString(data);

            OnPacketRecieved?.Invoke(decoded);
            stream.BeginRead(recieveBuffer, 0, Manager.RecievePacketSize, RecieveCallback, null);
        }