/// <summary>
    /// Create peer and start server
    /// </summary>
    /// <returns></returns>
    public static RakNet_Peer CreateServer(string address, int port, int maxConnections)
    {
        RakNet_Peer peer = new RakNet_Peer(NET_Create());

        if (NET_StartServer(peer.ptr, address, port, maxConnections) == 0)
        {
            peer.guid           = ulong.MaxValue;
            peer.address        = address;
            peer.port           = port;
            peer.maxConnections = maxConnections;
            peer.state          = PeerState.RunAsServer;
            peer.runningTime    = 0;

            //set the bandwidth so that it is enough for all connections
            //peer.SetBandwidth(MAX_BANDWIDTH*(uint)maxConnections);
            if (debugLevel >= RakDebugLevel.Low)
            {
                Debug.Log("[Peer] [" + peer.state + "] Server started on port " + port + " (max connections: " + maxConnections + ")");
            }
            return(peer);
        }
        string text = Marshal.PtrToStringAnsi(NET_LastStartupError(peer.ptr));

        if (debugLevel >= RakDebugLevel.Low)
        {
            Debug.LogError("[Peer] [" + peer.state + "] Couldn't create server on port " + port + " (" + text + ")");
        }
        peer.state = PeerState.ServerInitFailed;
        peer.Shutdown();
        return(null);
    }
    /// <summary>
    /// Create peer and connect to server
    /// </summary>
    public static RakNet_Peer Connect(string address, int port, int retries, int timeout)
    {
        IntPtr      ptr  = NET_Create();
        RakNet_Peer peer = new RakNet_Peer(ptr);

        int state = NET_StartClient(ptr, address, port, retries, 1400, timeout * 1000);

        if (state == 0)
        {
            peer.address     = address;
            peer.port        = port;
            peer.state       = PeerState.RunAsClient;
            peer.runningTime = 0;

            if (debugLevel >= RakDebugLevel.Low)
            {
                Debug.Log("[Peer] [" + peer.state + "] Connecting to " + address + ":" + port + " with " + retries + " retry count [Timeout: " + timeout + "]");
            }

            return(peer);
        }

        string text = Marshal.PtrToStringAnsi(NET_LastStartupError(peer.ptr));

        if (debugLevel >= RakDebugLevel.Low)
        {
            Debug.LogWarning("[Peer] [" + peer.state + "] Couldn't connect to server " + address + ":" + port + " (" + text + ")");
        }
        peer.state = PeerState.ClientInitFailed;
        peer.Shutdown();
        return(null);
    }
    public void StopServer()
    {
        for (int i = 0; i < connections.Count; i++)
        {
            Kick(connections[i].guid, "Server shutting down!");
        }
        if (server_peer != null)
        {
            OnServerShutdown();
            server_peer.Shutdown();
            server_peer = null;
        }

        Query.Shutdown();
    }
Beispiel #4
0
 public void Disconnect(DisconnectionType disconnectionType = DisconnectionType.Unknown)
 {
     if (client_peer != null)
     {
         client_peer.Shutdown();
         IsConnected = IsConnecting = false;
         if (disconnectionType != DisconnectionType.Unknown)
         {
             OnDisconnected(client_peer.address, client_peer.port, disconnectionType, disconnect_reason);
             OnDisconnectedEvent?.Invoke(client_peer.address, client_peer.port, disconnectionType, disconnect_reason);
         }
         else if (disconnectionType == DisconnectionType.Unknown)
         {
             OnDisconnected(client_peer.address, client_peer.port, DisconnectionType.ByUser, string.Empty);
             OnDisconnectedEvent?.Invoke(client_peer.address, client_peer.port, DisconnectionType.ByUser, string.Empty);
         }
         if (RakNet_Peer.debugLevel >= RakDebugLevel.Low)
         {
             Debug.Log("[Client] Disconnected... (" + disconnectionType + ")");
         }
         client_peer = null;
     }
 }
Beispiel #5
0
 private void OnDestroy()
 {
     BehaviourManager.UnregisterBehaviour(this); client_peer?.Shutdown();
 }