public void Update() { try { var polled = false; // poll loop to retrieve more than one data per frame while (!polled) { if (_host.CheckEvents(out var netEvent) <= 0) { if (_host.Service(0, out netEvent) <= 0) { break; } polled = true; } var data = new ServerEventData(netEvent.ChannelID, netEvent.Peer.ID, netEvent.Peer.IP); switch (netEvent.Type) { case EventType.Connect: AddPeer(netEvent.Peer); OnConnect?.Invoke(data); NetworkRouter.PeerConnection(data.PeerId); break; case EventType.Disconnect: RemovePeer(netEvent.Peer); OnDisconnect?.Invoke(data); NetworkRouter.PeerDisconnection(data.PeerId); break; case EventType.Timeout: RemovePeer(netEvent.Peer); OnTimeout?.Invoke(data); NetworkRouter.PeerTimeout(data.PeerId); break; case EventType.Receive: var buffer = new byte[netEvent.Packet.Length]; netEvent.Packet.CopyTo(buffer); netEvent.Packet.Dispose(); data.Content = buffer; OnReceive?.Invoke(data); NetworkRouter.PeerReceive(data.PeerId, data.ChannelId, data.Content); break; } } } catch (Exception e) { UnityEngine.Debug.Log("ERROR Update : " + e); } }
public void Update() { if (!_host.IsSet) { return; } var polled = false; // poll loop to retrieve more than one data per frame while (!polled) { if (_host.CheckEvents(out var netEvent) <= 0) { if (_host.Service(0, out netEvent) <= 0) { break; } polled = true; } var data = new ClientEventData(netEvent.ChannelID, netEvent.Peer.ID); switch (netEvent.Type) { case EventType.Connect: OnConnect?.Invoke(data); NetworkRouter.PeerConnection(data.PeerId); break; case EventType.Disconnect: OnDisconnect?.Invoke(data); NetworkRouter.PeerDisconnection(data.PeerId); break; case EventType.Timeout: OnTimeout?.Invoke(data); NetworkRouter.PeerTimeout(data.PeerId); break; case EventType.Receive: // Get the byte[] from the netEvent.Packet var buffer = new byte[netEvent.Packet.Length]; netEvent.Packet.CopyTo(buffer); netEvent.Packet.Dispose(); data.Content = buffer; OnReceive?.Invoke(data); NetworkRouter.PeerReceive(data.PeerId, data.ChannelId, data.Content); break; } } }