Exemplo n.º 1
0
        public IEnumerator SetServer(NetConnection server, NetZone zone)
        {
            zone.Assigned = true;

            if (server != Socket.Self) {
                var setServerRequest = Socket.Request.Send<string>("AssignZone", server, zone);

                yield return setServerRequest.WaitUntilDone;

                if (!setServerRequest.IsSuccessful) {
                    zone.Assigned = false;
                    yield break;
                }

                zone.PublicEndpoint = setServerRequest.Result;
            } else {
                NetZoneServer zoneServ = GetComponent<NetZoneServer>();
                zoneServ.AssignZoneSelf(zone);
            }

            zone.Server = server;
            zone.ServerEndpoint = zone.Server.Endpoint;
            if (string.IsNullOrEmpty(zone.PublicEndpoint)) zone.PublicEndpoint = zone.Server.Endpoint.ToString();
            ZoneAssigned(zone);
            NetLog.Info("Server assigned to zone. Endpoint: " + server.Endpoint);
            zone.Available = true;
        }
Exemplo n.º 2
0
 void DecrementPending(NetConnection conn)
 {
     pendingSetup[conn]--;
     if (pendingSetup[conn] > 0) return;
     pendingSetup.Remove(conn);
     NotifySuccess(conn);
 }
Exemplo n.º 3
0
 private void ConnectedToServer(NetConnection connection) {
     if (connection.Endpoint.ToString() == ServerAddress) {
         Debug.Log("Finished connection to main server: " + connection.Endpoint);
         server = connection;
     } else {
         Debug.Log("Finished connection to server: " + connection.Endpoint);
     }
 }
Exemplo n.º 4
0
 public bool TryGetAccount(NetConnection connection, out Account account) {
     if (!sessionLookup.ContainsKey(connection.Endpoint)) {
         account = null;
         return false;
     }
     account = sessions[sessionLookup[connection.Endpoint]];
     return true;
 }
Exemplo n.º 5
0
 /// <summary> Calculates the scope for the supplied connection against the scope of every view.
 /// This is useful, for example, when a connection is first created and an immediate population
 /// of views is desired. This can be detrimental when there is so much instantiation data that
 /// sending it all at once would cause an immediate send window overflow. </summary>
 internal void FullScopeCalculation(NetConnection connection) {
     for (int i = 0; i < ViewManager.Views.Count; i++) {
         var view = ViewManager.Views[i];
         if (view.Server != Socket.Self) continue;
         if (!connection.InGroup(view.Group) || !view.CanInstantiateFor(connection)) continue;
         if (!UpdateScope(connection.Scope, view) && !view.IsController(connection)) continue;
         view.SendInstantiateData(connection);
     }
 }
Exemplo n.º 6
0
 void ConnectToZone(int count, IPEndPoint ep, NetConnection conn) {
     if (!conn.IsServer) return;
     if (!pendingSetup.ContainsKey(conn)) pendingSetup.Add(conn, count);
     if (socket.EndpointConnected(ep)) DecrementPending(conn);
     else {
         pendingConn.Add(conn);
         pendingEp.Add(ep);
         if (!socket.ConnectingTo(ep)) socket.Connect(ep);
     }
 }
Exemplo n.º 7
0
 private void LoginRequest(string username, string password, NetConnection connection) {
     if (!InputValidator.IsValidEmail(username) || !InputValidator.IsValidPassword(password)) return;
     if (!InputValidator.LowercaseOnly(username)) return;
     foreach (Account account in accounts) {
         if (account.Username != username) continue;
         if (sessions.ContainsValue(account)) SendAlreadyLoggedIn(connection);
         else if (account.Password == password) SendLoginSuccess(account, connection);
         else SendBadCredentials(connection);
         return;
     }
     SendBadCredentials(connection);
 }
Exemplo n.º 8
0
 private void CreateAccountRequest(string username, string password, NetConnection connection) {
     if (!InputValidator.IsValidEmail(username) || !InputValidator.IsValidPassword(password)) return;
     if (!InputValidator.LowercaseOnly(username)) return;
     foreach (Account account in accounts) {
         if (account.Username != username) continue;
         SendEmailDuplicate(connection);
         return;
     }
     ulong randId = NetMath.RandomUlong();
     var newAcc = new Account(randId, username, password);
     accounts.Add(newAcc);
     SendLoginSuccess(newAcc, connection);
 }
Exemplo n.º 9
0
        /// <summary> Invokes RPC with parameters from the NetMessage. </summary>
        internal void Invoke(object instance, string methodName, NetMessage message, NetConnection sender) {

            RpcMethodInfo rpcInfo = RpcInfoCache.Get(methodName);
            MethodInfo method = rpcInfo.MethodInfoLookup[instance.GetType()];

            if (rpcInfo.TakesRequests && Socket.Request.Dispatch(message, sender, method, instance)) return;

            if (method.ReturnType == typeof (IEnumerator)) {
                var coroutine = (IEnumerator) method.Invoke(instance, message.Parameters);
                var behaviour = (MonoBehaviour) instance;
                if (coroutine != null) behaviour.StartCoroutine(coroutine);
            }
            else method.Invoke(instance, message.Parameters);
        }
Exemplo n.º 10
0
        /// <summary> Working with a given connection, a List of all views, SliceSize and Position,
        /// the connection has its scope updated for the given position/slice of the provided list
        /// of views. If views.Count == 512, Position == 64, and slice size == 32, connection will
        /// have scope updated for views occupying index 64 through 96 of the views List only. </summary>
        internal void UpdateScopeRange(NetConnection connection, List<NetView> views) {
            if (Position >= views.Count) Position = 0;

            int count = SliceSize;
            NetScope scope = connection.Scope;
            for (int i = Position; i < views.Count; i++) {
                var view = views[i];
                if (view != connection.View && view.Server == Socket.Self && connection.InGroup(view.Group) && view.CanInstantiateFor(connection) && UpdateScope(scope, view)) {
                    if (scope.In(view.Id)) view.SendInstantiateData(connection);
                    else ViewManager.SendOutOfScope(view.Id, connection);
                }
                count--;
                if (count == 0) break;
            }
        }
Exemplo n.º 11
0
 /// <summary> Releases a stream back to the pool for reuse. This should be called once a stream is no longer needed. </summary>
 public void Release() {
     if (Pool.Count > MaxSize) return;
     Reset();
     Connection = null;
     Socket = null;
     WriteLength = false;
     if (!Pool.Contains(this)) Pool.Enqueue(this);
 }
Exemplo n.º 12
0
 private void RemovePeer(NetZone zone, NetConnection connection) {
     if (!connection.IsPeer && !connection.IsServer) return;
     RemovePeerSelf(zone);
 }
Exemplo n.º 13
0
 private void AddPeer(NetZone zone, NetConnection connection) {
     if (!connection.IsPeer && !connection.IsServer) return;
     AddPeerSelf(zone);
     if (!Socket.EndpointConnected(zone.ServerEndpoint)) Socket.ConnectToPeer(zone.ServerEndpoint);
 }
Exemplo n.º 14
0
 /// <summary> Returns true if the supplied connection is in the controllers list. </summary>
 internal bool IsController(NetConnection connection)
 {
     return(Controllers.Contains(connection));
 }
Exemplo n.º 15
0
        /// <summary> Sends a request to the server for each local RPC method name that needs an ID assignment. </summary>
        internal void RequestAssignments(NetConnection connection) {

            foreach (string methodName in RpcInfoCache.RpcMethods().Keys) {
                if (HasId(methodName)) {
                    if (!HasName(NameToId(methodName))) idToName.Add(NameToId(methodName), methodName);
                    continue;
                }
                Socket.Command.Send((int) Cmd.AssignmentRequest, connection, methodName);
            }

            if (IdCount >= RpcInfoCache.Count) Socket.SendRequirementsMet(connection);
        }
Exemplo n.º 16
0
        /// <summary> Adds the assigned id and remote method name to the RemoteRpcIds dictionary. </summary>
        internal void ReceiveRemoteAssignment(NetMessage netMessage, NetConnection connection) {

            if (!connection.IsServer && !connection.IsPeer) return;

            var rpcId = (ushort)netMessage.Parameters[0];
            var rpcName = (string) netMessage.Parameters[1];

            if (!HasId(rpcName) && !HasName(rpcId)) {
                nameToId.Add(rpcName, rpcId);
                idToName.Add(rpcId, rpcName);
                foreach (NetConnection conn in Socket.Connections) {
                    if (conn == connection || conn.IsServer) continue;
                    Socket.Command.Send((int)Cmd.RemoteAssignment, conn, nameToId[rpcName], rpcName);
                }
            }

            WaitingForRpcs--;
            //NetLog.Trace("Waiting for RPCs: " + WaitingForRpcs);
            if (WaitingForRpcs != 0) return;

            if (IdCount >= RpcInfoCache.Count) Socket.SendRequirementsMet(connection);
            else RequestAssignments(connection);
        }
Exemplo n.º 17
0
 private void SpawnPlayer(NetConnection connection)
 {
     viewManager.CreateView(connection, 0, "ISOPlayer");
 }
Exemplo n.º 18
0
        /// <summary> Processes a request to assign an RPC method name to a numeric ID. </summary>
        internal void ReceiveAssignmentRequest(NetMessage netMessage, NetConnection connection) {
            // If we aren't responsible for RPC ID assignment, ignore request.
            if (!Socket.ProtocolAuthority) return;

            var rpcName = (string) netMessage.Parameters[0];
            if (!HasId(rpcName)) {
                if (NameCount > 1800) return;
                AssignRemoteRpc(rpcName);
                // Send new assignment to all connections.
                foreach (NetConnection conn in Socket.Connections) {
                    if (conn == connection || conn.IsServer) continue;
                    Socket.Command.Send((int) Cmd.RemoteAssignment, conn, nameToId[rpcName], rpcName);
                }
            }
            Socket.Command.Send((int) Cmd.AssignmentResponse, connection, nameToId[rpcName], rpcName);
        }
Exemplo n.º 19
0
 internal void SendOutOfScope(int viewId, NetConnection connection)
 {
     Socket.Command.Send((int)ViewCmd.OutOfScope, connection, viewId);
 }
Exemplo n.º 20
0
        public NetView CreateView(NetConnection controller, string prefabBase, NetStream instantiateData)
        {
            NetView view = CreateView(controller, 0, prefabBase, instantiateData);

            return(view);
        }
Exemplo n.º 21
0
 /// <summary> Authoritatively creates a view for a connected client and triggers network instantiation. </summary>
 public NetView CreateView(NetConnection controller, string prefabBase)
 {
     return(CreateView(controller, 0, prefabBase));
 }
Exemplo n.º 22
0
 /// <summary> Adds the provided connection to the controller list, allowing connection to send RPCs to this view. </summary>
 internal void AddController(NetConnection connection)
 {
     Controllers.Add(connection);
 }
Exemplo n.º 23
0
 private void SpawnRequest(NetConnection connection)
 {
     SpawnPlayer(connection);
 }
Exemplo n.º 24
0
 private void PeerConnected(NetConnection connection)
 {
     if (!peers.Contains(connection)) peers.Add(connection);
     if (Authority) AddServer(connection);
 }
Exemplo n.º 25
0
 private void ClientDisconnected(NetConnection connection)
 {
     viewManager.DestroyAuthorizedViews(connection);
 }
Exemplo n.º 26
0
 private void RemoveServer(NetConnection server)
 {
     for (int i = 0; i < zones.Count; i++) {
         if (zones[i].Server == server) zones[i].RemoveServer();
     }
     if (unassignedPeers.ContainsKey(server)) unassignedPeers.Remove(server);
 }
Exemplo n.º 27
0
        /// <summary> Processes a response to an RPC assignment request. The assigned id and method name are added to the LocalRpcs dictionary. </summary>
        internal void ReceiveAssignmentResponse(NetMessage netMessage, NetConnection connection) {

            if (!connection.IsServer && !connection.IsPeer) return;

            var id = (ushort) netMessage.Parameters[0];
            var methodName = (string) netMessage.Parameters[1];

            if (RpcInfoCache.Exists(methodName) && !idToName.ContainsKey(id)) {
                idToName.Add(id, methodName);
                if (!nameToId.ContainsKey(methodName)) nameToId.Add(methodName, id);
            }
            else NetLog.Error("Cannot assign local RPC. ID: " + id + " MethodName: " + methodName);

            if (idToName.Count == RpcInfoCache.Count) Socket.SendRequirementsMet(connection);
        }
Exemplo n.º 28
0
 void ConnectedToZone(NetConnection connection)
 {
     RemoveEndpoint(connection.Endpoint);
 }
Exemplo n.º 29
0
        /// <summary> Sends a message for each RPC method names and IDs to the supplied connection. </summary>
        internal void SendLocalAssignments(NetConnection connection) {

            foreach (KeyValuePair<ushort, string> kvp in idToName) {
                Socket.Command.Send((int) Cmd.RemoteAssignment, connection, kvp.Key, kvp.Value);
            }
        }
Exemplo n.º 30
0
 private string AssignZone(NetZone zone, NetConnection connection) {
     if (!connection.IsPeer && !connection.IsServer) return "";
     AssignZoneSelf(zone);
     return Socket.Address;
 }
Exemplo n.º 31
0
 private void ClientConnected(NetConnection connection) {
     SendPeers(connection);
 }
Exemplo n.º 32
0
 /// <summary>
 /// Sends a reliable RPC. This should be used for infrequent messages which require guaranteed delivery.
 /// Reliable should never be used for state-sync due to the increased bandwidth, garbage, and CPU overhead.
 /// </summary>
 /// <param name="methodName"> The name of the RPC method. </param>
 /// <param name="target"> The NetConnection to send the RPC to. </param>
 /// <param name="parameters">The parameters to send.</param>
 public void SendReliable(string methodName, NetConnection target, params object[] parameters)
 {
     ViewManager.Send(Id, true, methodName, target, parameters);
 }
Exemplo n.º 33
0
 private void AddServer(NetConnection server)
 {
     unassignedPeers.Add(server, false);
     AssignServers();
 }
Exemplo n.º 34
0
 void ZoneConnectSuccess(NetConnection connection) {
     if (OnClientSuccess != null) OnClientSuccess(connection);
 }
Exemplo n.º 35
0
 private void PeerDisconnected(NetConnection connection)
 {
     NetLog.Info("ZoneManager: Peer Disconnected: " + connection.Endpoint);
     if (peers.Contains(connection)) peers.Remove(connection);
     if (Authority) RemoveServer(connection);
 }
Exemplo n.º 36
0
 void ZoneConnectFail(IPEndPoint ep, NetConnection connection) {
     if (OnClientFailed != null) OnClientFailed(connection);
 }
Exemplo n.º 37
0
 private void SendPeers(NetConnection connection) {
     foreach (NetZone zone in peers) {
         Socket.Send("ConnectToZone", connection, peers.Count, zone.Server.Endpoint);
     }
 }
Exemplo n.º 38
0
 public Request <T> Send <T>(string methodName, NetConnection target, params object[] parameters)
 {
     return(Send <T>(0, methodName, target, parameters));
 }