示例#1
0
 void OnServerInitSuccess(NetworkEvent netEvent)
 {
     ConnectionIds.Add(new ConnectionId(0));
     NodeState = State.Server;
     m_StartServerCallback.TryInvoke(true);
     m_StartServerCallback = null;
 }
示例#2
0
        public override void OnServerReceive(TransportEventData eventData)
        {
            CentralServerPeerInfo tempPeerInfo;

            switch (eventData.type)
            {
            case ENetworkEvent.ConnectEvent:
                Logging.Log(LogTag, "OnPeerConnected peer.ConnectionId: " + eventData.connectionId);
                ConnectionIds.Add(eventData.connectionId);
                break;

            case ENetworkEvent.DataEvent:
                ReadPacket(eventData.connectionId, eventData.reader);
                break;

            case ENetworkEvent.DisconnectEvent:
                Logging.Log(LogTag, "OnPeerDisconnected peer.ConnectionId: " + eventData.connectionId + " disconnectInfo.Reason: " + eventData.disconnectInfo.Reason);
                ConnectionIds.Remove(eventData.connectionId);
                // Remove disconnect map spawn server
                MapSpawnServerPeers.Remove(eventData.connectionId);
                // Remove disconnect map server
                if (MapServerPeers.TryGetValue(eventData.connectionId, out tempPeerInfo))
                {
                    MapServerPeersByMapId.Remove(tempPeerInfo.extra);
                    MapServerPeersByInstanceId.Remove(tempPeerInfo.extra);
                    MapServerPeers.Remove(eventData.connectionId);
                    RemoveMapUsers(eventData.connectionId);
                }
                break;

            case ENetworkEvent.ErrorEvent:
                Logging.LogError(LogTag, "OnPeerNetworkError endPoint: " + eventData.endPoint + " socketErrorCode " + eventData.socketError + " errorMessage " + eventData.errorMessage);
                break;
            }
        }
示例#3
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;
        }
示例#4
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;
            }
        }