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; }
void DecrementPending(NetConnection conn) { pendingSetup[conn]--; if (pendingSetup[conn] > 0) return; pendingSetup.Remove(conn); NotifySuccess(conn); }
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); } }
public bool TryGetAccount(NetConnection connection, out Account account) { if (!sessionLookup.ContainsKey(connection.Endpoint)) { account = null; return false; } account = sessions[sessionLookup[connection.Endpoint]]; return true; }
/// <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); } }
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); } }
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); }
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); }
/// <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); }
/// <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; } }
/// <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); }
private void RemovePeer(NetZone zone, NetConnection connection) { if (!connection.IsPeer && !connection.IsServer) return; RemovePeerSelf(zone); }
private void AddPeer(NetZone zone, NetConnection connection) { if (!connection.IsPeer && !connection.IsServer) return; AddPeerSelf(zone); if (!Socket.EndpointConnected(zone.ServerEndpoint)) Socket.ConnectToPeer(zone.ServerEndpoint); }
/// <summary> Returns true if the supplied connection is in the controllers list. </summary> internal bool IsController(NetConnection connection) { return(Controllers.Contains(connection)); }
/// <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); }
/// <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); }
private void SpawnPlayer(NetConnection connection) { viewManager.CreateView(connection, 0, "ISOPlayer"); }
/// <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); }
internal void SendOutOfScope(int viewId, NetConnection connection) { Socket.Command.Send((int)ViewCmd.OutOfScope, connection, viewId); }
public NetView CreateView(NetConnection controller, string prefabBase, NetStream instantiateData) { NetView view = CreateView(controller, 0, prefabBase, instantiateData); return(view); }
/// <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)); }
/// <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); }
private void SpawnRequest(NetConnection connection) { SpawnPlayer(connection); }
private void PeerConnected(NetConnection connection) { if (!peers.Contains(connection)) peers.Add(connection); if (Authority) AddServer(connection); }
private void ClientDisconnected(NetConnection connection) { viewManager.DestroyAuthorizedViews(connection); }
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); }
/// <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); }
void ConnectedToZone(NetConnection connection) { RemoveEndpoint(connection.Endpoint); }
/// <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); } }
private string AssignZone(NetZone zone, NetConnection connection) { if (!connection.IsPeer && !connection.IsServer) return ""; AssignZoneSelf(zone); return Socket.Address; }
private void ClientConnected(NetConnection connection) { SendPeers(connection); }
/// <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); }
private void AddServer(NetConnection server) { unassignedPeers.Add(server, false); AssignServers(); }
void ZoneConnectSuccess(NetConnection connection) { if (OnClientSuccess != null) OnClientSuccess(connection); }
private void PeerDisconnected(NetConnection connection) { NetLog.Info("ZoneManager: Peer Disconnected: " + connection.Endpoint); if (peers.Contains(connection)) peers.Remove(connection); if (Authority) RemoveServer(connection); }
void ZoneConnectFail(IPEndPoint ep, NetConnection connection) { if (OnClientFailed != null) OnClientFailed(connection); }
private void SendPeers(NetConnection connection) { foreach (NetZone zone in peers) { Socket.Send("ConnectToZone", connection, peers.Count, zone.Server.Endpoint); } }
public Request <T> Send <T>(string methodName, NetConnection target, params object[] parameters) { return(Send <T>(0, methodName, target, parameters)); }