void connect() { try { // get policy if we are on the web or in editor //if ((Application.platform == RuntimePlatform.WindowsWebPlayer) || (Application.platform == RuntimePlatform.WindowsEditor)) if ((Application.platform == RuntimePlatform.WebGLPlayer) || (Application.platform == RuntimePlatform.WindowsEditor)) {/////////////////// //Security.PrefetchSocketPolicy(ipAddress, pfrPort); } cSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); cSock.Connect(new IPEndPoint(IPAddress.Parse(ipAddress), sPort)); clientConnection gsCon = new clientConnection(cSock); } catch { Debug.Log("Unable to connect to server."); } }
//----------------- //Below methods are responible for server connection private void connect() { try { // get policy if we are on the web or in editor if ((Application.platform == RuntimePlatform.WindowsWebPlayer) || (Application.platform == RuntimePlatform.WindowsEditor)) { Security.PrefetchSocketPolicy(ipAddress, pfrPort); } cSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); cSock.Connect(new IPEndPoint(IPAddress.Parse(ipAddress), sPort)); clientConnection gsCon = new clientConnection(cSock, "opendata"); } catch { Debug.Log("Unable to connect to server."); } }
public const int SIO_UDP_CONNRESET = -1744830452; // Magic number for ignoring a particular socket exeption when building the server locally #endif // Server entrypoint public static void Main() { udp = new UdpClient(PORT); // The udp client listening on PORT #if LOCAL_SERVER udp.Client.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null); // Ignore a particular socket exception locally #endif System.Console.WriteLine("Server started on port " + PORT); while (true) { // Receive a message from some client IPEndPoint client = new IPEndPoint(IPAddress.Any, 0); Byte[] receivedBytes = null; try { receivedBytes = udp.Receive(ref client); } catch (Exception e) { Console.WriteLine("UDP RECEIVE EXCEPTION"); Console.WriteLine(e.ToString()); continue; } string receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); lastMessage = receivedText; // Keep track of clients bool inList = false; clientConnection connection = new clientConnection(); connection.address = client; foreach (var c in connectedClients) { if (sameAddress(c.address, client)) { inList = true; connection = c; break; } } if (!inList) { // Not in list, create a new entry and send all entities connectedClients.Add(connection); for (int i = 0; i < entityStates.Count; ++i) { if ((entityStates[i] != null) && (entityStates[i].Count > 0)) { clientUpdate(i, entityStates[i].ToArray(), connection.address); } } } connection.lastActive = time; // 10 second client timeout double currentTime = time; foreach (var c in connectedClients.ToArray()) { if (currentTime - c.lastActive > 10) { System.Console.WriteLine("cliennt at " + c.address.Address + " timeout."); connectedClients.Remove(c); } } // Logging System.Console.WriteLine("\n\nSERVER INFO"); System.Console.WriteLine("Connected clients: " + connectedClients.Count); foreach (var c in connectedClients) { System.Console.WriteLine(" Client, address: " + c.address.Address + ":" + c.address.Port + ", last active: " + c.lastActive); } System.Console.WriteLine("\n-----Last server message, from: " + client.Address + ":" + client.Port + "-----"); System.Console.WriteLine(lastMessage); System.Console.WriteLine("-----------------------------"); System.Console.WriteLine("Entities:"); for (int i = 0; i < entityStates.Count; ++i) { System.Console.WriteLine(" Entity: " + i); if ((entityStates[i] == null) || (entityStates[i].Count == 0)) { Console.WriteLine(" free to reassign"); } else { foreach (var s in entityStates[i]) { System.Console.WriteLine(" " + s); } } } // Alive messages need not be parsed if (receivedText.StartsWith(aliveMessage)) { continue; } // Parse the message var split = receivedText.Split('\n'); if (split.Length < 1) { throw new System.NotImplementedException("TODO: deal with this error case."); } List <string> updates = new List <string>(split); updates.RemoveAt(0); int id = int.Parse(split[0]); // Assign this entity with a unique entity id if (id < 0) { int assignedID = firstAvailEntitySlot; clientUpdate(id, new string[] { "id:" + assignedID }, client); System.Console.WriteLine("Assigned new entity id: " + assignedID); continue; } // Keep network entites stored on the server up to date for (int i = 0; i < updates.Count; ++i) { var u = updates[i]; if (u.StartsWith("destroy")) { // Remove entity stored on server, queue destroy update to be sent // and ignore all other updates entityStates[id] = null; updates = new List <string>() { "destroy" }; break; } var spl = u.Split(';'); if (spl.Length >= 2) { int index = int.Parse(spl[0]); u = spl[1]; updateState(id, index, u); } updates[i] = u; } // Send the updates to all clients (including the one that // send the update to me, see below) // Send the updates back to the client that sent them // as a test that the updates that the network entity // sent are actually compatible with its current state. // If this is not the case then this will result in a // feedback loop where a network entity continually // updates itself with its own state. var updtArray = updates.ToArray(); foreach (var c in connectedClients) { clientUpdate(id, updtArray, c.address); } } }