async void Tick()
        {
            byte[] tick = new RouteHeader()
            {
                header = Header.TICK
            }.ToByteArray();

            while (true)
            {
                try
                {
                    //if (!isRunTick)
                    //{
                    //    await Task.Delay(10);
                    //    continue;
                    //}
                    //Console.WriteLine("send tick");
                    socket.SendTo(tick, remoteEnd);
                    await Task.Delay(100);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
Beispiel #2
0
        public async void Recv()
        {
            if (isRun)
            {
                try
                {
                    await Task.Factory.StartNew(() =>
                    {
                        if (socket.Available <= 0)
                        {
                            Thread.Sleep(1);
                            Recv();
                            return;
                        }
                        byte[] buffer = new byte[1024 * 16];
                        EndPoint endp = new IPEndPoint(IPAddress.Any, 0);
                        int n         = socket.ReceiveFrom(buffer, headOffset, buffer.Length - headOffset, SocketFlags.None, ref endp);
                        Recv();
                        //Console.WriteLine($"RouteServer:: Recv { Encoding.ASCII.GetString(buffer, 0, n)} remote = {endp} ");
                        if (endp.eq(serverPoint))
                        {
                            int idx = headOffset;

                            if (buffer.TryGet(ref idx, out RouteHeader header))
                            {
                                //Console.WriteLine("header " + header.header.opcode);
                                if (Header.TICK == header.header)
                                {
                                    tickCount = 0;
                                    //Interlocked.Exchange(ref tickCount, 0);
                                }
                                else if (Header.MSG == header.header)
                                {
                                    tickCount = 0;
                                    // Interlocked.Exchange(ref tickCount, 0);
                                    if (id2client.TryGetValue(header.cid, out var point))
                                    {
                                        socket.SendTo(buffer, idx, n - headOffset, SocketFlags.None, point);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (!client2id.TryGetValue(endp, out var cid))
                            {
                                cid = seed++;
                                client2id.Add(endp, cid);
                                id2client.Add(cid, endp);
                            }
                            //Console.WriteLine($"RouteServer orgion:: {buffer.ToHexStr(0, n)}");
                            var header = new RouteHeader()
                            {
                                header = Header.MSG, cid = cid
                            };
                            int id = 0;
                            buffer.TrySet(ref id, header);
                            //Console.WriteLine($"RouteServer after:: {buffer.ToHexStr(0, n)}");
                            socket.SendTo(buffer, 0, n + headOffset, SocketFlags.None, serverPoint);
                        }
                    }).ConfigureAwait(false);
                }
                catch (SocketException se)
                {
                    if (se.SocketErrorCode == SocketError.ConnectionReset)
                    {
                        //continue;
                    }
                    if (se.SocketErrorCode == SocketError.Interrupted)
                    {
                        isRun = false;
                        //continue;
                    }
                    Console.WriteLine(se);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            else
            {
                Console.WriteLine("RouteServer:: Over");
                socket.Close();
                socket.Dispose();
            }
        }