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> /// Packs a connect request with version and token strings. /// </summary> internal static int PackConnectRequest( byte[] buffer, string version, string token) { int versionBytes = Encoding.UTF8.GetByteCount(version); int tokenBytes = Encoding.UTF8.GetByteCount(token); NetDebug.Assert((byte)versionBytes == versionBytes); NetDebug.Assert((byte)tokenBytes == tokenBytes); // Pack header info buffer[0] = (byte)NetPacketType.Connect; buffer[1] = (byte)versionBytes; buffer[2] = (byte)tokenBytes; int position = NetEncoding.CONNECT_HEADER_SIZE; Encoding.UTF8.GetBytes(version, 0, version.Length, buffer, position); position += versionBytes; Encoding.UTF8.GetBytes(token, 0, token.Length, buffer, position); position += tokenBytes; return(position); }
/// <summary> /// Handles an incoming connection request from a remote peer. /// </summary> private void HandleConnectRequest( IPEndPoint source, byte[] buffer, int length) { string version; string token; bool success = NetEncoding.ReadConnectRequest( buffer, out version, out token); // Validate if (success == false) { NetDebug.LogError("Error reading connect request"); return; } if (this.ShouldCreatePeer(source, version)) { long curTime = this.Time; // Create and add the new peer as a client NetPeer peer = new NetPeer(source, token, true, curTime); this.peers.Add(source, peer); peer.OnReceiveOther(curTime); // Accept the connection over the network this.sender.SendAccept(peer); // Queue the event out to the main thread to receive the connection this.eventOut.Enqueue( this.CreateEvent(NetEventType.PeerConnected, peer)); } }