예제 #1
0
        /// <summary>
        /// Create server
        /// </summary>
        /// <param name="networkPort">Server network port</param>
        /// <param name="timeoutTime">Timeout time in seconds</param>
        /// <returns>Server if successful, otherwise "null"</returns>
        public static IServerSynchronizer Create(ushort networkPort, uint timeoutTime)
        {
            if (networkPort < 1)
            {
                throw new ArgumentException("Network port can't be smaller than 1.");
            }
            IServerSynchronizer ret = null;

            if (NetworkLibrary.Initialize())
            {
                Host         server_host = null;
                IConnector[] connectors  = new IConnector[2];
                try
                {
                    server_host = new Host();
                    Address address = new Address
                    {
                        Port = networkPort
                    };
                    server_host.Create(address, (int)Library.maxPeers, 0);
                    ret = new ServerSynchronizer();
                    bool on_handle_peer_connection_attempt(IPeer peer) => !ret.Bans.IsPeerBanned(peer, out _);

                    connectors[0] = new LocalConnector(on_handle_peer_connection_attempt);
                    connectors[1] = new ENetConnector(server_host, networkPort, timeoutTime, on_handle_peer_connection_attempt);
                    foreach (IConnector connector in connectors)
                    {
                        ret.AddConnector(connector);
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);
                    server_host?.Dispose();
                    foreach (IConnector connector in connectors)
                    {
                        connector?.Dispose();
                    }
                    ret?.Dispose();
                    NetworkLibrary.Deinitialize();
                }
            }
            return(ret);
        }
예제 #2
0
        /// <summary>
        /// Connects to a network instance
        /// </summary>
        /// <param name="ipAddress">IP address</param>
        /// <param name="port">Port</param>
        /// <param name="token">Token</param>
        /// <param name="timeoutTime">Timeout time in seconds</param>
        /// <returns>Client synchronizer if successful, otherwise "null"</returns>
        public static IClientSynchronizer ConnectToNetwork(string ipAddress, ushort port, string token, uint timeoutTime)
        {
            if (ipAddress == null)
            {
                throw new ArgumentNullException(nameof(ipAddress));
            }
            if (port <= 0)
            {
                throw new ArgumentException(nameof(port));
            }
            IClientSynchronizer ret = null;

            if (NetworkLibrary.Initialize())
            {
                Host client_host = null;
                try
                {
                    client_host = new Host();
                    Address address = new Address();
                    address.SetHost(ipAddress);
                    address.Port = port;
                    client_host.Create(1, 0);
                    ENetConnector connector = new ENetConnector(client_host, port, timeoutTime, (_) => true);
                    ret = new ClientSynchronizer(connector.ConnectToNetwork(address), token);
                    ret.AddConnector(connector);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);
                    if (client_host != null)
                    {
                        client_host.Dispose();
                    }
                    NetworkLibrary.Deinitialize();
                }
            }
            return(ret);
        }