public NetPeer AddConnection(IPEndPoint endpoint, string token) { if (token == null) { token = ""; } if (Encoding.UTF8.GetByteCount(token) > NetConfig.MAX_TOKEN_BYTES) { throw new ApplicationException("Token string too long"); } NetPeer pending = this.controller.BeginConnect(endpoint, token); pending.SetCore(this); return(pending); }
public void PollEvents() { NetEvent evnt; while (this.controller.TryReceiveEvent(out evnt)) { NetPeer peer = evnt.Peer; // No events should fire if the user closed the peer if (peer.ClosedByUser == false) { switch (evnt.EventType) { case NetEventType.PeerConnected: peer.SetCore(this); peer.OnPeerConnected(); this.PeerConnected?.Invoke(peer, peer.Token); break; case NetEventType.PeerClosed: peer.OnPeerClosed(evnt.CloseReason, evnt.UserKickReason, evnt.SocketError); this.PeerClosed?.Invoke(peer, evnt.CloseReason, evnt.UserKickReason, evnt.SocketError); break; case NetEventType.Payload: peer.OnPayloadReceived(evnt.EncodedData, evnt.EncodedLength); this.PeerPayload?.Invoke(peer, evnt.EncodedData, evnt.EncodedLength); break; case NetEventType.Notification: peer.OnNotificationReceived(evnt.EncodedData, evnt.EncodedLength); this.PeerNotification?.Invoke(peer, evnt.EncodedData, evnt.EncodedLength); break; default: throw new NotImplementedException(); } } this.controller.RecycleEvent(evnt); } }