Renamed connectionId to clientId

This commit is contained in:
Albin Corén 2018-01-07 05:24:54 +01:00
parent 6465eb2142
commit 24c03f6324
2 changed files with 78 additions and 74 deletions

View File

@ -49,7 +49,7 @@ namespace MLAPI
} }
} }
public int ownerConnectionId public int ownerClientId
{ {
get get
{ {
@ -87,7 +87,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from server to server is not yet supported"); Debug.LogWarning("MLAPI: Sending messages from server to server is not yet supported");
return; return;
} }
NetworkingManager.singleton.Send(NetworkingManager.singleton.localConnectionId, messageType, channelName, data); NetworkingManager.singleton.Send(NetworkingManager.singleton.serverClientId, messageType, channelName, data);
} }
public void SendToLocalClient(string messageType, string channelName, byte[] data) public void SendToLocalClient(string messageType, string channelName, byte[] data)
@ -97,7 +97,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported"); Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return; return;
} }
NetworkingManager.singleton.Send(ownerConnectionId, messageType, channelName, data); NetworkingManager.singleton.Send(ownerClientId, messageType, channelName, data);
} }
public void SendToNonLocalClients(string messageType, string channelName, byte[] data) public void SendToNonLocalClients(string messageType, string channelName, byte[] data)
@ -107,37 +107,37 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported"); Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return; return;
} }
NetworkingManager.singleton.Send(messageType, channelName, data, ownerConnectionId); NetworkingManager.singleton.Send(messageType, channelName, data, ownerClientId);
} }
public void SendToClient(int connectionId, string messageType, string channelName, byte[] data) public void SendToClient(int clientId, string messageType, string channelName, byte[] data)
{ {
if (!isServer) if (!isServer)
{ {
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported"); Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return; return;
} }
NetworkingManager.singleton.Send(connectionId, messageType, channelName, data); NetworkingManager.singleton.Send(clientId, messageType, channelName, data);
} }
public void SendToClients(int[] connectionIds, string messageType, string channelName, byte[] data) public void SendToClients(int[] clientIds, string messageType, string channelName, byte[] data)
{ {
if (!isServer) if (!isServer)
{ {
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported"); Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return; return;
} }
NetworkingManager.singleton.Send(connectionIds, messageType, channelName, data); NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
} }
public void SendToClients(List<int> connectionIds, string messageType, string channelName, byte[] data) public void SendToClients(List<int> clientIds, string messageType, string channelName, byte[] data)
{ {
if (!isServer) if (!isServer)
{ {
Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported"); Debug.LogWarning("MLAPI: Sending messages from client to other clients is not yet supported");
return; return;
} }
NetworkingManager.singleton.Send(connectionIds, messageType, channelName, data); NetworkingManager.singleton.Send(clientIds, messageType, channelName, data);
} }
public void SendToClients(string messageType, string channelName, byte[] data) public void SendToClients(string messageType, string channelName, byte[] data)

View File

@ -41,7 +41,7 @@ namespace MLAPI
private Dictionary<ushort, Dictionary<int, Action<int, byte[]>>> messageCallbacks; private Dictionary<ushort, Dictionary<int, Action<int, byte[]>>> messageCallbacks;
private Dictionary<ushort, int> messageHandlerCounter; private Dictionary<ushort, int> messageHandlerCounter;
private Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters; private Dictionary<ushort, Stack<int>> releasedMessageHandlerCounters;
public int localConnectionId; internal int serverClientId;
public int PlaySceneIndex; public int PlaySceneIndex;
public int MenuSceneIndex; public int MenuSceneIndex;
internal Dictionary<uint, NetworkedObject> spawnedObjects; internal Dictionary<uint, NetworkedObject> spawnedObjects;
@ -107,7 +107,7 @@ namespace MLAPI
return go; return go;
} }
internal GameObject SpawnPlayerObject(int connectionId, uint networkId) internal GameObject SpawnPlayerObject(int clientId, uint networkId)
{ {
GameObject go = Instantiate(DefaultPlayerPrefab); GameObject go = Instantiate(DefaultPlayerPrefab);
NetworkedObject netObject = go.GetComponent<NetworkedObject>(); NetworkedObject netObject = go.GetComponent<NetworkedObject>();
@ -116,7 +116,7 @@ namespace MLAPI
Debug.LogWarning("MLAPI: Please add a NetworkedObject component to the root of the player prefab"); Debug.LogWarning("MLAPI: Please add a NetworkedObject component to the root of the player prefab");
netObject = go.AddComponent<NetworkedObject>(); netObject = go.AddComponent<NetworkedObject>();
} }
netObject.OwnerClientId = connectionId; netObject.OwnerClientId = clientId;
if(isServer) if(isServer)
{ {
netObject.NetworkId = singleton.GetNetworkObjectId(); netObject.NetworkId = singleton.GetNetworkObjectId();
@ -126,7 +126,7 @@ namespace MLAPI
netObject.NetworkId = networkId; netObject.NetworkId = networkId;
} }
netObject.IsPlayerObject = true; netObject.IsPlayerObject = true;
connectedClients[connectionId].PlayerObject = go; connectedClients[clientId].PlayerObject = go;
singleton.spawnedObjectIds.Add(netObject.NetworkId); singleton.spawnedObjectIds.Add(netObject.NetworkId);
singleton.spawnedObjects.Add(netObject.NetworkId, netObject); singleton.spawnedObjects.Add(netObject.NetworkId, netObject);
return go; return go;
@ -288,7 +288,7 @@ namespace MLAPI
isServer = false; isServer = false;
isClient = true; isClient = true;
isListening = true; isListening = true;
localConnectionId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error); serverClientId = NetworkTransport.Connect(hostId, NetworkConfig.Address, NetworkConfig.Port, 0, out error);
} }
public void StartHost(NetworkingConfiguration netConfig) public void StartHost(NetworkingConfiguration netConfig)
@ -308,6 +308,10 @@ namespace MLAPI
isClient = true; isClient = true;
isListening = true; isListening = true;
connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 }); connectedClients.Add(-1, new NetworkedClient() { ClientId = -1 });
if(NetworkConfig.HandleObjectSpawning)
{
SpawnPlayerObject(-1, 0);
}
} }
private void OnEnable() private void OnEnable()
@ -329,7 +333,7 @@ namespace MLAPI
//Receive stuff //Receive stuff
int hostId; int hostId;
int connectionId; int clientId;
int channelId; int channelId;
int receivedSize; int receivedSize;
byte error; byte error;
@ -342,14 +346,14 @@ namespace MLAPI
do do
{ {
messagesProcessed++; messagesProcessed++;
eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error); eventType = NetworkTransport.Receive(out hostId, out clientId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error);
NetworkError networkError = (NetworkError)error; NetworkError networkError = (NetworkError)error;
if(networkError == NetworkError.Timeout) if(networkError == NetworkError.Timeout)
{ {
//Client timed out. //Client timed out.
if (isServer) if (isServer)
{ {
OnClientDisconnect(connectionId); OnClientDisconnect(clientId);
continue; continue;
} }
} }
@ -363,8 +367,8 @@ namespace MLAPI
case NetworkEventType.ConnectEvent: case NetworkEventType.ConnectEvent:
if (isServer) if (isServer)
{ {
pendingClients.Add(connectionId); pendingClients.Add(clientId);
StartCoroutine(ApprovalTimeout(connectionId)); StartCoroutine(ApprovalTimeout(clientId));
} }
else else
{ {
@ -379,17 +383,17 @@ namespace MLAPI
writer.Write(NetworkConfig.ConnectionData); writer.Write(NetworkConfig.ConnectionData);
} }
} }
Send(connectionId, "MLAPI_CONNECTION_REQUEST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray()); Send(clientId, "MLAPI_CONNECTION_REQUEST", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray());
} }
} }
break; break;
case NetworkEventType.DataEvent: case NetworkEventType.DataEvent:
HandleIncomingData(connectionId, messageBuffer); HandleIncomingData(clientId, messageBuffer);
break; break;
case NetworkEventType.DisconnectEvent: case NetworkEventType.DisconnectEvent:
if(isServer) if(isServer)
{ {
OnClientDisconnect(connectionId); OnClientDisconnect(clientId);
} }
break; break;
} }
@ -399,18 +403,18 @@ namespace MLAPI
} }
} }
IEnumerator ApprovalTimeout(int connectionId) IEnumerator ApprovalTimeout(int clientId)
{ {
float timeStarted = Time.time; float timeStarted = Time.time;
//We yield every frame incase a pending client disconnects and someone else gets its connection id //We yield every frame incase a pending client disconnects and someone else gets its connection id
while (Time.time - timeStarted < NetworkConfig.ClientConnectionBufferTimeout && pendingClients.Contains(connectionId)) while (Time.time - timeStarted < NetworkConfig.ClientConnectionBufferTimeout && pendingClients.Contains(clientId))
{ {
yield return null; yield return null;
} }
if(pendingClients.Contains(connectionId) && !connectedClients.ContainsKey(connectionId)) if(pendingClients.Contains(clientId) && !connectedClients.ContainsKey(clientId))
{ {
//Timeout //Timeout
DisconnectClient(connectionId); DisconnectClient(clientId);
} }
} }
@ -423,9 +427,9 @@ namespace MLAPI
ushort messageType = reader.ReadUInt16(); ushort messageType = reader.ReadUInt16();
//Client tried to send a network message that was not the connection request before he was accepted. //Client tried to send a network message that was not the connection request before he was accepted.
if (isServer && pendingClients.Contains(connectionId) && messageType != 0) if (isServer && pendingClients.Contains(clientId) && messageType != 0)
{ {
Debug.LogWarning("MLAPI: Message recieved from connectionId " + connectionId + " before it has been accepted"); Debug.LogWarning("MLAPI: Message recieved from clientId " + clientId + " before it has been accepted");
return; return;
} }
@ -436,7 +440,7 @@ namespace MLAPI
//Custom message, invoke all message handlers //Custom message, invoke all message handlers
foreach (KeyValuePair<int, Action<int, byte[]>> pair in messageCallbacks[messageType]) foreach (KeyValuePair<int, Action<int, byte[]>> pair in messageCallbacks[messageType])
{ {
pair.Value(connectionId, incommingData); pair.Value(clientId, incommingData);
} }
} }
else else
@ -455,18 +459,18 @@ namespace MLAPI
if (!NetworkConfig.CompareConfig(configHash)) if (!NetworkConfig.CompareConfig(configHash))
{ {
Debug.LogWarning("MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match."); Debug.LogWarning("MLAPI: NetworkConfiguration missmatch. The configuration between the server and client does not match.");
DisconnectClient(connectionId); DisconnectClient(clientId);
return; return;
} }
if (NetworkConfig.ConnectionApproval) if (NetworkConfig.ConnectionApproval)
{ {
ushort bufferSize = messageReader.ReadUInt16(); ushort bufferSize = messageReader.ReadUInt16();
byte[] connectionBuffer = messageReader.ReadBytes(bufferSize); byte[] connectionBuffer = messageReader.ReadBytes(bufferSize);
NetworkConfig.ConnectionApprovalCallback(connectionBuffer, connectionId, HandleApproval); NetworkConfig.ConnectionApprovalCallback(connectionBuffer, clientId, HandleApproval);
} }
else else
{ {
HandleApproval(connectionId, true); HandleApproval(clientId, true);
} }
} }
} }
@ -566,7 +570,7 @@ namespace MLAPI
} }
} }
internal void Send(int connectionId, string messageType, string channelName, byte[] data) internal void Send(int clientId, string messageType, string channelName, byte[] data)
{ {
using (MemoryStream stream = new MemoryStream()) using (MemoryStream stream = new MemoryStream())
{ {
@ -579,11 +583,11 @@ namespace MLAPI
//2 bytes for message type and 2 bytes for byte length //2 bytes for message type and 2 bytes for byte length
int size = data.Length + 4; int size = data.Length + 4;
byte[] dataToSend = stream.ToArray(); byte[] dataToSend = stream.ToArray();
NetworkTransport.Send(hostId, connectionId, channels[channelName], dataToSend, dataToSend.Length, out error); NetworkTransport.Send(hostId, clientId, channels[channelName], dataToSend, dataToSend.Length, out error);
} }
} }
internal void Send(int[] connectionIds, string messageType, string channelName, byte[] data) internal void Send(int[] clientIds, string messageType, string channelName, byte[] data)
{ {
using (MemoryStream stream = new MemoryStream()) using (MemoryStream stream = new MemoryStream())
{ {
@ -597,14 +601,14 @@ namespace MLAPI
int size = data.Length + 4; int size = data.Length + 4;
byte[] dataToSend = stream.ToArray(); byte[] dataToSend = stream.ToArray();
int channel = channels[channelName]; int channel = channels[channelName];
for (int i = 0; i < connectionIds.Length; i++) for (int i = 0; i < clientIds.Length; i++)
{ {
NetworkTransport.Send(hostId, connectionIds[i], channel, dataToSend, size, out error); NetworkTransport.Send(hostId, clientIds[i], channel, dataToSend, size, out error);
} }
} }
} }
internal void Send(List<int> connectionIds, string messageType, string channelName, byte[] data) internal void Send(List<int> clientIds, string messageType, string channelName, byte[] data)
{ {
using (MemoryStream stream = new MemoryStream()) using (MemoryStream stream = new MemoryStream())
{ {
@ -618,9 +622,9 @@ namespace MLAPI
int size = data.Length + 4; int size = data.Length + 4;
byte[] dataToSend = stream.ToArray(); byte[] dataToSend = stream.ToArray();
int channel = channels[channelName]; int channel = channels[channelName];
for (int i = 0; i < connectionIds.Count; i++) for (int i = 0; i < clientIds.Count; i++)
{ {
NetworkTransport.Send(hostId, connectionIds[i], channel, dataToSend, size, out error); NetworkTransport.Send(hostId, clientIds[i], channel, dataToSend, size, out error);
} }
} }
} }
@ -646,7 +650,7 @@ namespace MLAPI
} }
} }
internal void Send(string messageType, string channelName, byte[] data, int connectionIdToIgnore) internal void Send(string messageType, string channelName, byte[] data, int clientIdToIgnore)
{ {
using (MemoryStream stream = new MemoryStream()) using (MemoryStream stream = new MemoryStream())
{ {
@ -662,35 +666,35 @@ namespace MLAPI
int channel = channels[channelName]; int channel = channels[channelName];
foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients) foreach (KeyValuePair<int, NetworkedClient> pair in connectedClients)
{ {
if (pair.Key == connectionIdToIgnore) if (pair.Key == clientIdToIgnore)
continue; continue;
NetworkTransport.Send(hostId, pair.Key, channel, dataToSend, size, out error); NetworkTransport.Send(hostId, pair.Key, channel, dataToSend, size, out error);
} }
} }
} }
internal void DisconnectClient(int connectionId) internal void DisconnectClient(int clientId)
{ {
if (!isServer) if (!isServer)
return; return;
if (pendingClients.Contains(connectionId)) if (pendingClients.Contains(clientId))
pendingClients.Remove(connectionId); pendingClients.Remove(clientId);
if (connectedClients.ContainsKey(connectionId)) if (connectedClients.ContainsKey(clientId))
connectedClients.Remove(connectionId); connectedClients.Remove(clientId);
NetworkTransport.Disconnect(hostId, connectionId, out error); NetworkTransport.Disconnect(hostId, clientId, out error);
} }
private void OnClientDisconnect(int connectionId) private void OnClientDisconnect(int clientId)
{ {
if (pendingClients.Contains(connectionId)) if (pendingClients.Contains(clientId))
pendingClients.Remove(connectionId); pendingClients.Remove(clientId);
if (connectedClients.ContainsKey(connectionId)) if (connectedClients.ContainsKey(clientId))
{ {
if(NetworkConfig.HandleObjectSpawning) if(NetworkConfig.HandleObjectSpawning)
{ {
Destroy(connectedClients[connectionId].PlayerObject); Destroy(connectedClients[clientId].PlayerObject);
} }
connectedClients.Remove(connectionId); connectedClients.Remove(clientId);
} }
if (isServer) if (isServer)
@ -699,42 +703,42 @@ namespace MLAPI
{ {
using (BinaryWriter writer = new BinaryWriter(stream)) using (BinaryWriter writer = new BinaryWriter(stream))
{ {
writer.Write(connectionId); writer.Write(clientId);
} }
Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray(), connectionId); Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray(), clientId);
} }
} }
} }
private void HandleApproval(int connectionId, bool approved) private void HandleApproval(int clientId, bool approved)
{ {
if(approved) if(approved)
{ {
//Inform new client it got approved //Inform new client it got approved
if (pendingClients.Contains(connectionId)) if (pendingClients.Contains(clientId))
pendingClients.Remove(connectionId); pendingClients.Remove(clientId);
NetworkedClient client = new NetworkedClient() NetworkedClient client = new NetworkedClient()
{ {
ClientId = connectionId ClientId = clientId
}; };
connectedClients.Add(connectionId, client); connectedClients.Add(clientId, client);
if(NetworkConfig.HandleObjectSpawning) if(NetworkConfig.HandleObjectSpawning)
{ {
uint networkId = GetNetworkObjectId(); uint networkId = GetNetworkObjectId();
GameObject go = SpawnPlayerObject(connectionId, networkId); GameObject go = SpawnPlayerObject(clientId, networkId);
connectedClients[connectionId].PlayerObject = go; connectedClients[clientId].PlayerObject = go;
} }
using (MemoryStream writeStream = new MemoryStream()) using (MemoryStream writeStream = new MemoryStream())
{ {
using (BinaryWriter writer = new BinaryWriter(writeStream)) using (BinaryWriter writer = new BinaryWriter(writeStream))
{ {
writer.Write(connectionId); writer.Write(clientId);
writer.Write(connectedClients.Count - 1); writer.Write(connectedClients.Count - 1);
foreach (KeyValuePair<int, NetworkedClient> item in connectedClients) foreach (KeyValuePair<int, NetworkedClient> item in connectedClients)
{ {
//Our own ID. Already added as the first one above //Our own ID. Already added as the first one above
if (item.Key == connectionId) if (item.Key == clientId)
continue; continue;
writer.Write(item.Key); //Connection id writer.Write(item.Key); //Connection id
} }
@ -750,7 +754,7 @@ namespace MLAPI
} }
} }
} }
Send(connectionId, "MLAPI_CONNECTION_APPROVED", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray()); Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_RELIABLE_FRAGMENTED", writeStream.ToArray());
} }
//Inform old clients of the new player //Inform old clients of the new player
@ -761,23 +765,23 @@ namespace MLAPI
if (NetworkConfig.HandleObjectSpawning) if (NetworkConfig.HandleObjectSpawning)
{ {
writer.Write(true); writer.Write(true);
writer.Write(connectedClients[connectionId].PlayerObject.GetComponent<NetworkedObject>().NetworkId); writer.Write(connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().NetworkId);
writer.Write(connectionId); writer.Write(clientId);
writer.Write(-1); writer.Write(-1);
} }
else else
{ {
writer.Write(connectionId); writer.Write(clientId);
} }
} }
Send("MLAPI_ADD_OBJECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray(), connectionId); Send("MLAPI_ADD_OBJECT", "MLAPI_RELIABLE_FRAGMENTED", stream.ToArray(), clientId);
} }
} }
else else
{ {
if (pendingClients.Contains(connectionId)) if (pendingClients.Contains(clientId))
pendingClients.Remove(connectionId); pendingClients.Remove(clientId);
NetworkTransport.Disconnect(hostId, connectionId, out error); NetworkTransport.Disconnect(hostId, clientId, out error);
} }
} }
} }