Esempio n. 1
0
        /*public void SendUnreliable(RudpAddress address, uint service, int type, G2Packet packet)
         * {
         *  // insecure, rudp provides this same method which is more secure, if a rudp connection is already established
         *
         *  RudpPacket wrap = CreateRudpPacket(address.Address, service, type, packet, false);
         *
         *  int sentBytes = LightClient.SendtoAddress(Core.Network, address, wrap);
         *
         *  Core.ServiceBandwidth[service].OutPerSec += sentBytes;
         * }*/

        public void ReceivePacket(G2ReceivedPacket raw, RudpPacket packet)
        {
            DhtClient client = new DhtClient(packet.SenderID, packet.SenderClient);

            if (!Clients.ContainsKey(client.RoutingID))
            {
                Clients[client.RoutingID] = new LightClient(client);
            }

            LightClient light = Clients[client.RoutingID];

            light.LastSeen = Core.TimeNow;

            // either direct, or node's proxy
            light.AddAddress(Core, new RudpAddress(raw.Source), true);

            if (raw.ReceivedTcp) // add this second so sending ack through tcp proxy is perferred
            {
                light.AddAddress(Core, new RudpAddress(raw.Source, raw.Tcp), true);
            }


            if (packet.PacketType == RudpPacketType.LightAck)
            {
                ReceiveAck(raw, light, packet);
            }

            else if (packet.PacketType == RudpPacketType.Light)
            {
                RudpLight info = new RudpLight(packet.Payload);

                if (Core.ServiceBandwidth.ContainsKey(info.Service))
                {
                    Core.ServiceBandwidth[info.Service].InPerSec += raw.Root.Data.Length;
                }

                if (Data.Contains(info.Service, info.Type))
                {
                    Data[info.Service, info.Type].Invoke(client, info.Data);
                }

                if (packet.Sequence == 1) // reliable packet
                {
                    SendAck(light, packet, info.Service);
                }
            }
        }
Esempio n. 2
0
        RudpPacket CreateRudpPacket(DhtClient client, uint service, int type, G2Packet packet, bool reliable)
        {
            RudpPacket comm = new RudpPacket();

            comm.SenderID     = Network.Local.UserID;
            comm.SenderClient = Network.Local.ClientID;
            comm.TargetID     = client.UserID;
            comm.TargetClient = client.ClientID;
            comm.PacketType   = RudpPacketType.Light;
            comm.Payload      = RudpLight.Encode(service, type, packet.Encode(Network.Protocol));

            if (reliable)
            {
                comm.PeerID   = (ushort)Core.RndGen.Next(ushort.MaxValue); // used to ack
                comm.Sequence = 1;
            }

            return(comm);
        }