/*
- Copyright 2004-2005 Chris Tallon
+ Copyright 2004-2020 Chris Tallon
This file is part of VOMP.
along with VOMP. If not, see <https://www.gnu.org/licenses/>.
*/
-#include "vconnect.h"
-
#include "video.h"
#include "colour.h"
#include "command.h"
#include "vserverselect.h"
#include "messagequeue.h"
+#include "vconnect.h"
+
VConnect::VConnect(char* tServer)
+: server(tServer)
{
- server = tServer;
-
boxstack = BoxStack::getInstance();
vdr = VDR::getInstance();
logger = Log::getInstance();
VConnect::~VConnect()
{
- irun = 0;
+ threadReqQuit = true;
vdr->cancelFindingServer();
- threadStop();
+ stop();
}
void VConnect::draw()
void VConnect::run()
{
- threadStart();
+ threadMutex.lock();
+ threadReqQuit = false;
+ connectThread = std::thread([this]
+ {
+ threadMutex.lock();
+ threadMutex.unlock();
+ threadMethod();
+ });
+ threadMutex.unlock();
+}
+
+void VConnect::stop()
+{
+ threadMutex.lock();
+ threadReqQuit = true;
+ threadCond.notify_one();
+ threadMutex.unlock();
+ connectThread.join();
}
void VConnect::threadMethod()
{
ULONG delay = 0;
int success;
- irun = 1;
+
+ std::unique_lock<std::mutex> ul(threadMutex, std::defer_lock);
do
{
boxstack->update(this);
vdr->findServers(servers);
- if (!irun)
+ if (threadReqQuit)
{
for(UINT k = 0; k < servers.size(); k++)
{
boxstack->add(vs);
boxstack->update(vs);
- threadLock();
- threadWaitForSignal();
- threadUnlock();
+ ul.lock();
+ if (threadReqQuit) { ul.unlock(); return; }
+ threadCond.wait(ul);
+ ul.unlock();
}
- if (!irun)
+ if (threadReqQuit)
{
for(UINT k = 0; k < servers.size(); k++)
{
if (m->message == Message::SERVER_SELECTED)
{
selectedServer = m->parameter;
- threadSignal();
+ threadCond.notify_one();
}
}
#ifndef VCONNECT_H
#define VCONNECT_H
-#include <stdio.h>
#include <string.h>
#include <vector>
+#include <mutex>
+#include <thread>
+#include <condition_variable>
#include "vinfo.h"
#include "vdr.h"
-#ifdef WIN32
-#include "threadwin.h"
-#else
-#include "threadp.h"
-#endif
-
class Log;
class BoxStack;
class Message;
-class VConnect : public VInfo, public Thread_TYPE
+class VConnect : public VInfo
{
public:
VConnect(char* server);
void run();
private:
- void threadMethod();
+
void clearServerIPs();
BoxStack* boxstack;
- UCHAR irun{};
VDR* vdr;
Log* logger;
std::vector<VDRServer> servers;
int selectedServer;
char* server;
+
+ std::thread connectThread;
+ std::mutex threadMutex;
+ std::condition_variable threadCond;
+ bool threadReqQuit;
+ void threadMethod();
+ void stop();
};
#endif