Example #1
0
        public bool RemoveClient(VNetClient client)
        {
            if (client == null)
            {
                return(false);
            }
            if (VNetCommon.SHOW_LOGS)
            {
                UnityEngine.Debug.Log("VNet: Removing client " + client.GetName());
            }

            // Callback when clietns are removed
            ClientRemovedCallback(client);

            // Check if we need to do a host migration
            m_netHost.CheckForHostLeft(client);

            // Remove from the client list and delete
            m_clientsByUID.Remove(client.GetUID());

            // disconnect it
            client.Disconnect();

            return(true);
        }
Example #2
0
        public VNet()
        {
            Inst          = this;
            m_netDispatch = new VNetDispatch();
            m_netTimer    = new VNetTimer();

            // Initialize client and multi links
            m_clientLink = UnityEngine.Transform.FindObjectOfType <VNetSockClientLink>();
            if (m_clientLink == null)
            {
                GameObject newObj = new GameObject("VNetSockets");
                GameObject.DontDestroyOnLoad(newObj);
                m_clientLink = newObj.AddComponent <VNetSockClientLink>();
                m_multiLink  = newObj.AddComponent <VNetSockMultiLink>();
            }
            else
            {
                m_multiLink = m_clientLink.GetComponent <VNetSockMultiLink>();
            }

            // m_clientLink = new VNetSockClientLink();
            // m_multiLink = new VNetSockMultiLink();

            m_multicastClient   = new VNetClient();
            m_availableSessions = new VNetAvailableSessions();
            m_netSession        = new VNetSession();



            RegisterListeners();
        }
Example #3
0
        public VNetClientPing(VNetClient client)
        {
            m_client = client;
            m_times  = new double[VNetCommon.NET_CLIENT_PING_QUEUE_LENGTH];
            Reset();

            VNetDispatch.RegisterListenerInst <VNetMessagePingClient>(OnPing);
            VNetDispatch.RegisterListenerInst <VNetMessagePongClient>(OnPong);
        }
Example #4
0
        public void OnClientsWantsToLeave(VNetMessageLeaveSession leaveRequest)
        {
            VNetMessageLeaveSessionConfirm confirm = new VNetMessageLeaveSessionConfirm();
            VNetClient client = leaveRequest._client;

            confirm.clientUID = client.GetUID();
            client.SendNetMessage(confirm, false);
            client.SendPacketToClient();
            RemoveClient(client);
        }
Example #5
0
        // Listeners
        public void OnAcceptClientJoinRequest(VNetMessageAcceptClient accept)
        {
            // Not us
            if (accept.clientUID != VNet.Inst.GetUID())
            {
                return;
            }

            // Different session?
            if (accept.sessionUID != m_attemptingToJoinSession.sessionUID)
            {
                return;
            }

            // We aren't trying to join..
            if (m_sessionState != VNetSessionState.Connecting)
            {
                return;
            }

            // joined
            m_sessionState = VNetSessionState.InSession;

            // Add the host
            VNetSimpleClientData sc = m_attemptingToJoinSession.host;

            m_netHost.SetHostInfo(sc.uid, sc.role);

            VNetClient hostClient = GetClientByUID(sc.uid);

            hostClient.SetName(m_attemptingToJoinSession.host.name);
            hostClient.SetRole(sc.role);

            // add remaining clients
            for (SByte i = 0; i < m_attemptingToJoinSession.numClients; i++)
            {
                sc = m_attemptingToJoinSession.clients[i];

                // skip yourself
                if (sc.uid == VNet.Inst.GetUID())
                {
                    continue;
                }

                VNetClient client = AddClient(sc.uid, new IPEndPoint(sc.ip, sc.port));
                client.SetName(sc.name);
                client.SetRole(sc.role);
            }

            if (VNetCommon.SHOW_LOGS)
            {
                UnityEngine.Debug.Log("VNet: Joined session hosted by " + hostClient.GetName());
            }
        }
Example #6
0
        public void OnNewClient(VNetMessageNewClient newClient)
        {
            // make sure this client doesn't already exist
            if (GetClientByUID(newClient.clientData.uid) != null)
            {
                return;
            }

            VNetClient client = AddClient(newClient.clientData.uid, new IPEndPoint(newClient.clientData.ip, newClient.clientData.port));

            client.SetName(newClient.clientData.name);
            client.SetRole(newClient.role);
        }
Example #7
0
        public void OnClientJoinRequest(VNetMessageJoinSession joinRequest)
        {
            // If i'm not the host, ignore this
            if (LocalIsHost() == false)
            {
                return;
            }

            // If this is for a separate session, ignore
            UInt64 sessionUID = VNetSession.Inst.GetSessionUID();

            if (joinRequest.sessionUID != sessionUID)
            {
                return;
            }

            // Could be a dup, ignore if if that's the case
            if (VNetSession.Inst.GetClientByUID(joinRequest._packet.header.clientUID) != null)
            {
                return;
            }

            // Add this client
            VNetMessageNewClient nmc = new VNetMessageNewClient();

            nmc.clientData        = new VNetSimpleClientData();
            nmc.clientData.active = 1;
            nmc.clientData.ip     = joinRequest._packet.IP_Port.Address;
            nmc.clientData.port   = joinRequest._packet.IP_Port.Port;
            nmc.clientData.uid    = joinRequest._packet.header.clientUID;
            nmc.clientData.name   = joinRequest.userName;
            nmc.clientData.role   = joinRequest.role;

            nmc.sessionUID = sessionUID;
            VNet.Inst.SendToLobby(nmc, true);

            // Add the client to the local list
            VNetClient client = VNetSession.Inst.AddClient(joinRequest._packet.header.clientUID, joinRequest._packet.IP_Port);

            client.SetName(joinRequest.userName);
            client.SetRole(joinRequest.role);

            // Accept this client
            VNetMessageAcceptClient ac = new VNetMessageAcceptClient();

            ac.clientUID  = client.GetUID();
            ac.sessionUID = sessionUID;
            ac.role       = joinRequest.role;
            client.SendNetMessage(ac, true);
        }
Example #8
0
        // Functions
        public void CheckForHostLeft(VNetClient client)
        {
            if (client.GetUID() == m_hostUID)
            {
                if (VNetCommon.SHOW_LOGS)
                {
                    UnityEngine.Debug.Log("VNet: The host left the session.");
                }

                // In the future, become the host based on some internal value
                Disconnect();
                VNetSession.Inst.DisconnectAll();
            }
        }
Example #9
0
 public void Update(VNetClient host)
 {
     if (host != null && m_timeSyncsRemaining > 0)
     {
         m_nextTimeSync -= VNetTimer.Inst.GetFrameTimeFloat();
         if (m_nextTimeSync <= 0)
         {
             m_nextTimeSync = VNetCommon.NET_TIME_SYNC_WAIT_TIME;
             VNetMessageTimeRequest request = new VNetMessageTimeRequest();
             request.currentTime = VNetTimer.Inst.GetSystemTimeNow();
             host.SendNetMessage(request, false);
         }
     }
 }
Example #10
0
        public void AttemptToJoinSession(UInt64 sessionUID, Int32 role)
        {
            m_attemptingToJoinSession = VNet.Inst.m_availableSessions.GetSession(sessionUID);
            m_sessionState            = VNetSessionState.Connecting;
            m_attemptingToJoinTimer   = 0;
            m_sessionUID           = sessionUID;
            m_attemptingToJoinRole = role;

            // Add the host as a client
            VNetClient host = AddClient(m_attemptingToJoinSession.host.uid, new IPEndPoint(m_attemptingToJoinSession.host.ip, m_attemptingToJoinSession.host.port));

            host.SetRole(m_attemptingToJoinSession.host.role);

            if (VNetCommon.SHOW_LOGS)
            {
                UnityEngine.Debug.Log("VNet: Attempting to join session #" + sessionUID.ToString());
            }
        }
Example #11
0
        public void Update()
        {
            if (m_isInitialized == false)
            {
                return;
            }

            m_netTimer.Update();
            m_netSession.Update();

            // Read packet data from the net
            VNetPacket inPacket = new VNetPacket();

            while (m_multiLink.Recv(inPacket))
            {
                if (VNetVerifier.VerifyPacket(inPacket))
                {
                    m_netDispatch.HandlePacketIn(inPacket, null);
                }
            }

            while (m_clientLink.Recv(inPacket))
            {
                if (VNetVerifier.VerifyPacket(inPacket))
                {
                    VNetClient client = m_netSession.ResolveClientFromPacket(inPacket);
                    if (client == null)
                    {
                        continue;
                    }

                    // reliable, packet stuff
                    client.OnDataReceived(inPacket);

                    // dispatch
                    m_netDispatch.HandlePacketIn(inPacket, client);
                }
            }

            // Update clients
            m_multicastClient.Update();
            m_netSession.UpdateClients();
        }
Example #12
0
        public void UpdateClients()
        {
            List <VNetClient> disconnectedClients = new List <VNetClient>();

            foreach (KeyValuePair <UInt64, VNetClient> kvp in m_clientsByUID)
            {
                VNetClient client = kvp.Value;
                client.Update();
                if (client.CheckForTimeout())
                {
                    disconnectedClients.Add(client);
                }
            }

            foreach (VNetClient client in disconnectedClients)
            {
                m_clientsByUID.Remove(client.GetUID());
            }
        }
Example #13
0
        public VNetClient AddClient(UInt64 clientID, IPEndPoint endpoint)
        {
            if (GetClientByUID(clientID) != null)
            {
                return(null);
            }

            // create the client
            VNetClient sockClient = new VNetClient();

            sockClient.SetClientRaw(clientID, endpoint);

            // Add to the list
            m_clientsByUID.Add(clientID, sockClient);

            // Call add-client so systems can propogate this
            ClientAddedCallback(sockClient);

            if (VNetCommon.SHOW_LOGS)
            {
                UnityEngine.Debug.Log("VNet: Adding client #" + clientID.ToString());
            }
            return(sockClient);
        }
Example #14
0
        public void HandlePacketIn(VNetPacket packet, VNetClient client)
        {
            int numMessagesToRead = packet.header.numMessages;

            packet.StartPacketRead();

            Dictionary <int, VNetMessage> reliableIn = new Dictionary <int, VNetMessage>();

            while (numMessagesToRead-- > 0)
            {
                if (packet.UpdateNextMessageHeaders() == false)
                {
                    break;
                }

                if (m_register.ContainsKey(packet.m_nextMessageType.checksum) == false)
                {
                    packet.SkipNextMessage();
                }
                else
                {
                    VNetDispatchRegistryBase reg = m_register[packet.m_nextMessageType.checksum];
                    if (reg.GetNumCallbacks() == 0)
                    {
                        packet.SkipNextMessage();
                    }
                    else
                    {
                        VNetMessage message = reg.ConvertDataToMessage(packet);
                        message._client    = client;
                        message._packet    = packet;
                        message.__typeName = new VNetStringChksum(packet.m_nextMessageType.checksum);


                        if (message.GetReliableIndex() != -1)
                        {
                            if (reliableIn.ContainsKey(message.GetReliableIndex()))
                            {
                                continue;
                            }
                            reliableIn.Add(message.GetReliableIndex(), message);
                            continue;
                        }

                        reg.Callback(message);
                    }
                }
            }
            if (client != null)
            {
                while (reliableIn.ContainsKey(client.GetNextReliableIndex()))
                {
                    VNetMessage message = reliableIn[client.GetNextReliableIndex()];
                    client.ReliableMessageCheck(message);

                    VNetDispatchRegistryBase reg = m_register[message.GetMessageType().checksum];
                    reg.Callback(message);
                }
            }

            reliableIn.Clear();
        }