Skip to content

Commit

Permalink
Minor code cleanup and replaced CreateThread with std::thread
Browse files Browse the repository at this point in the history
  • Loading branch information
MartB committed Jul 20, 2018
1 parent f47be29 commit 3e66742
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 46 deletions.
17 changes: 0 additions & 17 deletions RETC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Server", "server-exe\server
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ShimDLL", "shim-dll\shim.vcxproj", "{758DB129-9123-4E1B-A6C3-47323714123A}"
EndProject
Project("{911E67C6-3D85-4FCE-B560-20A9C3E3FF48}") = "retc-rpc-server-32", "out\x86\Debug\Server\retc-rpc-server-32.exe", "{99C1CD65-766B-4E30-AFA5-F5FFF092F5CF}"
ProjectSection(DebuggerProjectSystem) = preProject
PortSupplier = 00000000-0000-0000-0000-000000000000
Executable = D:\projects\Privat\RETC\out\x86\Debug\Server\retc-rpc-server-32.exe
RemoteMachine = DESKTOP-6T3SGUV
StartingDirectory = D:\projects\Privat\RETC\out\x86\Debug\Server
Environment = Standard
LaunchingEngine = 00000000-0000-0000-0000-000000000000
UseLegacyDebugEngines = No
LaunchSQLEngine = No
AttachLaunchAction = No
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Expand Down Expand Up @@ -53,10 +40,6 @@ Global
{758DB129-9123-4E1B-A6C3-47323714123A}.Release|Win32.Build.0 = Release|Win32
{758DB129-9123-4E1B-A6C3-47323714123A}.Release|x64.ActiveCfg = Release|x64
{758DB129-9123-4E1B-A6C3-47323714123A}.Release|x64.Build.0 = Release|x64
{99C1CD65-766B-4E30-AFA5-F5FFF092F5CF}.Debug|Win32.ActiveCfg = Release
{99C1CD65-766B-4E30-AFA5-F5FFF092F5CF}.Debug|x64.ActiveCfg = Release
{99C1CD65-766B-4E30-AFA5-F5FFF092F5CF}.Release|Win32.ActiveCfg = Release
{99C1CD65-766B-4E30-AFA5-F5FFF092F5CF}.Release|x64.ActiveCfg = Release
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions rpc-midl/rpc_retc.idl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ interface rpc_retc
typedef GUID RZEFFECTID;
typedef GUID RZDEVICEID;
typedef LONG RZRESULT;
typedef unsigned __int32 efsize_t;

// If you change the order in the below enum adjust commonData.h
// If you change the order in the below enum adjust the order of genericEffectType and effectSize in server/src/common/commonData.h
typedef enum RETCDeviceType
{
KEYBOARD,
Expand All @@ -40,7 +41,7 @@ interface rpc_retc

CONTEXT_HANDLE initialize([in] handle_t hBinding, [out] RETCClientConfig* config);

RZRESULT playEffect(RETCDeviceType deviceType, [in] int type, [in, out, unique]RZEFFECTID *pEffectID, unsigned long effectSize, [size_is(effectSize)] char effectData[*], CONTEXT_HANDLE hContext);
RZRESULT playEffect(RETCDeviceType deviceType, [in] int type, [in, out, unique]RZEFFECTID *pEffectID, efsize_t effectSize, [size_is(effectSize)] char effectData[*], CONTEXT_HANDLE hContext);

RZRESULT setEffect([in] RZEFFECTID effID, CONTEXT_HANDLE hContext);
RZRESULT deleteEffect([in] RZEFFECTID effID, CONTEXT_HANDLE hContext);
Expand Down
2 changes: 1 addition & 1 deletion server-exe/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ConfigManager::ConfigManager() {
ReloadConfigFile();
}

#define SUCCESS(cond) cond >= SI_OK
#define SUCCESS(cond) ((cond) >= SI_OK)

void ConfigManager::ReloadConfigFile() {
simpleIni->Reset();
Expand Down
10 changes: 5 additions & 5 deletions server-exe/CorsairSDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ bool CorsairSDK::initialize() {

switch (devType) {
case MOUSE: {
const auto& numberOfKeys = devInfo->physicalLayout - CPL_Zones1 + 1;
const auto& numberOfKeys = static_cast<size_t>(devInfo->physicalLayout - CPL_Zones1 + 1);
ledVector.reserve(numberOfKeys);
for (auto key = 0; key < numberOfKeys; key++) {
for (size_t key = 0; key < numberOfKeys; key++) {
auto ledId = static_cast<CorsairLedId>(CLM_1 + key);
ledVector.emplace_back(ledId);
}
Expand All @@ -96,7 +96,7 @@ bool CorsairSDK::initialize() {
case MOUSEPAD:
case KEYPAD: {
auto const ledPositions = CorsairGetLedPositionsByDeviceIndex(i);
auto const ledCount = ledPositions->numberOfLed;
auto const ledCount = static_cast<size_t>(ledPositions->numberOfLed);

if (ledCount == 0) {
continue;
Expand All @@ -105,7 +105,7 @@ bool CorsairSDK::initialize() {
ledVector.reserve(ledCount);

const auto& ledData = ledPositions->pLedPosition;
for (auto key = 0; key < ledCount; key++) {
for (size_t key = 0; key < ledCount; key++) {
auto ledId = ledData[key].ledId;
ledVector.emplace_back(ledId);
}
Expand Down Expand Up @@ -277,7 +277,7 @@ RZRESULT CorsairSDK::prepareMouseEffect(int type, const char effectData[]) {

for (const auto ledId : ledVector) {
RZLED val = findMouseLed(ledId);
if (val == -1) {
if (val == RZLED_NONE) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions server-exe/EffectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ EffectManager::~EffectManager()
clearEffects();
}

bool EffectManager::storeEffect(const RETCDeviceType& deviceType, int effectType, RZEFFECTID* pEffectID, unsigned long effectSize, const char effectData[]) {
bool EffectManager::storeEffect(const RETCDeviceType& deviceType, int effectType, RZEFFECTID* pEffectID, efsize_t effectSize, const char effectData[]) {
internalEffectData effData;
effData.type = effectType;
effData.deviceType = deviceType;
effData.data = new char[effectSize];
std::move(effectData, effectData + effectSize, effData.data);
effData.data = new char[static_cast<size_t>(effectSize)];
std::move(effectData, effectData + static_cast<size_t>(effectSize), effData.data);

RZEFFECTID newEffectID;
if (!createUniqueEffectID(&newEffectID)) {
Expand Down
2 changes: 1 addition & 1 deletion server-exe/EffectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EffectManager {
static bool createUniqueEffectID(RZEFFECTID* guid) { return CoCreateGuid(guid) == S_OK; }

~EffectManager();
bool storeEffect(const RETCDeviceType& deviceType, int effectType, RZEFFECTID* pEffectID, unsigned long effectSize, const char effectData[]);
bool storeEffect(const RETCDeviceType& deviceType, int effectType, RZEFFECTID* pEffectID, efsize_t effectSize, const char effectData[]);
bool deleteEffect(const RZEFFECTID& pEffectID);
void clearEffects();

Expand Down
2 changes: 1 addition & 1 deletion server-exe/RPCReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CONTEXT_HANDLE initialize(handle_t /*hBinding*/, RETCClientConfig* out) {
return sdkManager->getClientConfig();
}

RZRESULT playEffect(RETCDeviceType deviceType, int effectType, RZEFFECTID* pEffectId, unsigned long effectSize, char* effectData, CONTEXT_HANDLE) {
RZRESULT playEffect(RETCDeviceType deviceType, int effectType, RZEFFECTID* pEffectId, efsize_t effectSize, char* effectData, CONTEXT_HANDLE) {
if (PROCESSING_DELAY > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(PROCESSING_DELAY));
}
Expand Down
6 changes: 3 additions & 3 deletions server-exe/SDKManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ void SDKManager::reloadEmulatedDevices() {
}

LightingSDK* SDKManager::getSDKForDeviceType(RETCDeviceType type) {
if (type >= ALL || type < KEYBOARD) {
if (type >= ALL) {
return nullptr;
}

return m_selectedSDKs[type];
}

void SDKManager::checkAvailability() {
bool hasAnySDK = false;;
bool hasAnySDK = false;
for (auto&& sdk : m_availableSDKs) {
if (!sdk->init(m_sdkLoader.get())) {
LOG_T(L"an sdk failed to initialize {0}", sdk->getSDKName());
Expand Down Expand Up @@ -135,7 +135,7 @@ RZRESULT SDKManager::playbackEffect(const RETCDeviceType& devType, int effectTyp
return (sdk) ? sdk->playEffect(devType, effectType, effectData) : RZRESULT_NOT_FOUND;
}

RZRESULT SDKManager::playEffect(const RETCDeviceType& devType, int effectType, RZEFFECTID* pEffectId, unsigned long size, const char effectData[]) {
RZRESULT SDKManager::playEffect(const RETCDeviceType& devType, int effectType, RZEFFECTID* pEffectId, efsize_t size, const char effectData[]) {
// We need to store the effect
if (pEffectId != nullptr) {
return m_effectManager->storeEffect(devType, effectType, pEffectId, size, effectData) ? RZRESULT_SUCCESS : RZRESULT_FAILED;
Expand Down
2 changes: 1 addition & 1 deletion server-exe/SDKManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SDKManager {
public:
RZRESULT deleteEffect(const RZEFFECTID& effID) const;
RZRESULT setEffect(const RZEFFECTID& effID);
RZRESULT playEffect(const RETCDeviceType& devType, int effectType, RZEFFECTID* pEffectId, unsigned long size, const char effectData[]);
RZRESULT playEffect(const RETCDeviceType& devType, int effectType, RZEFFECTID* pEffectId, efsize_t size, const char effectData[]);

private:
void checkAvailability();
Expand Down
2 changes: 1 addition & 1 deletion server-exe/commonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace LookupArrays {
{Keypad::CHROMA_NONE, Keypad::CHROMA_WAVE, Keypad::CHROMA_SPECTRUMCYCLING, Keypad::CHROMA_BREATHING, -1, Keypad::CHROMA_REACTIVE, Keypad::CHROMA_STATIC, Keypad::CHROMA_CUSTOM} // Keypad
};

static const unsigned long effectSize[ESIZE][CHROMA_RESERVED] = {
static const efsize_t effectSize[ESIZE][CHROMA_RESERVED] = {
{sizeof(Keyboard::BREATHING_EFFECT_TYPE), sizeof(Keyboard::CUSTOM_EFFECT_TYPE), sizeof(Keyboard::REACTIVE_EFFECT_TYPE), sizeof(Keyboard::STATIC_EFFECT_TYPE), sizeof(SPECTRUMCYCLING_EFFECT_TYPE), sizeof(WAVE_EFFECT_TYPE), 0, sizeof(Keyboard::CUSTOM_KEY_EFFECT_TYPE)},
{sizeof(Mouse::BLINKING_EFFECT_TYPE), sizeof(Keyboard::BREATHING_EFFECT_TYPE), sizeof(Keyboard::CUSTOM_EFFECT_TYPE), sizeof(Keyboard::REACTIVE_EFFECT_TYPE), sizeof(SPECTRUMCYCLING_EFFECT_TYPE), sizeof(STATIC_EFFECT_TYPE), sizeof(WAVE_EFFECT_TYPE), sizeof(Mouse::CUSTOM_EFFECT_TYPE2)},
{sizeof(Headset::STATIC_EFFECT_TYPE), sizeof(Headset::BREATHING_EFFECT_TYPE), sizeof(SPECTRUMCYCLING_EFFECT_TYPE), sizeof(Headset::CUSTOM_EFFECT_TYPE), 0, 0, 0, 0},
Expand Down
2 changes: 1 addition & 1 deletion server-exe/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ BOOL WINAPI consoleHandler(DWORD signal) {
#define DEF_FLUSH_LEVEL spdlog::level::err
#endif

DWORD WINAPI SVCWorkerThread(LPVOID) {
int SVCWorkerThread() {
CONFIG.reset(new ConfigManager());

try {
Expand Down
19 changes: 14 additions & 5 deletions server-exe/service.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

#include <Windows.h>
#include <tchar.h>
#include <thread>

#define SVC_NAME _T("RETC")
#define SVC_FNAME _T("RETC Service")
Expand All @@ -12,7 +16,7 @@ void InstallService();
void RemoveService();
VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
VOID WINAPI ServiceController(DWORD);
DWORD WINAPI SVCWorkerThread(LPVOID lpParam);
int SVCWorkerThread();


SERVICE_STATUS_HANDLE g_StatusHandle = nullptr;
Expand All @@ -38,9 +42,11 @@ int _tmain(const int argc, TCHAR* argv[]) {

if (StartServiceCtrlDispatcher(ServiceTableEntry) == FALSE) {
OutputDebugString(_T("Service start failed, falling back to normal launch."));
SVCWorkerThread(nullptr);
SVCWorkerThread();
return GetLastError();
}

return EXIT_SUCCESS;
}

VOID WINAPI ServiceMain(DWORD, LPTSTR*) {
Expand All @@ -61,8 +67,10 @@ VOID WINAPI ServiceMain(DWORD, LPTSTR*) {

SetServiceStatus(g_StatusHandle, &g_ServiceStatus);

// Start mainthread.
const auto hThread = CreateThread(nullptr, 0, SVCWorkerThread, nullptr, 0, nullptr);
// Start service thread
auto serviceThread = std::thread([=] {
SVCWorkerThread();
});

g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
Expand All @@ -71,7 +79,8 @@ VOID WINAPI ServiceMain(DWORD, LPTSTR*) {

SetServiceStatus(g_StatusHandle, &g_ServiceStatus);

WaitForSingleObject(hThread, INFINITE);
// Wait until the server terminates
serviceThread.join();

g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
Expand Down
10 changes: 5 additions & 5 deletions shim-dll/shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD /*callReason*/, LPVOID /*lpRese
}

BOOL hasSupportFor(RETCDeviceType type) {
if (!isInitialized() || type >= ESIZE || type < KEYBOARD) {
if (!isInitialized() || type >= ESIZE) {
return false;
}

Expand Down Expand Up @@ -133,7 +133,7 @@ RZRESULT sendEffect(RETCDeviceType deviceType, int effectID, PRZPARAM effectData
RpcEndExcept
}

RZRESULT CreateEffect(RZDEVICEID DeviceId, EFFECT_TYPE effectID, PRZPARAM pParam, RZEFFECTID* pEffectId) {
RZRESULT CreateEffect(RZDEVICEID DeviceId, EFFECT_TYPE effectID, PRZPARAM pParam, RZEFFECTID* pEffectId) { //-V813
auto deviceType = ESIZE;

if (DeviceId == GUID_NULL) {
Expand Down Expand Up @@ -177,23 +177,23 @@ RZRESULT CreateKeypadEffect(Keypad::EFFECT_TYPE Effect, PRZPARAM pParam, RZEFFEC
}


RZRESULT SetEffect(RZEFFECTID EffectId) {
RZRESULT SetEffect(RZEFFECTID EffectId) { //-V813
RpcTryExcept
return setEffect(EffectId, rpcCTXHandle);
RpcExcept(1)
return RZRESULT_NOT_VALID_STATE;
RpcEndExcept
}

RZRESULT DeleteEffect(RZEFFECTID EffectId) {
RZRESULT DeleteEffect(RZEFFECTID EffectId) { //-V813
RpcTryExcept
return deleteEffect(EffectId, rpcCTXHandle);
RpcExcept(1)
return RZRESULT_NOT_VALID_STATE;
RpcEndExcept
}

RZRESULT QueryDevice(RZDEVICEID DeviceID, DEVICE_INFO_TYPE& DeviceInfo) {
RZRESULT QueryDevice(RZDEVICEID DeviceID, DEVICE_INFO_TYPE& DeviceInfo) { //-V813
for (int devID = KEYBOARD; devID < ALL; devID++) {
if (DeviceID == CONFIG.emulatedDeviceIDS[devID]) {
DeviceInfo.Connected = CONFIG.supportedDeviceTypes[devID];
Expand Down

0 comments on commit 3e66742

Please sign in to comment.