Kolja Strohm 1 тиждень тому
батько
коміт
3b7ad81381

+ 2 - 0
FactoryCraft/Main.cpp

@@ -96,6 +96,8 @@ int KSGStart Framework::Start(Framework::Startparam p)
     StartMessageLoop();
     rTh.terminate();
 
+    screen.tick(0.0); // execute all pending actions before releasing the screen
+
     releaseVariables();
     Network::Exit();
     return 0;

+ 3 - 1
FactoryCraft/Menu.cpp

@@ -19,6 +19,8 @@ void Menu::hide()
 {
     zScreen->postAction([this]() {
         for (auto member : elements)
-            zScreen->removeMember(dynamic_cast<Drawable*>(member->getThis()));
+        {
+            zScreen->removeMember(member);
+        }
     });
 }

+ 63 - 32
FactoryCraft/ServerSelection.cpp

@@ -144,6 +144,14 @@ ServerStatus::ServerStatus(Framework::Text name,
 
 ServerStatus::~ServerStatus()
 {
+    requestIdLock.lockRead();
+    while (currentRequestIds.getEntryCount())
+    {
+        requestIdLock.unlockRead();
+        Sleep(100); // wait until running requets are completed
+        requestIdLock.lockRead();
+    }
+    requestIdLock.unlockRead();
     secrets->release();
     removeButton->release();
     join->release();
@@ -153,64 +161,87 @@ void ServerStatus::updatePlayerName(Framework::Text playerName)
 {
     status = "";
     this->playerName = playerName;
-    ServerStatus* tmp = dynamic_cast<ServerStatus*>(getThis());
     requestIdLock.lockWrite();
     int id = ++requestId;
     requestIdLock.unlockWrite();
-    new AsynchronCall([tmp, id]() {
+    bool* ready = new bool;
+    *ready = 0;
+    AsynchronCall* request = new AsynchronCall([this, id, ready]() {
         FactoryClient* client = new FactoryClient();
-        if (!client->connect(tmp->getIp(), tmp->getSSLPort()))
+        if (!client->connect(getIp(), getSSLPort()))
         {
-            tmp->requestIdLock.lockRead();
-            if (tmp->requestId == id)
+            requestIdLock.lockRead();
+            if (requestId == id)
             {
-                tmp->status = "The Server is currently not reachable";
-                tmp->statusId = 404;
-                tmp->ping = -1;
-                tmp->rend = 1;
+                status = "The Server is currently not reachable";
+                statusId = 404;
+                ping = -1;
+                rend = 1;
             }
-            tmp->requestIdLock.unlockRead();
+            requestIdLock.unlockRead();
         }
         else
         {
-            tmp->requestIdLock.lockRead();
-            if (tmp->requestId == id)
+            requestIdLock.lockRead();
+            if (requestId == id)
             {
                 Text secret = "";
-                if (tmp->secrets->has(tmp->playerName))
-                    secret = tmp->secrets->get(tmp->playerName);
-                tmp->requestIdLock.unlockRead();
+                if (secrets->has(this->playerName))
+                    secret = secrets->get(this->playerName);
+                requestIdLock.unlockRead();
                 int ping = client->ping();
-                int statusId = client->status(tmp->playerName, secret);
+                int statusId = client->status(this->playerName, secret);
                 if (statusId == 403)
                 {
-                    int secondStatus = client->status(tmp->playerName, "");
+                    int secondStatus = client->status(this->playerName, "");
                     if (secondStatus == 200) statusId = 203;
                 }
                 if (secret.isEqual("") && statusId == 200)
                 {
                     statusId = 203;
                 }
-                tmp->requestIdLock.lockRead();
-                if (tmp->requestId == id)
+                requestIdLock.lockRead();
+                if (requestId == id)
                 {
-                    tmp->writeLock().lock();
-                    tmp->ping = ping;
-                    tmp->statusId = statusId;
-                    if (tmp->statusId == 200)
-                        tmp->status = "The Server is reachable";
-                    if (statusId == 203) tmp->status = "Create a new player";
-                    if (tmp->statusId == 403)
-                        tmp->status = "The name is already in use";
-                    tmp->rend = 1;
-                    tmp->writeLock().unlock();
+                    writeLock().lock();
+                    this->ping = ping;
+                    this->statusId = statusId;
+                    if (statusId == 200) status = "The Server is reachable";
+                    if (statusId == 203) status = "Create a new player";
+                    if (statusId == 403) status = "The name is already in use";
+                    rend = 1;
+                    writeLock().unlock();
                 }
             }
-            tmp->requestIdLock.unlockRead();
+            requestIdLock.unlockRead();
         }
         client->release();
-        tmp->release();
+        while (!*ready)
+        {
+            Sleep(100);
+        }
+        delete ready;
+        requestIdLock.lockWrite();
+        auto idIterator = currentRequestIds.begin();
+        auto requestIterator = currentRequests.begin();
+        while (idIterator && requestIterator)
+        {
+            if (*idIterator == id)
+            {
+                requestIterator.remove();
+                idIterator.remove();
+                break;
+            }
+            ++idIterator;
+            ++requestIterator;
+        }
+        requestIdLock.unlockWrite();
     });
+    requestIdLock.lockWrite();
+    currentRequestIds.add(id);
+    currentRequests.add(request);
+    *ready = 1;
+    requestIdLock.unlockWrite();
 }
 
 void ServerStatus::doMouseEvent(Framework::MouseEvent& me, bool userRet)
@@ -238,7 +269,7 @@ void ServerStatus::render(Framework::Image& rObj)
         tr.renderText(5, 5, name, rObj, 0xFFFFFFFF);
         tr.renderText(
             5, 25, ip + ":" + sslPort + " (" + port + ")", rObj, 0xFF808080);
-        if (requestId && requestId != 404)
+        if (requestId && statusId != 404)
         {
             int tbr = tr.getTextWidth(Text("ping: ") + ping);
             if (ping >= 0)

+ 3 - 0
FactoryCraft/ServerSelection.h

@@ -1,5 +1,6 @@
 #pragma once
 
+#include <AsynchronCall.h>
 #include <Button.h>
 #include <HashMap.h>
 #include <List.h>
@@ -23,6 +24,8 @@ private:
     Framework::Button* removeButton;
     Framework::Button* join;
     ReadWriteLock requestIdLock;
+    Framework::Array<int> currentRequestIds;
+    Framework::Array<AsynchronCall*> currentRequests;
 
 public:
     ServerStatus(Framework::Text name,