Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize NucleusLib async callbacks using lambdas #1539

Merged
merged 15 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 13 additions & 54 deletions Sources/Plasma/NucleusLib/pnAsyncCore/Private/pnAcIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]
#define PLASMA20_SOURCES_PLASMA_NUCLEUSLIB_PNASYNCCORE_PRIVATE_PNACIO_H

#include <functional>
#include <optional>

#include "pnNetCommon/plNetAddress.h"
#include "pnUUID/pnUUID.h"
Expand All @@ -67,54 +68,15 @@ typedef struct AsyncCancelIdStruct * AsyncCancelId;

constexpr unsigned kAsyncSocketBufferSize = 1460;


/****************************************************************************
*
* Socket event notifications
*
***/

enum EAsyncNotifySocket {
kNotifySocketConnectFailed,
kNotifySocketConnectSuccess,
kNotifySocketDisconnect,
kNotifySocketRead,
kNotifySocketWrite
};

struct AsyncNotifySocket {
void * param;

AsyncNotifySocket() : param() { }
class AsyncNotifySocketCallbacks
{
public:
virtual void AsyncNotifySocketConnectFailed(plNetAddress remoteAddr) = 0;
virtual bool AsyncNotifySocketConnectSuccess(AsyncSocket sock, const plNetAddress& localAddr, const plNetAddress& remoteAddr) = 0;
virtual void AsyncNotifySocketDisconnect(AsyncSocket sock) = 0;
virtual std::optional<size_t> AsyncNotifySocketRead(AsyncSocket sock, uint8_t* buffer, size_t bytes) = 0;
};

struct AsyncNotifySocketConnect : AsyncNotifySocket {
plNetAddress localAddr;
plNetAddress remoteAddr;
};

struct AsyncNotifySocketRead : AsyncNotifySocket {
uint8_t * buffer;
size_t bytes;
size_t bytesProcessed;

AsyncNotifySocketRead() : buffer(), bytes(), bytesProcessed() { }
};


struct AsyncNotifySocketWrite : AsyncNotifySocketRead {
size_t bytesCommitted;

AsyncNotifySocketWrite() : AsyncNotifySocketRead(), bytesCommitted() { }
};

/*! \brief return false to disconnect
\param sock
\param code
\param notify
*/
using FAsyncNotifySocketProc = std::function<bool(AsyncSocket, EAsyncNotifySocket, AsyncNotifySocket*, void**)> ;


/****************************************************************************
*
Expand All @@ -125,8 +87,7 @@ using FAsyncNotifySocketProc = std::function<bool(AsyncSocket, EAsyncNotifySocke
void AsyncSocketConnect (
AsyncCancelId * cancelId,
const plNetAddress& netAddr,
FAsyncNotifySocketProc notifyProc,
void * param = nullptr,
AsyncNotifySocketCallbacks* callbacks,
const void * sendData = nullptr,
unsigned sendBytes = 0
);
Expand Down Expand Up @@ -162,12 +123,10 @@ void AsyncSocketEnableNagling (
*
***/

typedef std::function<void (void* /* param */, const ST::string& /* name */,
const std::vector<plNetAddress>& /* addrs */)> FAsyncLookupProc;
typedef std::function<void(const std::vector<plNetAddress>& /* addrs */)> FAsyncLookupProc;

void AsyncAddressLookupName (
FAsyncLookupProc lookupProc,
const ST::string & name,
unsigned port,
void * param
const ST::string& name,
unsigned port,
FAsyncLookupProc lookupProc
);
11 changes: 6 additions & 5 deletions Sources/Plasma/NucleusLib/pnAsyncCore/Private/pnAcTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,25 @@ struct AsyncTimer;

// Return callbackMs to wait that long until next callback.
// Return kAsyncTimeInfinite to stop callbacks (note: does not destroy Timer structure)
typedef std::function<unsigned (void* /* param */)> FAsyncTimerProc;
typedef std::function<unsigned()> FAsyncTimerProc;

// 1) Timer procs do not get starved by I/O, they are called periodically.
// 2) Timer procs will never be called by multiple threads simultaneously.
AsyncTimer* AsyncTimerCreate (
FAsyncTimerProc timerProc,
unsigned callbackMs,
void * param = nullptr
unsigned callbackMs,
FAsyncTimerProc timerProc
);

typedef std::function<void()> FAsyncTimerDestroyProc;

// Timer procs can be in the process of getting called in
// another thread during the unregister function -- be careful!
// This will wait until the timer has been unregistered and is
// no longer in the process of being called before returning.
void AsyncTimerDelete(AsyncTimer* timer);
void AsyncTimerDeleteCallback (
AsyncTimer * timer,
FAsyncTimerProc destroyProc
FAsyncTimerDestroyProc destroyProc
);

// Set the time value for a timer
Expand Down
10 changes: 3 additions & 7 deletions Sources/Plasma/NucleusLib/pnAsyncCoreExe/pnAceDns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ struct DnsResolveData
{
ST::string fName;
FAsyncLookupProc fLookupProc;
void* fParam;
};

static std::recursive_mutex s_critsect;
Expand All @@ -95,7 +94,7 @@ static void AddressResolved(const asio::error_code& err,
if (err || results.empty()) {
if (err)
LogMsg(kLogFatal, "DNS: Failed to resolve {}: {}", data.fName, err.message());
data.fLookupProc(data.fParam, data.fName, {});
data.fLookupProc({});
return;
}

Expand All @@ -113,14 +112,12 @@ static void AddressResolved(const asio::error_code& err,
addrs.emplace_back(ipv4_addr.to_bytes(), endpoint.port());
}

data.fLookupProc(data.fParam, data.fName, addrs);
data.fLookupProc(addrs);

PerfSubCounter(kAsyncPerfNameLookupAttemptsCurr, 1);
}

void AsyncAddressLookupName(FAsyncLookupProc lookupProc,

const ST::string& name, unsigned port, void* param)
void AsyncAddressLookupName(const ST::string& name, unsigned port, FAsyncLookupProc lookupProc)
{
ASSERT(lookupProc);

Expand All @@ -142,7 +139,6 @@ void AsyncAddressLookupName(FAsyncLookupProc lookupProc,
DnsResolveData data;
data.fName = name;
data.fLookupProc = std::move(lookupProc);
data.fParam = param;

hsLockGuard(s_critsect);

Expand Down
Loading