Пример #1
0
 protected override void Deal(SimpleUdpServce socket, EndPoint remote, ReqNat value)
 {
     TestLog.Log($"ReqNatServerDealer recv");
     if (env.value == EnvEnum.Center)
     {
         if (!server.tryGet(value.address, out var endp))
         {
             TestLog.Log($"nat ip::{endp} not found");
             socket.Send(ResNat.NO_SERVER_FOUND, remote);
         }
         else
         {
             TestLog.Log($"server ip::{endp} client id::{remote}");
             var remoteAddr = remote.ValueAddress();
             //if (remoteAddr.ipaddress == value.address.ipaddress)
             //    remoteAddr.ipaddress = ValueAddress.LocalAddr;
             socket.Send(new ReqNat {
                 address = remoteAddr
             }, endp);
         }
     }
     if (env.value == EnvEnum.Server)
     {
         var endp = value.address.toEndPoint();
         TestLog.Log($"nat start watting ip = {endp}");
         //using(Socket natSocket=new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp))
         //{
         //    natSocket.Bind(new IPEndPoint(IPAddress.Any, value.address.port));
         //    natSocket.SendTo(ResNat.SUCCESS.ToByteArray(), endp);
         //}
         //socket.remoteEnd = endp;
         socket.Send(ResNat.SUCCESS, endp);
         TestLog.Log($"nat end");
     }
 }
Пример #2
0
 public async void Start()
 {
     try
     {
         isRun = true;
         byte[] buffer = new byte[1024];
         await Task.Factory.StartNew(() =>
         {
             while (isRun)
             {
                 int idx  = 0;
                 int recv = 0;
                 if (remoteEnd == null)
                 {
                     remoteEnd = new IPEndPoint(IPAddress.Any, 0);
                 }
                 try
                 {
                     recv = socket.ReceiveFrom(buffer, ref remoteEnd);
                 }
                 catch (Exception)
                 {
                     throw;
                 }
                 finally
                 {
                     TestLog.Log($"ddd:{remoteEnd}");
                 }
                 //Console.WriteLine("SimpleUdpServce recv");
                 if (recv < Head.size)
                 {
                     continue;
                 }
                 var head = buffer.Get <Head>(ref idx);
                 //Console.WriteLine($"SimpleUdpServce recv size ok mask ={ head.mark} vailed = {Head.defauleMark}");
                 if (head.mark != Head.defauleMark)
                 {
                     continue;
                 }
                 //Console.WriteLine("SimpleUdpServce recv mask ok");
                 if (Msg.TryGetDealer(head.opcode, out IDealer dealer))
                 {
                     dealer.Deal(this, remoteEnd, buffer, idx);
                 }
                 else
                 {
                     TestLog.Log($"warnning:: msg id = {head.opcode} not found dealer");
                 }
             }
         }, TaskCreationOptions.LongRunning);
     }
     catch (Exception e)
     {
         TestLog.Log($"error:: {e}");
     }
 }
Пример #3
0
 protected override void Deal(SimpleUdpServce socket, EndPoint remote, ResNat value)
 {
     if (value.Equal(ResNat.NO_SERVER_FOUND))
     {
         TestLog.Log("server  not found");
     }
     else if (value.Equal(ResNat.SUCCESS))
     {
         TestLog.Log("nat success");
     }
 }
Пример #4
0
        protected override void Deal(SimpleUdpServce socket, EndPoint remote, ReqAsServer value)
        {
            var v = remote.ValueAddress();

            if (!server.Contains(v))
            {
                server.Add(v, remote);
            }
            TestLog.Log($"server ip::{remote}");
            socket.Send(new ResAsServer()
            {
                address = v
            }, remote);
        }
Пример #5
0
 protected override void Deal(SimpleUdpServce socket, EndPoint remote, ResAsServer value)
 {
     TestLog.Log($"outer ip::{value.address.toEndPoint()}");
 }