public Task <UdpResult> OnceRun(EndPoint end) { var Sock = world.Globel.GetComponent <SocketComponent>(); if (end == null) { throw new Exception("can not recv from a null endpoint"); } if (Sock.socket == null) { throw new Exception("you socket net inited ,please use Behavior\"UdpInit\" to init it!"); } return(Task.Factory.StartNew(() => { byte[] buffer = unSafeByteHelper.Take(1024); int n = Sock.socket.ReceiveFrom(buffer, ref end); ByteStream stream = ByteStream.Take(); stream.Set(buffer, 0, n); UdpResult result = new UdpResult() { endPoint = (IPEndPoint)end, stream = stream }; unSafeByteHelper.Return(buffer); return result; }, Sock.cancellation.Token)); }
public override Task Run(UdpResult result, ConnectorsComponent connectors, UdpSender sender) { uint eid; connectors.dictionary.TryGetValue(result.endPoint, out eid); connectors.conTcs.SetResult(0); world.Get <Entity>(eid)?.RemoveComponent <ConTimeCounter>(); world.RemoveBehavior <TimeCount>(); return(Task.CompletedTask); }
public override Task Run(UdpResult result, ConnectorsComponent connectors, UdpSender sender) { return(Task.Factory.StartNew(() => { uint eid; if (connectors.dictionary.TryGetValue(result.endPoint, out eid)) { world.RemoveEntity(eid); connectors.dictionary.Remove(result.endPoint); sender.Send(result.endPoint, ConDefine.disconnected); } })); }
public override Task Run(UdpResult result, ConnectorsComponent connectors, UdpSender sender) { return(Task.Factory.StartNew(() => { if (!connectors.dictionary.ContainsKey(result.endPoint)) { Entity e = world.Add <Entity>(); e.AddComponent <EndPointComponent>().remote = result.endPoint; connectors.dictionary.Add(result.endPoint, e.id); System.Console.WriteLine($"Accept::Run::ID={e.id}"); sender.Send(result.endPoint, ConDefine.connected); } })); }
public override Task Run(UdpResult result, ConnectorsComponent connectors, UdpSender sender) { return(Task.Factory.StartNew(() => { uint eid; if (connectors.dictionary.TryGetValue(result.endPoint, out eid)) { ushort func = result.stream.GetUShort(); IPaylodable paylodable = MsgMaper.Get(func); if (paylodable == null) { return; } paylodable.GetFrom(result.stream); APDealer dealer = world.GetBehavior <APDealer>(MsgMaper.Get(paylodable.GetType().Name)); dealer?.Deal(eid, paylodable); ByteStream.Return(result.stream as ByteStream); } })); }
public async void Run(EndPoint end = null) { if (end == null) { end = new IPEndPoint(IPAddress.Any, 0); } var Sock = world.Globel.GetComponent <SocketComponent>(); var Pid = world.Globel.GetComponent <PIDComponent>(); var con = world.Globel.GetComponent <ConnectorsComponent>(); var sender = world.GetBehavior <UdpSender>(); while (!Sock.cancellation.Token.IsCancellationRequested) { try { UdpResult result = await OnceRun(end); uint pid = result.stream.GetUInt(); if (Pid.Pid != pid) { continue; } string dealer; if (con.dealers.TryGetValue(result.stream.GetByte(), out dealer)) { await world.GetBehavior <ConDealer>(dealer)?.Run(result, con, sender); } } catch (System.Net.Sockets.SocketException e) { if (e.ErrorCode == 10054) { continue; } } catch (Exception e) { Console.WriteLine(e); } } }
public abstract Task Run(UdpResult result, ConnectorsComponent connectors, UdpSender sender);