示例#1
0
        static void Main(string[] args)
        {
            var config        = new LoggingConfiguration();
            var consoleTarget = new ColoredConsoleTarget();

            config.AddRuleForAllLevels(consoleTarget);
            LogManager.Configuration = config;

            var logger = LogManager.GetLogger("Main");

            logger.Info("Starting server");
            var fileTransfer = new FileTransfer();

            fileTransfer.FileReceived += (sender, stream) => logger.Info("Received file, saved to {path}", stream.Name);

            var factory = new PacketsFactory();

            factory.RegisterPacket <ChunkedDataPacket>();

            var server = new Server(IPAddress.Any, 50505, 4, factory);

            server.PacketReceived += (sender, packetArgs) => fileTransfer.OnPacketReceived(packetArgs.Client.EndPoint, packetArgs.Packet);
            server.Start();
            Console.ReadLine();
            server.Stop();
        }
示例#2
0
        /// <summary>
        /// Initialize a new server instance
        /// </summary>
        /// <param name="bindIp">IP to bind to, easiest is <see cref="IPAddress.Any"/></param>
        /// <param name="port">The port to bind to</param>
        /// <param name="maxClients">The maximum amount of clients that are allowed to connect</param>
        /// <param name="packetsFactory">The factory responsible for parsing received data</param>
        public Server(IPAddress bindIp, ushort port, uint maxClients, PacketsFactory packetsFactory)
        {
            listener = new TcpListener(bindIp, port);
            clients  = new ServerClient[maxClients];
            for (var i = 0; i < clients.Length; i++)
            {
                clients[i] = new ServerClient();
            }
            MaxClients = maxClients;
            packetsFactory.RegisterPacket <PingPacket>();
            this.packetsFactory = packetsFactory;

            PacketReceived += async(sender, args) =>
            {
                if (!(args.Packet is PingPacket packet))
                {
                    return;
                }
                if (packet.didBounce)
                {
                    var diff = DateTime.UtcNow - packet.SendTime;
                    args.Client.Ping = (int)diff.TotalMilliseconds;
                    Logger.Debug("Ping to client {client}: {ping}ms", args.Client.EndPoint, args.Client.Ping);
                }
                else
                {
                    packet.didBounce = true;
                    await SendAsync(packet, args.Client);
                }
            };
        }
示例#3
0
        static void Main(string[] args)
        {
            var config        = new LoggingConfiguration();
            var consoleTarget = new ColoredConsoleTarget();

            config.AddRuleForAllLevels(consoleTarget);
            LogManager.Configuration = config;

            var logger = LogManager.GetLogger("Main");

            logger.Info("Starting client");
            var fileTransfer = new FileTransfer();

            var factory = new PacketsFactory();

            factory.RegisterPacket <ChunkedDataPacket>();

            var server = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 50505);
            var client = new Client(server, factory);

            client.PacketReceived  += (sender, packet) => fileTransfer.OnPacketReceived(server, packet);
            client.FailedToConnect += (sender, eventArgs) => client.Start();
            client.Disconnected    += (sender, eventArgs) => client.Start();
            client.Start();
            Console.ReadLine();
            using (var fileStream = new FileStream("img.jpg", FileMode.Open))
            {
                fileTransfer.SendFile(fileStream, client.Send);
            }

            Console.ReadLine();
            client.Stop();
        }
示例#4
0
文件: Client.cs 项目: vinhui/Packets
        /// <summary>
        /// Initialize a new connection with the given server
        /// </summary>
        /// <param name="serverIp">IP of the server to connect to</param>
        /// <param name="serverPort">Port that the server is bound to</param>
        /// <param name="packetsFactory">The factory responsible for parsing received data</param>
        public Client(IPAddress serverIp, int serverPort, PacketsFactory packetsFactory)
        {
            this.serverIp   = serverIp;
            this.serverPort = serverPort;
            packetsFactory.RegisterPacket <PingPacket>();
            this.packetsFactory = packetsFactory;
            tcpClient           = new TcpClient();

            PacketReceived += (sender, args) =>
            {
                if (!(args is PingPacket packet))
                {
                    return;
                }
                if (packet.didBounce)
                {
                    var diff = DateTime.UtcNow - packet.SendTime;
                    Ping = (int)diff.TotalMilliseconds;
                    Logger.Debug("Ping to server: {ping}ms", Ping);
                }
                else
                {
                    packet.didBounce = true;
                    Send(packet);
                }
            };
        }
示例#5
0
 public void Setup()
 {
     _factory = new PacketsFactory();
     _factory.RegisterPacket <EmptyPacket>();
     _factory.RegisterPacket <TestPacket>();
 }