private static int[] GetIds(SocketKind socket) { int[] ids; int counter = 0; Dictionary <int, Socket> dict; if (socket == SocketKind.Unity) { dict = unityClients; } else { dict = pythonClients; } ids = new int[dict.Count]; foreach (KeyValuePair <int, Socket> pair in dict) { ids[counter] = pair.Key; counter++; } return(ids); }
public SocketData(WebSocket socket, SocketKind kind, IPAddress address, string offer) { Id = Guid.NewGuid(); SocketKind = kind; Address = address; Offer = offer; Socket = socket; }
private static void HandleLine(SocketKind type, string message) { NetworkData messageData = JsonUtility.FromJson <NetworkData>(message); int senderId = messageData.ID_Sender; int[] recvIds = messageData.ID_Receiver; if (type == SocketKind.Python) { List <int> toRemove = new List <int>(); foreach (int id in recvIds) { if (unityClients.ContainsKey(id)) { Socket s = unityClients[id]; bool sent = Send(s, message + "\n"); if (!sent) { toRemove.Add(id); } } } foreach (int s in toRemove) { unityClients.Remove(s); Debug.LogWarning("AsyncServer: Removed a Unity client from the list."); } } else if (type == SocketKind.Unity) { List <int> toRemove = new List <int>(); foreach (int id in messageData.ID_Receiver) { if (pythonClients.ContainsKey(id)) { Socket s = pythonClients[id]; bool sent = Send(s, message + "\n"); if (!sent) { toRemove.Add(id); } } } foreach (int s in toRemove) { pythonClients.Remove(s); Debug.LogWarning("AsyncServer: Removed a Python client from the list."); } } else { Debug.LogError("AsyncServer: Unrecognized client type " + type.ToString() + ". Message will not be sent."); } }
private static void ReadCallback(SocketKind type, DataBuffer buffer, Socket handler, int bytesRead) { // Read the data from the client socket if (bytesRead > 0) { buffer.str += Encoding.ASCII.GetString(buffer.buffer, 0, bytesRead); // Check that this is the end of the message if (buffer.str.IndexOf("}", StringComparison.CurrentCulture) == -1) { // Need to continue getting more data Debug.Log("AsyncServer: Incomplete data was received. Getting more data..."); if (type == SocketKind.Python) { handler.BeginReceive(buffer.buffer, 0, DataBuffer.bufferSize, 0, new AsyncCallback(PythonCallback), buffer); } else { handler.BeginReceive(buffer.buffer, 0, DataBuffer.bufferSize, 0, new AsyncCallback(UnityCallback), buffer); } return; } // Process each line separately until we have no full lines remaining while (buffer.str.IndexOf("}", StringComparison.CurrentCulture) != -1) { int index = buffer.str.IndexOf("}", StringComparison.CurrentCulture); string line = buffer.str.Substring(0, index + 1); buffer.str = buffer.str.Remove(0, index + 2); HandleLine(type, line); } } }