Example #1
0
        static async Task test_udpa()
        {
            UdpAccel a = new UdpAccel(IPAddress.Any, false);

            Console.ReadLine();

            a.Dispose();

            await Task.Yield();
        }
Example #2
0
        static async Task nb_socket_udp_proc()
        {
            UdpClient uc = new UdpClient(AddressFamily.InterNetwork);

            uc.Client.Bind(new IPEndPoint(IPAddress.Any, 0));
            Console.WriteLine($"port: {((IPEndPoint)uc.Client.LocalEndPoint).Port}");

            IPAddress server_ip = IPAddress.Parse("130.158.6.60");

            using (NonBlockSocket b = new NonBlockSocket(new PalSocket(uc.Client)))
            {
                long next_send = 0;
                while (b.IsDisconnected == false)
                {
                    long now = Time.Tick64;
                    if (next_send == 0 || next_send <= now)
                    {
                        next_send = now + 500;

                        lock (b.SendUdpQueue)
                        {
                            b.SendUdpQueue.Enqueue(new Datagram(new byte[] { (byte)'B' }, new IPEndPoint(server_ip, 5004)));
                        }

                        b.EventSendNow.Set();
                    }

                    lock (b.RecvUdpQueue)
                    {
                        while (b.RecvUdpQueue.Count >= 1)
                        {
                            Datagram pkt = b.RecvUdpQueue.Dequeue();
                            Dbg.Where($"recv: {pkt.Data.Length} {pkt.IPEndPoint}");

                            string tmp = Encoding.ASCII.GetString(pkt.Data.Span);
                            var    ep  = UdpAccel.ParseIPAndPortStr(tmp);
                            Console.WriteLine(ep);
                        }
                    }

                    await WebSocketHelper.WaitObjectsAsync(
                        cancels : new CancellationToken[] { b.CancelToken },
                        events : new AsyncAutoResetEvent[] { b.EventRecvReady, b.EventSendReady },
                        timeout : (int)(next_send - now));

                    Dbg.Where();
                }
                Dbg.Where("Disconnected.");
            }
        }
Example #3
0
        static async Task pipe_socket_udp_proc()
        {
            UdpClient uc = new UdpClient(AddressFamily.InterNetwork);

            uc.Client.Bind(new IPEndPoint(IPAddress.Any, 0));
            Console.WriteLine($"port: {((IPEndPoint)uc.Client.LocalEndPoint).Port}");

            IPAddress server_ip = IPAddress.Parse("130.158.6.60");

            AsyncCleanuperLady lady = new AsyncCleanuperLady();

            try
            {
                FastPipe pipe   = new FastPipe(lady);
                var      reader = pipe.B_UpperSide.DatagramReader;
                var      writer = pipe.B_UpperSide.DatagramWriter;

                using (FastPipeEndSocketWrapper w = new FastPipeEndSocketWrapper(lady, pipe.A_LowerSide, new PalSocket(uc.Client)))
                {
                    try
                    {
                        long next_send = 0;

                        FastPipeNonblockStateHelper helper = new FastPipeNonblockStateHelper(reader, writer);

                        long now = FastTick64.Now;
                        while (true)
                        {
                            if (next_send == 0 || next_send <= now)
                            {
                                next_send = now + 500;

                                lock (writer.LockObj)
                                {
                                    writer.Enqueue(new Datagram(new byte[] { (byte)'B' }, new IPEndPoint(server_ip, 5004)));
                                }

                                writer.CompleteWrite();
                            }

                            List <Datagram> pkts;

                            lock (reader.LockObj)
                            {
                                pkts = reader.DequeueAll(out _);
                            }
                            reader.CompleteRead();

                            foreach (var pkt in pkts)
                            {
                                //Dbg.Where($"recv: {pkt.Data.Length} {pkt.IPEndPoint}");

                                string tmp = Encoding.ASCII.GetString(pkt.Data.Span);
                                var    ep  = UdpAccel.ParseIPAndPortStr(tmp);
                                //Console.WriteLine(ep);
                                writer.Enqueue(pkt);
                                writer.CompleteWrite();
                            }

                            if (await helper.WaitIfNothingChanged((int)(next_send - now)))
                            {
                                now = FastTick64.Now;
                            }

                            //Dbg.Where();
                        }
                        Dbg.Where("Disconnected.");
                    }
                    finally
                    {
                        await w.AsyncCleanuper;
                    }
                }
            }
            finally
            {
                await lady;
            }
        }