3

I know how to play the game privately.

BoltLauncher.StartSinglePlayer

But this time instead of playing the game offline, what I want to know is how to make the room private so no more players can join.

I set IsOpen to false and keep it private when the player intended, but other clients can join the room.

Does anyone know how to set private when the owner of the server intends it?

1 Answers1

1

Set accept mode to manual.

image of accept mode being highlighted. source: link below

Override ConnectRequest to check whatever variable you have to keep track if the session should be private, and call BoltNetwork.Refuse if it should be private, or if the connection should be allowed, BoltNetwork.Accept:

private bool serverShouldBePrivateNow; 

// ...

public override void ConnectRequest(UdpEndPoint endpoint, Bolt.IProtocolToken token) {
    bool connectionShouldBeRejected = false;

    connectionShouldBeRejected = connectionShouldBeRejected || serverShouldBePrivateNow;

    // any other appropriate checks go here
    // connectionShouldBeRejected = connectionShouldBeRejected || ...; 
    // connectionShouldBeRejected = connectionShouldBeRejected || ...; 
    // connectionShouldBeRejected = connectionShouldBeRejected || ...; 
   
    if (connectionShouldBeRejected)
    {
        BoltNetwork.Refuse(endpoint);
    }
    else
    {
        BoltNetwork.Accept(endpoint);
    }
}

If you need to return data to the client (such as an explanation of exactly why the connection was refused), you can respond with a token using BoltNetwork.Refuse(UdpEndPoint endpoint, IProtocolToken refuseToken);:

  1. Define an IProtocolToken:
using UdpKit;

public class RequestToken : Bolt.IProtocolToken
{
    public enum RefuseReason {privateReason1, privateReason2}

    // other reasons go here 
    public RefuseReason reason { get; set; }

    public void Read(UdpPacket packet)
    {
        this.reason = (RefuseReason)packet.ReadInt();
    }

    public void Write(UdpPacket packet)
    {
        packet.WriteInt((int)this.reason);
    }
}
  1. Use it on the server code in ConnectRequest:
public override void ConnectRequest(UdpEndPoint endpoint, Bolt.IProtocolToken token) {
    bool connectionShouldBeRejected = false;

    connectionShouldBeRejected = connectionShouldBeRejected || serverShouldBePrivateNow;

    if (connectionShouldBeRejected)
    {
        RequestToken token = new RequestToken();
        if (serverShouldBePrivateNow)
        { 
            token.reason = RequestToken.RefuseReason.privateReason1;
        } 
        else 
        {
            token.reason = RequestToken.RefuseReason.privateReason2;
        }

        BoltNetwork.Refuse(endpoint, token);
    }
    else
    {
        BoltNetwork.Accept(endpoint);
    }
} 
  1. Check it in the client code in void ConnectRefused(UdpEndPoint endpoint, Bolt.IProtocolToken token):
public override void ConnectRefused(UdpEndpoint endpoint, Bolt.IProtocolToken token)
{
    Debug.Assert(token is RequestToken);
    RequestToken reqTok = (RequestToken)token;

    switch (token.reason)
    {
    case RequestToken.RefuseReason.privateReason1:
        // handle private Reason 1 
        break;
    case RequestToken.RefuseReason.privateReason2:
        // handle private Reason 2
        break;
    default:
        // handle error in packet
        Debug.Assert(false, "Undefined value for enum"); 
    }
}

See Accepting and Refusing Connections on the Bolt wiki for more information

Ruzihm
  • 19,749
  • 5
  • 36
  • 48