예제 #1
0
        /// <summary>
        /// Receive packest from the given network host.
        /// </summary>
        public void GatherPackets(NetworkHost host)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            Packet packet;

            while (host.Receive(out packet))
            {
                // Reconstruct object
                var obj = Serializer.Deserialize(packet.Data);

                TypedPacket <object> typedPacket = new TypedPacket <object>();
                typedPacket.Data       = obj;
                typedPacket.Connection = packet.Connection;
                typedPacket.Channel    = packet.Channel;

                //Debug.LogFormat( "Received a {0} ( {1} bytes )", obj.GetType().Name, packet.Data.Length );

                // If packet type is not yet encountered, created queue for that type.
                if (Queue.ContainsKey(obj.GetType()) == false)
                {
                    Queue[obj.GetType()] = new Queue <TypedPacket <object> >();
                }

                // Add packet object to queue.
                Queue[obj.GetType()].Enqueue(typedPacket);
            }
        }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        Network.ProcessEvents();

        // Broadcast Packets
        BroadcastPacket bPacket;

        while (Network.ReceiveBroadcast(out bPacket))
        {
            // If not a local address
            if (!NetworkHost.IsLocalAddress(bPacket.RemoteInfo.Address))
            {
                var address = bPacket.RemoteInfo.Address;
                var port    = bPacket.RemoteInfo.Port;

                Debug.LogFormat("Broadcast Received From: {0}:{1}", address, port);

                if (KnownPeople.Contains(bPacket.RemoteInfo))
                {
                    continue;
                }
                else
                {
                    KnownPeople.Add(bPacket.RemoteInfo);

                    //
                    var textUi = GetComponent <Text>();
                    textUi.text += address + "\n";
                }
            }
        }

        // Regular Packets
        Packet packet;

        while (Network.Receive(out packet))
        {
            //
        }
    }