示例#1
0
        private void UdpApmReceive(NeutronPlayer player)
        {
            if (player.TokenSource.Token.IsCancellationRequested)
            {
                return; //* Check if the token is cancelled.
            }
            SocketHelper.BeginReadBytes(player.UdpClient, player.StateObject, (ar) =>
            {
                EndPoint remoteEp = player.StateObject.NonAllocEndPoint;                       //* Get the remote end point, prevent GC pressure/allocations.

                int bytesRead = SocketHelper.EndReadBytes(player.UdpClient, ref remoteEp, ar); //* End the read.
                if (!player.StateObject.UdpIsReady())
                {
                    player.StateObject.UdpRemoteEndPoint = (IPEndPoint)remoteEp; //* Set the remote end point.
                }
                if (bytesRead > 0)
                {
                    player.StateObject.SlicedDatagram = new byte[bytesRead];                                                   //* Create the datagram.
                    Buffer.BlockCopy(player.StateObject.ReceivedDatagram, 0, player.StateObject.SlicedDatagram, 0, bytesRead); //* Copy the received bytes to the datagram.
                    CreateUdpPacket(player);                                                                                   //* Create the packet.
                }

                if (!NeutronModule.IsUnityThread)
                {
                    UdpApmReceive(player); //* Receive again.
                }
            });
        }