Skip to content

Commit fa85e53

Browse files
committed
Added shutdown functions and changed protection level of API
1 parent aa10528 commit fa85e53

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected NetworkedObject networkedObject
4747
}
4848
}
4949
private NetworkedObject _networkedObject = null;
50-
public uint objectNetworkId
50+
public uint networkId
5151
{
5252
get
5353
{
@@ -66,14 +66,14 @@ public int ownerClientId
6666
//Change data type
6767
private Dictionary<string, int> registeredMessageHandlers = new Dictionary<string, int>();
6868

69-
public int RegisterMessageHandler(string name, Action<int, byte[]> action)
69+
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
7070
{
7171
int counter = MessageManager.AddIncomingMessageHandler(name, action);
7272
registeredMessageHandlers.Add(name, counter);
7373
return counter;
7474
}
7575

76-
public void DeregisterMessageHandler(string name, int counter)
76+
protected void DeregisterMessageHandler(string name, int counter)
7777
{
7878
MessageManager.RemoveIncomingMessageHandler(name, counter);
7979
}
@@ -86,7 +86,7 @@ private void OnDestroy()
8686
}
8787
}
8888

89-
public void SendToServer(string messageType, string channelName, byte[] data)
89+
protected void SendToServer(string messageType, string channelName, byte[] data)
9090
{
9191
if (isServer)
9292
{
@@ -98,7 +98,7 @@ public void SendToServer(string messageType, string channelName, byte[] data)
9898
}
9999
}
100100

101-
public void SendToLocalClient(string messageType, string channelName, byte[] data)
101+
protected void SendToLocalClient(string messageType, string channelName, byte[] data)
102102
{
103103
if (!isServer)
104104
{
@@ -108,7 +108,7 @@ public void SendToLocalClient(string messageType, string channelName, byte[] dat
108108
NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data);
109109
}
110110

111-
public void SendToNonLocalClients(string messageType, string channelName, byte[] data)
111+
protected void SendToNonLocalClients(string messageType, string channelName, byte[] data)
112112
{
113113
if (!isServer)
114114
{
@@ -118,7 +118,7 @@ public void SendToNonLocalClients(string messageType, string channelName, byte[]
118118
NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId);
119119
}
120120

121-
public void SendToClient(int clientId, string messageType, string channelName, byte[] data)
121+
protected void SendToClient(int clientId, string messageType, string channelName, byte[] data)
122122
{
123123
if (!isServer)
124124
{
@@ -128,7 +128,7 @@ public void SendToClient(int clientId, string messageType, string channelName, b
128128
NetworkingManager.singleton.Send(clientId, messageType, channelName, data);
129129
}
130130

131-
public void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
131+
protected void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
132132
{
133133
if (!isServer)
134134
{
@@ -138,7 +138,7 @@ public void SendToClients(int[] clientIds, string messageType, string channelNam
138138
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
139139
}
140140

141-
public void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
141+
protected void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
142142
{
143143
if (!isServer)
144144
{
@@ -148,7 +148,7 @@ public void SendToClients(List<int> clientIds, string messageType, string channe
148148
NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
149149
}
150150

151-
public void SendToClients(string messageType, string channelName, byte[] data)
151+
protected void SendToClients(string messageType, string channelName, byte[] data)
152152
{
153153
if (!isServer)
154154
{
@@ -158,7 +158,7 @@ public void SendToClients(string messageType, string channelName, byte[] data)
158158
NetworkingManager.singleton.Send(messageType, channelName, data);
159159
}
160160

161-
public NetworkedObject GetNetworkedObject(uint networkId)
161+
protected NetworkedObject GetNetworkedObject(uint networkId)
162162
{
163163
return SpawnManager.spawnedObjects[networkId];
164164
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,41 @@ public void StartClient(NetworkingConfiguration netConfig)
147147
serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
148148
}
149149

150+
public void StopServer()
151+
{
152+
HashSet<int> sentIds = new HashSet<int>();
153+
//Don't know if I have to disconnect the clients. I'm assuming the NetworkTransport does all the cleaning on shtudown. But this way the clients get a disconnect message from server (so long it does't get lost)
154+
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
155+
{
156+
if(!sentIds.Contains(pair.Key))
157+
{
158+
sentIds.Add(pair.Key);
159+
NetworkTransport.Disconnect(hostId, pair.Key, out error);
160+
}
161+
}
162+
foreach (int clientId in pendingClients)
163+
{
164+
if (!sentIds.Contains(clientId))
165+
{
166+
sentIds.Add(clientId);
167+
NetworkTransport.Disconnect(hostId, clientId, out error);
168+
}
169+
}
170+
Shutdown();
171+
}
172+
173+
public void StopHost()
174+
{
175+
StopServer();
176+
//We don't stop client since we dont actually have a transport connection to our own host. We just handle host messages directly in the MLAPI
177+
}
178+
179+
public void StopClient()
180+
{
181+
NetworkTransport.Disconnect(hostId, serverClientId, out error);
182+
Shutdown();
183+
}
184+
150185
public void StartHost(NetworkingConfiguration netConfig)
151186
{
152187
ConnectionConfig cConfig = Init(netConfig);
@@ -188,6 +223,15 @@ private void OnEnable()
188223
private void OnDestroy()
189224
{
190225
singleton = null;
226+
Shutdown();
227+
}
228+
229+
private void Shutdown()
230+
{
231+
isListening = false;
232+
isClient = false;
233+
isServer = false;
234+
NetworkTransport.Shutdown();
191235
}
192236

193237
//Receive stuff
@@ -602,7 +646,7 @@ internal void Send(string messageType, string channelName, byte[] data, int clie
602646
}
603647
}
604648

605-
internal void DisconnectClient(int clientId)
649+
private void DisconnectClient(int clientId)
606650
{
607651
if (!isServer)
608652
return;

0 commit comments

Comments
 (0)