Exemplo n.º 1
0
        void OnNewConnection(NetworkEvent netEvent)
        {
            ConnectionId newCId = netEvent.ConnectionId;

            ConnectionIds.Add(newCId);

            if (NodeState == State.Uninitialized)
            {
                OnJoin.TryInvoke(newCId);

                // Add server as a connection on the client end
                ConnectionIds.Add(new ConnectionId(0));
                NodeState = State.Client;
            }
            else if (NodeState == State.Server)
            {
                OnJoin.TryInvoke(newCId);
                foreach (var id in ConnectionIds)
                {
                    if (id.id == 0 || id.id == newCId.id)
                    {
                        continue;
                    }

                    byte[] payload;

                    // Announce the new connection to the old ones and vice-versa
                    payload = new UniStreamWriter().WriteShort(newCId.id).Bytes;
                    Send(Packet.From(this).To(id).With(ReservedTags.ClientJoined, payload), true);

                    payload = new UniStreamWriter().WriteShort(id.id).Bytes;
                    Send(Packet.From(this).To(newCId).With(ReservedTags.ClientJoined, payload), true);
                }
            }

            m_ConnectCallback.TryInvoke(newCId);
            m_ConnectCallback = null;
        }
Exemplo n.º 2
0
        void OnMessageReceived(NetworkEvent netEvent, bool reliable)
        {
            var bytes  = netEvent.GetDataAsByteArray();
            var packet = Packet.Deserialize(bytes);

            // If packet is null, it is a "raw" byte array message.
            // Forward it to everyone
            if (packet == null)
            {
                OnGetBytes.TryInvoke(netEvent.ConnectionId, bytes, reliable);
                foreach (var r in ConnectionIds)
                {
                    // Forward to everyone except the original sender and the server
                    if (r == CId || r == netEvent.ConnectionId)
                    {
                        continue;
                    }
                    Send(Packet.From(CId).To(r).With(ReservedTags.PacketForwarding, packet.Serialize()), true);
                }
                return;
            }


            string reservedTag = packet.Tag.StartsWith("reserved") ? packet.Tag : string.Empty;

            // If is not a reserved message
            if (reservedTag == string.Empty)
            {
                OnGetPacket.TryInvoke(netEvent.ConnectionId, packet, reliable);

                if (NodeState != State.Server)
                {
                    return;
                }

                // The server tries to broadcast the packet to everyone else listed as recipients
                foreach (var r in packet.Recipients)
                {
                    // Forward to everyone except the original sender and the server
                    if (r == CId.id || r == netEvent.ConnectionId.id)
                    {
                        continue;
                    }
                    Send(Packet.From(CId).To(r).With(ReservedTags.PacketForwarding, packet.Serialize()), true);
                }
                return;
            }

            // handle reserved messages
            switch (reservedTag)
            {
            case ReservedTags.ServerStopped:
                OnServerStopped.TryInvoke();
                break;

            case ReservedTags.ClientJoined:
                ConnectionIds.Add(netEvent.ConnectionId);
                OnJoin.TryInvoke(netEvent.ConnectionId);
                break;

            case ReservedTags.ClientLeft:
                ConnectionIds.Remove(netEvent.ConnectionId);
                OnLeave.TryInvoke(netEvent.ConnectionId);
                break;

            case ReservedTags.PacketForwarding:
                OnGetPacket.TryInvoke(netEvent.ConnectionId, packet, reliable);
                break;
            }
        }