/// <summary> /// Attempts to send data to endpoint via OS socket. /// Returns false if the send failed. /// </summary> internal SocketError TrySend( IPEndPoint destination, byte[] buffer, int length) { try { int bytesSent = this.rawSocket.SendTo( buffer, length, SocketFlags.None, destination); if (bytesSent == length) { return(SocketError.Success); } return(SocketError.MessageSize); } catch (SocketException exception) { NetDebug.LogError("Send failed: " + exception.Message); NetDebug.LogError(exception.StackTrace); return(exception.SocketErrorCode); } }
private void HandlePong( NetPeer peer, byte[] buffer, int length) { if (peer.IsConnected == false) { return; } bool success = NetEncoding.ReadProtocol( buffer, length, out byte pongSeq, out byte drop); // Validate if (success == false) { NetDebug.LogError("Error reading pong"); return; } peer.OnReceivePong(Time, pongSeq, drop); }
/// <summary> /// Validates that a given kick reason is acceptable for a remote kick. /// </summary> internal static NetCloseReason ValidateKickReason(NetCloseReason reason) { switch (reason) { case NetCloseReason.RejectNotHost: return(reason); case NetCloseReason.RejectFull: return(reason); case NetCloseReason.RejectVersion: return(reason); case NetCloseReason.KickTimeout: return(reason); case NetCloseReason.KickShutdown: return(reason); case NetCloseReason.KickError: return(reason); case NetCloseReason.KickUserReason: return(reason); } NetDebug.LogError("Bad kick reason: " + reason); return(NetCloseReason.INVALID); }
private void HandlePayload( NetPeer peer, byte[] buffer, int length) { if (peer.IsConnected == false) { return; } // Read the payload bool success = NetEncoding.ReadPayload( CreateEvent, peer, buffer, length, out ushort payloadSeq, out NetEvent evnt); // Validate if (success == false) { NetDebug.LogError("Error reading payload"); return; } // Enqueue the event for processing if the peer can receive it if (peer.OnReceivePayload(Time, payloadSeq)) { eventOut.Enqueue(evnt); } }
/// <summary> /// Handles an incoming connection request from a remote peer. /// </summary> private void HandleConnectRequest( IPEndPoint source, byte[] buffer, int length) { bool success = NetEncoding.ReadConnectRequest( buffer, out string version, out string token); // Validate if (success == false) { NetDebug.LogError("Error reading connect request"); return; } if (ShouldCreatePeer(source, version)) { long curTime = Time; // Create and add the new peer as a client NetPeer peer = new NetPeer(source, token, true, curTime); peers.Add(source, peer); peer.OnReceiveOther(curTime); // Accept the connection over the network sender.SendAccept(peer); // Queue the event out to the main thread to receive the connection eventOut.Enqueue( CreateEvent(NetEventType.PeerConnected, peer)); } }
private void HandlePing( NetPeer peer, byte[] buffer, int length) { if (peer.IsConnected == false) { return; } bool success = NetEncoding.ReadProtocol( buffer, length, out byte pingSeq, out byte loss); // Validate if (success == false) { NetDebug.LogError("Error reading ping"); return; } peer.OnReceivePing(Time, loss); sender.SendPong(peer, pingSeq, peer.GenerateDrop()); }
/// <summary> /// Attempts to read from OS socket. Returns false if the read fails /// or if there is nothing to read. /// </summary> internal SocketError TryReceive(out IPEndPoint source, byte[] destBuffer, out int length) { source = null; length = 0; lock (readLock) { if (rawSocket.Poll(0, SelectMode.SelectRead) == false) { return(SocketError.NoData); } try { EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); length = rawSocket.ReceiveFrom(destBuffer, destBuffer.Length, SocketFlags.None, ref endPoint); if (length > 0) { source = endPoint as IPEndPoint; return(SocketError.Success); } return(SocketError.NoData); } catch (SocketException exception) { NetDebug.LogError("Receive failed: " + exception.Message); NetDebug.LogError(exception.StackTrace); return(exception.SocketErrorCode); } } }
public static void Assert(bool condition) { if (condition == false) { NetDebug.LogError("Assert Failed!"); } }
public static void Assert(bool condition, object message) { if (condition == false) { NetDebug.LogError("Assert Failed: " + message); } }
/// <summary> /// Queues a new notification to be send out reliably during ticks. /// </summary> internal bool QueueNotification(NetEvent data) { var notificationCount = outgoing.Count; if (notificationCount >= NetConfig.MaxPendingNotifications) { NetDebug.LogError("Notification queue full"); return(false); } data.Sequence = notificationSeq++; outgoing.Enqueue(data); return(true); }
private void HandleCarrier( NetPeer peer, byte[] buffer, int length) { if (peer.IsConnected == false) { return; } // Read the carrier and notifications ushort notificationAck; ushort notificationSeq; this.reusableQueue.Clear(); bool success = NetEncoding.ReadCarrier( this.CreateEvent, peer, buffer, length, out notificationAck, out notificationSeq, this.reusableQueue); // Validate if (success == false) { NetDebug.LogError("Error reading carrier"); return; } long curTime = this.Time; peer.OnReceiveCarrier(curTime, notificationAck, this.RecycleEvent); // The packet contains the first sequence number. All subsequent // notifications have sequence numbers in order, so we just increment. foreach (NetEvent notification in this.reusableQueue) { if (peer.OnReceiveNotification(curTime, notificationSeq++)) { this.eventOut.Enqueue(notification); } } }
internal bool ReadData(byte[] sourceBuffer, int position, ushort length) { if (length > NetConfig.DATA_MAXIMUM) { NetDebug.LogError("Data too long for NetEvent"); return(false); } // Resize if necessary int paddedLength = length + NetConfig.DATA_PADDING; if (this.buffer.Length < paddedLength) { this.buffer = new byte[paddedLength]; } // Copy the contents Array.Copy(sourceBuffer, position, this.buffer, 0, length); this.length = length; return(true); }
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)); }