Beispiel #1
0
        void OnDisconnected(NetworkEvent netEvent)
        {
            if (NodeState == State.Client)
            {
                NodeState = State.Uninitialized;
                Deinit();
            }
            else if (NodeState == State.Server)
            {
                OnLeave.TryInvoke(netEvent.ConnectionId);
                var dId = netEvent.ConnectionId;
                ConnectionIds.Remove(netEvent.ConnectionId);

                var payload = new UniStreamWriter().WriteShort(dId.id).Bytes;

                // Clients are not aware of each other as this is a star network
                // Send a reliable reserved message to everyone to announce the disconnection
                Send(Packet.From(CId).With(ReservedTags.ClientLeft, payload), true);
            }
        }
Beispiel #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;
            }
        }