From c4f08e5145cf5707c40bbcebbe80357472ab3d02 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 11 Dec 2019 16:18:47 -0500 Subject: [PATCH] finish remaining changes --- llarp/config/key_manager.cpp | 5 +++++ llarp/ev/ev.cpp | 2 +- llarp/ev/ev_libuv.cpp | 22 +++++++++++++++----- llarp/ev/ev_win32.cpp | 39 ++++++++++++++++++++---------------- llarp/ev/ev_win32.hpp | 2 +- 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/llarp/config/key_manager.cpp b/llarp/config/key_manager.cpp index f0310f35e..71a97f304 100644 --- a/llarp/config/key_manager.cpp +++ b/llarp/config/key_manager.cpp @@ -211,6 +211,10 @@ namespace llarp bool KeyManager::loadIdentityFromLokid() { +#if defined(_WIN32) || defined(_WIN64) + LogError("service node mode not supported on windows"); + return false; +#else CURL* curl = curl_easy_init(); if(curl) { @@ -307,6 +311,7 @@ namespace llarp LogError("failed to init curl"); return false; } +#endif } } // namespace llarp diff --git a/llarp/ev/ev.cpp b/llarp/ev/ev.cpp index 2f2426d17..17872533e 100644 --- a/llarp/ev/ev.cpp +++ b/llarp/ev/ev.cpp @@ -103,7 +103,7 @@ llarp_ev_add_tun(struct llarp_ev_loop *loop, struct llarp_tun_io *tun) if(dev) { dev->setup(); - return dev->add_ev(); // start up tun and add to event queue + return dev->add_ev(loop); // start up tun and add to event queue } llarp::LogWarn("Loop could not create tun"); return false; diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index 92e02d1ab..f172e0063 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -542,8 +542,11 @@ namespace libuv llarp::LogError("failed to start ticker"); return false; } +#if defined(_WIN32) || defined(_WIN64) +#else if(uv_fileno((const uv_handle_t*)&m_Handle, &m_UDP->fd)) return false; +#endif m_UDP->sendto = &SendTo; m_UDP->impl = this; return true; @@ -640,7 +643,8 @@ namespace libuv uv_poll_t m_Handle; uv_check_t m_Ticker; }; - +#if defined(_WIN32) || defined(_WIN64) +#else struct tun_glue : public glue { uv_poll_t m_Handle; @@ -654,7 +658,7 @@ namespace libuv { m_Handle.data = this; m_Ticker.data = this; - readpkt = false; + readpkt = false; } ~tun_glue() override @@ -784,11 +788,11 @@ namespace libuv return false; } m_Tun->writepkt = &WritePkt; - m_Tun->impl = this; + m_Tun->impl = this; return true; } }; - +#endif bool Loop::init() { @@ -801,7 +805,10 @@ namespace libuv #endif m_Impl.data = this; +#if defined(_WIN32) || defined(_WIN64) +#else uv_loop_configure(&m_Impl, UV_LOOP_BLOCK_SIGNAL, SIGPIPE); +#endif m_TickTimer.data = this; m_LogicCaller.data = this; uv_async_init(&m_Impl, &m_LogicCaller, [](uv_async_t* h) { @@ -938,14 +945,19 @@ namespace libuv bool Loop::tun_listen(llarp_tun_io* tun) { +#if defined(_WIN32) || defined(_WIN64) + (void)tun; + return false; +#else auto* glue = new tun_glue(tun); - tun->impl = glue; + tun->impl = glue; if(glue->Init(&m_Impl)) { return true; } delete glue; return false; +#endif } bool diff --git a/llarp/ev/ev_win32.cpp b/llarp/ev/ev_win32.cpp index 9ec07c6b4..1e7e15dfc 100644 --- a/llarp/ev/ev_win32.cpp +++ b/llarp/ev/ev_win32.cpp @@ -16,13 +16,12 @@ static CRITICAL_SECTION HandlerMtx; std::list< win32_tun_io* > tun_listeners; void -begin_tun_loop(int nThreads) +begin_tun_loop(int nThreads, llarp_ev_loop* loop) { kThreadPool = new HANDLE[nThreads]; for(int i = 0; i < nThreads; ++i) { - kThreadPool[i] = - CreateThread(nullptr, 0, &tun_ev_loop, nullptr, 0, nullptr); + kThreadPool[i] = CreateThread(nullptr, 0, &tun_ev_loop, loop, 0, nullptr); } llarp::LogInfo("created ", nThreads, " threads for TUN event queue"); poolSize = nThreads; @@ -74,7 +73,7 @@ win32_tun_io::setup() // first TUN device gets to set up the event port bool -win32_tun_io::add_ev() +win32_tun_io::add_ev(llarp_ev_loop* loop) { if(tun_event_queue == INVALID_HANDLE_VALUE) { @@ -85,7 +84,7 @@ win32_tun_io::add_ev() // threads tun_event_queue = CreateIoCompletionPort(tunif->tun_fd, nullptr, (ULONG_PTR)this, numCPU * 2); - begin_tun_loop(numCPU * 2); + begin_tun_loop(numCPU * 2, loop); } else CreateIoCompletionPort(tunif->tun_fd, tun_event_queue, (ULONG_PTR)this, 0); @@ -106,7 +105,7 @@ win32_tun_io::do_write(void* data, size_t sz) pkt->sz = sz; pkt->write = true; memset(&pkt->pkt, '\0', sizeof(pkt->pkt)); - WriteFile(tunif->tun_fd, data, sz, nullptr, &pkt->pkt); + WriteFile(tunif->tun_fd, data, sz, nullptr, p & pkt->pkt); } // while this one is called from the event loop @@ -131,9 +130,9 @@ win32_tun_io::read(byte_t* buf, size_t sz) // and now the event loop itself extern "C" DWORD FAR PASCAL -tun_ev_loop(void* unused) +tun_ev_loop(void* u) { - UNREFERENCED_PARAMETER(unused); + llarp_ev_loop* logic = static_cast< llarp_ev_loop* >(u); DWORD size = 0; OVERLAPPED* ovl = nullptr; @@ -154,9 +153,11 @@ tun_ev_loop(void* unused) for(const auto& tun : tun_listeners) { EnterCriticalSection(&HandlerMtx); - if(tun->t->tick) - tun->t->tick(tun->t); - tun->flush_write(); + logic->call_soon([tun = tun.get()]() { + if(tun->t->tick) + tun->t->tick(tun->t); + tun->flush_write(); + }); LeaveCriticalSection(&HandlerMtx); } continue; // let's go at it once more @@ -178,8 +179,11 @@ tun_ev_loop(void* unused) continue; } // EnterCriticalSection(&HandlerMtx); - if(ev->t->recvpkt) - ev->t->recvpkt(ev->t, llarp_buffer_t(pkt->buf, size)); + logic->call_soon([pkt, size, ev]() { + if(ev->t->recvpkt) + ev->t->recvpkt(ev->t, llarp_buffer_t(pkt->buf, size)); + delete pkt; + }); ev->read(ev->readbuf, sizeof(ev->readbuf)); // LeaveCriticalSection(&HandlerMtx); } @@ -191,11 +195,12 @@ tun_ev_loop(void* unused) // LeaveCriticalSection(&HandlerMtx); } EnterCriticalSection(&HandlerMtx); - if(ev->t->tick) - ev->t->tick(ev->t); - ev->flush_write(); + logic->call_soon([ev]() { + if(ev->t->tick) + ev->t->tick(ev->t); + ev->flush_write(); + }); LeaveCriticalSection(&HandlerMtx); - delete pkt; // don't leak } llarp::LogDebug("exit TUN event loop thread from system managed thread pool"); return 0; diff --git a/llarp/ev/ev_win32.hpp b/llarp/ev/ev_win32.hpp index e84dffec4..dbc7b465d 100644 --- a/llarp/ev/ev_win32.hpp +++ b/llarp/ev/ev_win32.hpp @@ -78,7 +78,7 @@ struct win32_tun_io // first TUN device gets to set up the event port bool - add_ev(); + add_ev(llarp_ev_loop* l); // places data in event queue for kernel to process void