Exemplo n.º 1
0
        private void HandleRpc(Client client, RpcPacket packet)
        {
            Console.WriteLine($"Received RPC of type {packet.FunctionId}");
            switch (packet.FunctionId)
            {
            case RpcFunctions.PLAYER_INGAME_NOTICE:
                client.SendPacket(new JoinedGamePacket(
                                      client.Id,
                                      server.ElementRepository.Count + 1,
                                      this.server.Root.Id,
                                      HttpDownloadType.HTTP_DOWNLOAD_ENABLED_PORT,
                                      80,
                                      "",
                                      5,
                                      1
                                      ));

                var existingPlayersListPacket = PlayerPacketFactory.CreatePlayerListPacket(
                    this.server.ElementRepository.GetByType <Client>(ElementType.Player).ToArray(),
                    true
                    );
                client.SendPacket(existingPlayersListPacket);

                var newPlayerListPacket = PlayerPacketFactory.CreatePlayerListPacket(new Client[] { client }, false);
                foreach (var player in this.server.ElementRepository.GetByType <Client>(ElementType.Player))
                {
                    player.SendPacket(newPlayerListPacket);
                }

                this.server.ElementRepository.Add(client);
                client.HandleJoin();

                break;
            }
        }
        public void InvokeRpc(string name, IEnumerable <INetworkPeer> receivers, PacketFlags?flags, params object[] args)
        {
            GuardComponentId();

            var normalizedName = name.ToLowerInvariant();

            var serializedArgs = SerializeArgs(args);

            var methodIndex = s_RpcMethodsMappings[GetType()][normalizedName];
            var packet      = new RpcPacket
            {
                Arguments   = serializedArgs,
                ComponentId = ComponentId.Value,
                MethodIndex = methodIndex
            };

            var serializedPacket = m_Serializer.Serialize(packet);

            var packetDescription = m_ConnectionHandler.GetPacketDescription(methodIndex);

            if (flags != null)
            {
                packetDescription             = packetDescription.Clone();
                packetDescription.PacketFlags = flags.Value;
            }

            m_ConnectionHandler.Send(new OutgoingPacket
            {
                Data = serializedPacket,
                PacketDescription = packetDescription,
                PacketId          = (byte)PacketType.Rpc,
                Peers             = receivers.ToList()
            });
        }
Exemplo n.º 3
0
        private void HandleRpc(Client client, RpcPacket packet)
        {
            Debug.WriteLine($"Received RPC of type {packet.FunctionId}");
            switch (packet.FunctionId)
            {
            case RpcFunctions.PLAYER_INGAME_NOTICE:
                client.SendPacket(new JoinedGamePacket(
                                      client.Player.Id,
                                      this.elementRepository.Count + 1,
                                      this.root.Id,
                                      configuration.HttpUrl != null ? HttpDownloadType.HTTP_DOWNLOAD_ENABLED_URL : HttpDownloadType.HTTP_DOWNLOAD_ENABLED_PORT,
                                      configuration.HttpPort,
                                      configuration.HttpUrl ?? "",
                                      configuration.HttpConnectionsPerClient,
                                      1
                                      ));

                var otherPlayers = this.elementRepository
                                   .GetByType <Player>(ElementType.Player)
                                   .Except(new Player[] { client.Player })
                                   .ToArray();

                var existingPlayersListPacket = PlayerPacketFactory.CreatePlayerListPacket(otherPlayers, true);
                client.SendPacket(existingPlayersListPacket);

                var newPlayerListPacket = PlayerPacketFactory.CreatePlayerListPacket(new Player[] { client.Player }, false);
                newPlayerListPacket.SendTo(otherPlayers);

                this.server.HandlePlayerJoin(client.Player);

                break;
            }
        }
Exemplo n.º 4
0
 protected override void HandlePacket(PacketQueueEntry queueEntry)
 {
     switch (queueEntry.PacketId)
     {
     case PacketId.PACKET_ID_RPC:
         RpcPacket packet = new RpcPacket();
         packet.Read(queueEntry.Data);
         HandleRpc(queueEntry.Client, packet);
         break;
     }
 }
        /// <summary>
        /// Dispatches a command with arguments to pipe.
        /// </summary>
        /// <param name="command">The command to execute</param>
        /// <param name="args"></param>
        public void DispatchCommand(RpcCommand command, object args = null)
        {
            var dispatch = new RpcDispatch();

            dispatch.Command   = command.GetCommandName();
            dispatch.Arguments = args;

            var packet = new RpcPacket();

            packet.OpCode = RpcOpCode.Frame;
            packet.SetData(dispatch.ToJson());

            rpc.RaiseLogEvent(this, LogLevel.Debug, $"Dispatching command {command.GetCommandName()}");
            Write(packet);
        }
Exemplo n.º 6
0
 public async void HandlePackets()
 {
     while (true)
     {
         while (this.packetQueue.TryDequeue(out PacketQueueEntry queueEntry))
         {
             switch (queueEntry.PacketId)
             {
             case PacketId.PACKET_ID_RPC:
                 RpcPacket packet = new RpcPacket();
                 packet.Read(queueEntry.Data);
                 HandleRpc(queueEntry.Client, packet);
                 break;
             }
         }
         await Task.Delay(this.sleepInterval);
     }
 }
        /// <summary>
        /// Write <see cref="RpcPacket"/> to pipe stream.
        /// </summary>
        /// <param name="packet">Packet to write in.</param>
        public void Write(RpcPacket packet)
        {
            if (!stream.IsConnected)
            {
                throw new InvalidOperationException("Pipe is not connected!");
            }

            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet), "Packet cannot be null!");
            }

            var buf = packet.GetBytes();

            stream.Write(buf, 0, buf.Length);

            rpc.RaiseLogEvent(this, LogLevel.Debug, $"Wrote frame with opcode: {packet.OpCode}, with data:\n{JsonConvert.SerializeObject(packet.GetData())}");
        }
 private Task OnSendPacket(RpcPacket packet)
 {
     var json = JsonConvert.SerializeObject(packet, AudioRpc.JsonSettings);
     BackgroundMediaPlayer.SendMessageToForeground(AudioRpc.ConstructMessage(AudioRpc.RpcMessageTagAudioControllerHandler, json));
     return Task.FromResult<object>(null);
 }