/// <summary> /// Logs the receipt of a notification for timing and keepalive. /// Returns false iff the notification is too old and should be rejected. /// </summary> internal bool OnReceiveNotification(long curTime, ushort notificationSeq) { // Reject it if it's too old, including statistics for it if (NetUtil.UShortSeqDiff(notificationSeq, this.NotificationAck) <= 0) { return(false); } this.notificationAck = notificationSeq; this.lastPacketRecvTime = curTime; this.lastNotificationRecvTime = curTime; return(true); }
/// <summary> /// Cleans out any notifications older than the received carrier ack. /// </summary> internal void OnReceiveCarrier(long curTime, ushort notificationAck, Action <NetEvent> deallocate) { traffic.OnReceiveOther(curTime); while (outgoing.Count > 0) { var front = outgoing.Peek(); if (NetUtil.UShortSeqDiff(notificationAck, front.Sequence) < 0) { break; } deallocate.Invoke(outgoing.Dequeue()); } }
/// <summary> /// Notifies a peer that we are disconnecting. May not arrive. /// </summary> internal SocketError SendKick(NetPeer peer, NetCloseReason reason, byte userReason = 0) { // Skip the packet if it's a bad reason (this will cause error output) if (NetUtil.ValidateKickReason(reason) == NetCloseReason.INVALID) { return(SocketError.Success); } lock (sendLock) { var length = NetEncoding.PackProtocol(sendBuffer, NetPacketType.Kick, (byte)reason, userReason); return(TrySend(peer.EndPoint, sendBuffer, length)); } }
/// <summary> /// Sends a kick (reject) message to an unconnected peer. /// </summary> internal SocketError SendReject( IPEndPoint destination, NetCloseReason reason) { // Skip the packet if it's a bad reason (this will cause error output) if (NetUtil.ValidateKickReason(reason) == NetCloseReason.INVALID) { return(SocketError.Success); } lock (this.sendLock) { int length = NetEncoding.PackProtocol( this.sendBuffer, NetPacketType.Kick, (byte)reason, 0); return(this.TrySend(destination, this.sendBuffer, length)); } }
private void HandleKick( NetPeer peer, byte[] buffer, int length) { if (peer.IsClosed) { return; } byte rawReason; byte userReason; bool success = NetEncoding.ReadProtocol( buffer, length, out rawReason, out userReason); // Validate if (success == false) { NetDebug.LogError("Error reading kick"); return; } NetCloseReason closeReason = (NetCloseReason)rawReason; // Skip the packet if it's a bad reason (this will cause error output) if (NetUtil.ValidateKickReason(closeReason) == NetCloseReason.INVALID) { return; } peer.OnReceiveOther(this.Time); this.ClosePeerSilent(peer); this.eventOut.Enqueue( this.CreateClosedEvent(peer, closeReason, userReason)); }
/// <summary> /// Logs the sequence in the accumulator. /// </summary> public void Store(ushort sequence) { int difference = NetUtil.UShortSeqDiff(this.latestSequence, sequence); if (difference == 0) { return; } if (difference >= NetTraffic.LOSS_BITS) { return; } if (difference > 0) { this.SetBit(difference); return; } this.Shift(-difference); this.latestSequence = sequence; this.data[0] |= 1; }