118 lines
2.6 KiB
C++
118 lines
2.6 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include <transportplugin.h>
|
|
#include <atomic>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
#include <cstdio>
|
|
|
|
extern "C"
|
|
{
|
|
typedef void (*LogCallback)(void* UsrPtr, int32_t Level, const char* Buffer);
|
|
|
|
// native -> managed API
|
|
void StartAndroidPortForwarder(LogCallback Callback, void* UsrPtr, const char* ADBExecutable, uint32_t Port);
|
|
void StopAndroidPortForwarder();
|
|
}
|
|
|
|
class ZenServerAdapter : public zen::TransportPlugin
|
|
{
|
|
public:
|
|
ZenServerAdapter(zen::TransportLogger* InLogger)
|
|
: Logger(InLogger)
|
|
{
|
|
}
|
|
virtual ~ZenServerAdapter() = default;
|
|
|
|
uint32_t AddRef() const override
|
|
{
|
|
return const_cast<ZenServerAdapter*>(this)->ReferenceCount.fetch_add(1) + 1;
|
|
}
|
|
|
|
uint32_t Release() const override
|
|
{
|
|
const uint32_t RefCount = const_cast<ZenServerAdapter*>(this)->ReferenceCount.fetch_sub(1);
|
|
if (RefCount <= 1)
|
|
{
|
|
delete this;
|
|
}
|
|
return RefCount - 1;
|
|
}
|
|
|
|
void Configure(const char* OptionTag, const char* OptionValue) override
|
|
{
|
|
if (!strcmp(OptionTag, "port"))
|
|
{
|
|
char* EndPtr = nullptr;
|
|
const long PortValue = strtoul(OptionValue, &EndPtr, 10);
|
|
if (OptionValue != EndPtr)
|
|
{
|
|
Port = (uint32_t)PortValue;
|
|
}
|
|
else
|
|
{
|
|
Logger->LogMessage(zen::TransportLogger::LogLevel::Err, "Can't parse port value");
|
|
}
|
|
}
|
|
else if (!strcmp(OptionTag, "adb_executable"))
|
|
{
|
|
snprintf(ADBExecutable, sizeof(ADBExecutable), "%s", OptionValue);
|
|
}
|
|
}
|
|
|
|
void Initialize(zen::TransportServer* ServerInterface) override
|
|
{
|
|
StartAndroidPortForwarder(
|
|
[](void* ThisPtr, int32_t Level, const char* Buffer)
|
|
{
|
|
static_cast<ZenServerAdapter*>(ThisPtr)->Logger->LogMessage(Level == 1 ? zen::TransportLogger::LogLevel::Err : zen::TransportLogger::LogLevel::Info, Buffer);
|
|
},
|
|
this, ADBExecutable, Port);
|
|
}
|
|
|
|
void Shutdown() override
|
|
{
|
|
StopAndroidPortForwarder();
|
|
}
|
|
|
|
const char* GetDebugName() override
|
|
{
|
|
return "AndroidPortForwarder";
|
|
}
|
|
|
|
bool IsAvailable() override
|
|
{
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
std::atomic<uint32_t> ReferenceCount = {0};
|
|
uint32_t Port = 8558;
|
|
char ADBExecutable[1024] = {};
|
|
zen::TransportLogger* Logger = nullptr;
|
|
};
|
|
|
|
#if defined(_MSC_VER)
|
|
# define DLL_TRANSPORT_API __declspec(dllexport)
|
|
#else
|
|
# define DLL_TRANSPORT_API
|
|
#endif
|
|
|
|
extern "C" DLL_TRANSPORT_API void GetTransportPluginVersion(uint32_t* OutApiVersion, uint32_t* OutPluginVersion)
|
|
{
|
|
if (OutApiVersion != nullptr)
|
|
{
|
|
*OutApiVersion = zen::kTransportApiVersion;
|
|
}
|
|
|
|
if (OutPluginVersion != nullptr)
|
|
{
|
|
*OutPluginVersion = 2;
|
|
}
|
|
}
|
|
|
|
extern "C" DLL_TRANSPORT_API zen::TransportPlugin* CreateTransportPlugin(zen::TransportLogger* Logger)
|
|
{
|
|
return new ZenServerAdapter(Logger);
|
|
}
|