コード例 #1
0
        static void Main(string[] args)
        {
            AlebClient.LogCommunication = true;

            string bind = Protocol.Localhost;

            if (args.Length == 2 && args[0] == "--bind")
            {
                bind = args[1];
            }
            else if (args.Length != 0)
            {
                Console.Error.WriteLine("Invalid arguments.");
                return;
            }

            TcpListener server = new TcpListener(
                IPAddress.TryParse(bind, out IPAddress ip)
                    ? ip
                    : Dns.GetHostEntry(bind).AddressList.First(i => i.AddressFamily == AddressFamily.InterNetwork),
                Protocol.Port
                );

            try {
                server.Start();
            } catch {
                Console.Error.WriteLine("Failed to start server! Is the port already in use?");
                return;
            }

            Console.WriteLine($"Aleb Server started at {server.LocalEndpoint}");

            while (true)
            {
                AlebClient client = new AlebClient(server.AcceptTcpClient());

                Console.WriteLine($"Connection from {client.Address}");

                client.MessageReceived += Login;
                client.Disconnected    += Disconnect;

                client.Run();

                client.Send("Version", Protocol.Version);
                client.Flush();
            }
        }
コード例 #2
0
        public static async Task <ConnectStatus> Connect(string host)
        {
            TcpClient tcp = null;

            try {
                tcp = new TcpClient();
                await tcp.ConnectAsync(host ?? Protocol.Localhost, Protocol.Port);
            } catch {
                tcp?.Dispose();
                return(ConnectStatus.Failed);
            }

            AlebClient.LogCommunication = true;

            Server = new AlebClient(tcp);

            Server.MessageReceived += Received;
            Server.Disconnected    += _ => Disconnected?.Invoke();

            Server.Name = "Server";

            Task <Message> VersionTask = Register("Version");

            Server.Run();

            await VersionTask;

            if (Convert.ToInt32((await VersionTask).Args[0]) != Protocol.Version)
            {
                Server.Dispose();
                Server = null;

                return(ConnectStatus.VersionMismatch);
            }

            return(ConnectStatus.Success);
        }