示例#1
0
        public bool StartServer(int Port, int MaxPlayers = DEFAULT_MAX_PLAYERS)
        {
            NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();

            peer.Connect("peer_connected", this, nameof(ServerOnPeerConnected));
            peer.Connect("peer_disconnected", this, nameof(ServerOnPeerDisconnected));
            ConfigurePeer(peer);

            Error error   = peer.CreateServer(Port, MaxPlayers);
            bool  Success = error == Error.Ok;

            MDLog.CLog(Success, LOG_CAT, MDLogLevel.Info,
                       $"Starting server on port {Port} with {MaxPlayers} max players.");
            MDLog.CLog(!Success, LOG_CAT, MDLogLevel.Error, $"Failed to start server on port {Port}");

            if (Success)
            {
                UPNPPort = Port;
                SetNetworkPeer(peer);
                ServerOnStarted();
            }
            else
            {
                MDLog.Error(LOG_CAT, "Failed to start server");
                OnSessionFailedEvent();
            }

            return(Success);
        }
示例#2
0
        public bool StartClient(string Address, int Port)
        {
            NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();

            peer.Connect("connection_succeeded", this, nameof(ClientOnConnected));
            peer.Connect("connection_failed", this, nameof(ClientOnFailedToConnect));
            peer.Connect("server_disconnected", this, nameof(ClientOnServerDisconnect));
            ConfigurePeer(peer);

            Error error   = peer.CreateClient(Address, Port);
            bool  Success = error == Error.Ok;

            MDLog.CLog(Success, LOG_CAT, MDLogLevel.Info, $"Connecting to server at {Address}:{Port}");
            MDLog.CLog(!Success, LOG_CAT, MDLogLevel.Error, $"Failed to connect to server at {Address}:{Port}");

            if (Success)
            {
                SetNetworkPeer(peer);
            }

            return(Success);
        }