Exemplo n.º 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();
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
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);
                }
            };
        }
Exemplo n.º 4
0
        /// <summary>
        /// Форминование информации из байтового массива.
        /// </summary>
        /// <param name="_buf"></param>
        /// <param name="_offset"></param>
        public void FromByteArray(byte[] _buf, int _offset)
        {
            PacketLength = BitConverter.ToInt32(_buf, _offset);
            int count = 0;

            using (var stream = new MemoryStream(_buf, _offset, PacketLength))
            {
                using (var reader = new BinaryReader(stream))
                {
                    reader.ReadUInt32();
                    count          += sizeof(UInt32);
                    Id              = new Guid(reader.ReadBytes(16));
                    count          += 16;
                    Time            = new DateTime(reader.ReadInt64());
                    count          += sizeof(Int64);
                    InformationType = (TypeInformation)reader.ReadByte();
                    count          += 1;

                    if (ProtocolType == ProtocolType.Baikal)
                    {
                        var packet1 = new BaikalPacket();
                    }
                    else
                    {
                        var packet2 = PacketsFactory.GeneratePacket(_buf, _offset);
                    }

                    byte[] row = reader.ReadBytes((int)(PacketLength - count));

                    RowData.FromByteArray(_buf, count);
                }
            }
            {
            }
        }
Exemplo n.º 5
0
        /// <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);
                }
            };
        }
Exemplo n.º 6
0
        public DebuggerClient(MainWindowViewModel viewModel)
        {
            _packetValidator = new PacketValidator();
            _viewModel       = viewModel;

            _packetValidator = new PacketValidator();
            _packetsFactory  = new PacketsFactory();

            _packetHandler = new Dictionary <PacketType, PacketHandlerBase>
            {
                { PacketType.Registers, new RegistersHandler(viewModel) },
                { PacketType.Pins, new PinsHandler(viewModel) },
                { PacketType.Cycles, new CyclesHandler(viewModel) },
                { PacketType.Memory, new MemoryHandler(viewModel) }
            };

            _pinsRequestPacketGenerator            = new PinsRequestPacketGenerator();
            _pinsPacketGenerator                   = new PinsPacketGenerator();
            _registersRequestPacketGenerator       = new RegistersRequestPacketGenerator();
            _registersPacketGenerator              = new RegistersPacketGenerator();
            _cyclesRequestPacketGenerator          = new CyclesRequestPacketGenerator();
            _stopCommandPacketGenerator            = new StopCommandPacketGenerator();
            _continueCommandPacketGenerator        = new ContinueCommandPacketGenerator();
            _nextCycleCommandPacketGenerator       = new NextCycleCommandPacketGenerator();
            _nextInstructionCommandPacketGenerator = new NextInstructionCommandPacketGenerator();
            _memoryRequestPacketGenerator          = new MemoryRequestPacketGenerator();
            _runToAddressCommandPacketGenerator    = new RunToAddressCommandPacketGenerator();
            _runUntilLoopCommandPacketGenerator    = new RunUntilLoopCommandPacketGenerator();
        }
Exemplo n.º 7
0
    IEnumerator SendClockTimeStamp()
    {
        GameSparksRTUnity rtSession = MyGameSparksManager.Instance().GetRTUnitySession();

        PacketsFactory.SendTimeStamp(rtSession);

        yield return(new WaitForSeconds(m_sendTimeStampRatioInSeconds));

        StartCoroutine(SendClockTimeStamp());
    }
Exemplo n.º 8
0
    public void ProcessTimeStampPacket(RTPacket rtPacket)
    {
        long originalUnixTime = rtPacket.Data.GetLong(1).Value;
        long now = PacketsFactory.GetUnixTime();

        roundTrip = now - originalUnixTime;

        latency = (long)(roundTrip / 2);

        long serverUnixTime = rtPacket.Data.GetLong(2).Value;
        long serverDelta    = PacketsFactory.GetUnixTime() - serverUnixTime;

        timeDelta = serverDelta + latency;
    }
Exemplo n.º 9
0
    private IEnumerator SendMovement(bool debug)
    {
        bool hasSpeed           = m_rigidbody.velocity.magnitude > VELOCITY_THRESHOLD;
        bool movingStateChanged = hasSpeed != m_wasMoving;
        bool crouchStateChanged = UserControl.Crouch != m_wasCrouch;
        bool jumpStateChanged   = UserControl.Jump != m_wasJumping;

        if (hasSpeed ||
            movingStateChanged ||
            crouchStateChanged ||
            jumpStateChanged)
        {
            if (!debug)
            {
                GameSparksRTUnity rtSession = MyGameSparksManager.Instance().GetRTUnitySession();
                PacketsFactory.SendPlayerMovement(this, rtSession);
            }
            else
            {
                m_debugOnlinePlayer.SendMovement(this);
            }
        }

        if (hasSpeed)
        {
            Debug.Log("SENDING: " + m_rigidbody.velocity + " TIME: " + Time.time);
        }

        if (!hasSpeed && m_wasMoving)
        {
            Debug.LogError("STOP MOVING " + Time.time);
        }

        //Debug.Log("isMoving: "+isMoving+" movingStateChanged: "+movingStateChanged+" crouchStateChanged: "+crouchStateChanged+" jumpStateChanged: "+jumpStateChanged);

        m_wasMoving      = hasSpeed;
        m_wasJumping     = UserControl.Jump;
        m_wasCrouch      = UserControl.Crouch;
        previousPosition = transform.position;

        yield return(new WaitForSeconds(GameController.UPDATE_RATE));

        StartCoroutine(SendMovement(debug));
    }
Exemplo n.º 10
0
        public DebuggerServer(M6502Core core, ushort port)
        {
            _port            = port;
            _packetValidator = new PacketValidator();
            _packetsFactory  = new PacketsFactory();

            _packetHandler = new Dictionary <PacketType, PacketHandlerBase>
            {
                { PacketType.RegistersRequest, new RegistersRequestHandler(core) },
                { PacketType.Registers, new RegistersHandler(core) },
                { PacketType.PinsRequest, new PinsRequestHandler(core) },
                { PacketType.Pins, new PinsHandler(core) },
                { PacketType.CyclesRequest, new CyclesRequestHandler(core) },
                { PacketType.StopCommand, new StopCommandHandler(core) },
                { PacketType.ContinueCommand, new ContinueCommandHandler(core) },
                { PacketType.NextCycleCommand, new NextCycleCommandHandler(core) },
                { PacketType.NextInstructionCommand, new NextInstructionCommandHandler(core) },
                { PacketType.MemoryRequest, new MemoryRequestHandler(core) },
                { PacketType.RunToAddressCommand, new RunToAddressCommandHandler(core) },
                { PacketType.RunUntilLoopCommand, new RunUntilLoopCommandHandler(core) }
            };
        }
Exemplo n.º 11
0
 public void Setup()
 {
     _factory = new PacketsFactory();
     _factory.RegisterPacket <EmptyPacket>();
     _factory.RegisterPacket <TestPacket>();
 }
Exemplo n.º 12
0
 /// <summary>
 /// Initialize a new connection with the given server
 /// </summary>
 /// <param name="endPoint">Server to connect to</param>
 /// <param name="packetsFactory">The factory responsible for parsing received data</param>
 public Client(IPEndPoint endPoint, PacketsFactory packetsFactory)
     : this(endPoint.Address, endPoint.Port, packetsFactory)
 {
 }