Exemple #1
0
        private async Task ReceiveDataAsync()
        {
            Zephy.Logger.Information("trying to receive UDP BC...");
            UdpReceiveResult receivedResult = await client.ReceiveAsync();

            // start new thread as soon as we have received any data (so we can
            // instantly start handling the next one while this one sends back)
            StartReceive();

            if (Packet.GetPacketType(receivedResult.Buffer) != IdentifyPacket.TYPE)
            {
                return;
            }

            IdentifyPacket recvPacket = new IdentifyPacket(receivedResult.Buffer);

            if (recvPacket.Data.src != "CLIENT")
            {
                return;
            }

            Zephy.Logger.Information($"received IdentifyPacket from Client {receivedResult.RemoteEndPoint.Address}.");

            // Send Packet with identifier "SERVER" back to client
            // to indicate that it was Server instance that sent data back
            IdentifyPacket sendPacket = new IdentifyPacket(new IdentifyPacketData("SERVER"));

            await client.Client.SendToAsync(sendPacket.Buffer, SocketFlags.None, receivedResult.RemoteEndPoint);

            Zephy.Logger.Information($"sending IdentifyPacket from Server back to sender.");
        }