From a03eb9599ff1aea7a308570cd7f96719165b9700 Mon Sep 17 00:00:00 2001 From: jmsgrogan Date: Mon, 29 Mar 2021 21:31:24 +0100 Subject: [PATCH] Whitespace and pointer cleanup. --- apps/audio-win.cpp | 122 ++++++++--------- apps/xml-practice.cpp | 48 +++---- src/audio/AudioDevice.cpp | 44 +++--- src/audio/AudioDevice.h | 38 +++--- src/audio/AudioManager.cpp | 5 +- src/audio/AudioManager.h | 6 +- src/audio/CMakeLists.txt | 7 +- src/audio/audio_interfaces/AlsaInterface.cpp | 4 +- src/audio/audio_interfaces/AlsaInterface.h | 32 +++-- src/audio/audio_interfaces/AudioInterface.cpp | 22 --- src/audio/audio_interfaces/IAudioInterface.h | 14 +- src/console/MainApplication.cpp | 2 +- src/core/ByteUtils.h | 128 +++++++++--------- src/core/StringUtils.cpp | 8 +- src/core/StringUtils.h | 22 +-- src/core/http/HttpResponse.cpp | 28 ++-- src/core/http/HttpResponse.h | 20 +-- src/database/Database.cpp | 12 +- src/database/Database.h | 14 +- src/database/DatabaseManager.cpp | 24 ++-- src/database/DatabaseManager.h | 16 +-- .../database_interfaces/SqliteInterface.cpp | 48 +++---- .../database_interfaces/SqliteInterface.h | 16 +-- src/network/CMakeLists.txt | 3 +- src/network/NetworkManager.cpp | 14 +- src/network/NetworkManager.h | 12 +- src/network/sockets/Socket.cpp | 31 +++-- src/network/sockets/Socket.h | 30 ++-- src/network/sockets/SocketInterface.cpp | 27 ---- src/network/sockets/SocketInterface.h | 19 +-- src/network/sockets/UnixSocketInterface.cpp | 64 ++++----- src/network/sockets/UnixSocketInterface.h | 29 ++-- 32 files changed, 441 insertions(+), 468 deletions(-) delete mode 100644 src/audio/audio_interfaces/AudioInterface.cpp delete mode 100644 src/network/sockets/SocketInterface.cpp diff --git a/apps/audio-win.cpp b/apps/audio-win.cpp index 2f4373a..633d3f3 100644 --- a/apps/audio-win.cpp +++ b/apps/audio-win.cpp @@ -10,73 +10,73 @@ const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); int main() { - CoInitialize(nullptr); - IMMDeviceEnumerator* pEnumerator = nullptr; - IMMDeviceCollection* pDeviceCollection = nullptr; - HRESULT hr = S_OK; + CoInitialize(nullptr); + IMMDeviceEnumerator* pEnumerator = nullptr; + IMMDeviceCollection* pDeviceCollection = nullptr; + HRESULT hr = S_OK; - hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, - IID_IMMDeviceEnumerator, (void**)& pEnumerator); - if (!pEnumerator) - { - std::cout << "Failed to populate enumerator" << std::endl; - return 0; - } + hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, + IID_IMMDeviceEnumerator, (void**)& pEnumerator); + if (!pEnumerator) + { + std::cout << "Failed to populate enumerator" << std::endl; + return 0; + } - hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pDeviceCollection); - UINT count; - hr = pDeviceCollection->GetCount(&count); - if (count == 0) - { - std::cout << "No devices found\n"; - } - // Each loop prints the name of an endpoint device. - IMMDevice* pEndpoint = nullptr; - IPropertyStore* pProps = nullptr; - LPWSTR pwszID = nullptr; - for (ULONG i = 0; i < count; i++) - { - // Get pointer to endpoint number i. - hr = pDeviceCollection->Item(i, &pEndpoint); + hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pDeviceCollection); + UINT count; + hr = pDeviceCollection->GetCount(&count); + if (count == 0) + { + std::cout << "No devices found\n"; + } + // Each loop prints the name of an endpoint device. + IMMDevice* pEndpoint = nullptr; + IPropertyStore* pProps = nullptr; + LPWSTR pwszID = nullptr; + for (ULONG i = 0; i < count; i++) + { + // Get pointer to endpoint number i. + hr = pDeviceCollection->Item(i, &pEndpoint); - // Get the endpoint ID string. - hr = pEndpoint->GetId(&pwszID); - hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps); + // Get the endpoint ID string. + hr = pEndpoint->GetId(&pwszID); + hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps); - PROPVARIANT varName; - // Initialize container for property value. - PropVariantInit(&varName); + PROPVARIANT varName; + // Initialize container for property value. + PropVariantInit(&varName); - // Get the endpoint's friendly-name property. - hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName); + // Get the endpoint's friendly-name property. + hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName); - // Print endpoint friendly name and endpoint ID. - std::cout << "Endpoint: " << i << " " << varName.pwszVal << " " << pwszID << std::endl; - CoTaskMemFree(pwszID); - pwszID = nullptr; + // Print endpoint friendly name and endpoint ID. + std::cout << "Endpoint: " << i << " " << varName.pwszVal << " " << pwszID << std::endl; + CoTaskMemFree(pwszID); + pwszID = nullptr; - PropVariantClear(&varName); - if (pProps) - { - pProps->Release(); - pProps = nullptr; - } - if (pEndpoint) - { - pEndpoint->Release(); - pEndpoint = nullptr; - } - } - if (pEnumerator) - { - pEnumerator->Release(); - pEnumerator = nullptr; - } - if (pDeviceCollection) - { - pDeviceCollection->Release(); - pDeviceCollection = nullptr; - } - return 0; + PropVariantClear(&varName); + if (pProps) + { + pProps->Release(); + pProps = nullptr; + } + if (pEndpoint) + { + pEndpoint->Release(); + pEndpoint = nullptr; + } + } + if (pEnumerator) + { + pEnumerator->Release(); + pEnumerator = nullptr; + } + if (pDeviceCollection) + { + pDeviceCollection->Release(); + pDeviceCollection = nullptr; + } + return 0; } diff --git a/apps/xml-practice.cpp b/apps/xml-practice.cpp index 9d2f3ae..913510d 100644 --- a/apps/xml-practice.cpp +++ b/apps/xml-practice.cpp @@ -6,33 +6,33 @@ int main(int argc, char *argv[]) { - CommandLineArgs command_line_args; - command_line_args.Process(argc, argv); + CommandLineArgs command_line_args; + command_line_args.Process(argc, argv); - if(command_line_args.GetNumberOfArgs() < 2) - { - std::cerr << "Expected a filepath argument" << std::endl; - return -1; - } + if(command_line_args.GetNumberOfArgs() < 2) + { + std::cerr << "Expected a filepath argument" << std::endl; + return -1; + } - XmlParser parser; - const auto filepath = command_line_args.GetArg(1); + XmlParser parser; + const auto filepath = command_line_args.GetArg(1); - if(!std::filesystem::exists(filepath)) - { - std::cerr << "Couldn't find file: " << filepath << std::endl; - return -1; - } + if(!std::filesystem::exists(filepath)) + { + std::cerr << "Couldn't find file: " << filepath << std::endl; + return -1; + } - std::ifstream xml_file; - xml_file.open(filepath, std::ifstream::in); - while(xml_file.good()) - { - std::string line; - std::getline(xml_file, line); - parser.ProcessLine(line); - } - xml_file.close(); + std::ifstream xml_file; + xml_file.open(filepath, std::ifstream::in); + while(xml_file.good()) + { + std::string line; + std::getline(xml_file, line); + parser.ProcessLine(line); + } + xml_file.close(); - return 0; + return 0; } diff --git a/src/audio/AudioDevice.cpp b/src/audio/AudioDevice.cpp index 072f5fc..7394194 100644 --- a/src/audio/AudioDevice.cpp +++ b/src/audio/AudioDevice.cpp @@ -1,11 +1,11 @@ #include "AudioDevice.h" AudioDevice::AudioDevice() - : mName("plughw:1,0"), - mSampleRate(44100), - mNumChannels(2), - mPeriod(2), - mBufferSize() + : mName("plughw:1,0"), + mSampleRate(44100), + mNumChannels(2), + mPeriod(2), + mBufferSize() { } @@ -15,57 +15,57 @@ AudioDevice::~AudioDevice() } -std::shared_ptr AudioDevice::Create() +std::unique_ptr AudioDevice::Create() { - return std::make_shared(); + return std::make_unique(); } void AudioDevice::SetNumChannels(unsigned numChannels) { - mNumChannels = numChannels; + mNumChannels = numChannels; } void AudioDevice::SetPeriod(unsigned period) { - mPeriod = period; + mPeriod = period; } void AudioDevice::SetBufferSize(std::size_t bufferSize) { - mBufferSize = bufferSize; + mBufferSize = bufferSize; } -unsigned AudioDevice::GetNumChannels() +unsigned AudioDevice::GetNumChannels() const { - return mNumChannels; + return mNumChannels; } -unsigned AudioDevice::GetPeriod() +unsigned AudioDevice::GetPeriod() const { - return mPeriod; + return mPeriod; } -std::size_t AudioDevice::GetBufferSize() +std::size_t AudioDevice::GetBufferSize() const { - return mBufferSize; + return mBufferSize; } void AudioDevice::SetSampleRate(unsigned rate) { - mSampleRate = rate; + mSampleRate = rate; } -unsigned AudioDevice::GetSampleRate() +unsigned AudioDevice::GetSampleRate() const { - return mSampleRate; + return mSampleRate; } void AudioDevice::SetName(const std::string& name) { - mName = name; + mName = name; } -std::string AudioDevice::GetName() +std::string AudioDevice::GetName() const { - return mName; + return mName; } diff --git a/src/audio/AudioDevice.h b/src/audio/AudioDevice.h index fb0b675..e213a0d 100644 --- a/src/audio/AudioDevice.h +++ b/src/audio/AudioDevice.h @@ -8,39 +8,39 @@ class AudioDevice private: - std::string mName; - unsigned mSampleRate; - unsigned mNumChannels; - unsigned mPeriod; - std::size_t mBufferSize; + std::string mName; + unsigned mSampleRate; + unsigned mNumChannels; + unsigned mPeriod; + std::size_t mBufferSize; public: - AudioDevice(); + AudioDevice(); - ~AudioDevice(); + ~AudioDevice(); - void SetSampleRate(unsigned rate); + void SetSampleRate(unsigned rate); - unsigned GetSampleRate(); + unsigned GetSampleRate() const; - void SetName(const std::string& name); + void SetName(const std::string& name); - void SetNumChannels(unsigned numChannels); + void SetNumChannels(unsigned numChannels); - void SetPeriod(unsigned period); + void SetPeriod(unsigned period); - void SetBufferSize(std::size_t bufferSize); + void SetBufferSize(std::size_t bufferSize); - unsigned GetNumChannels(); + unsigned GetNumChannels() const; - unsigned GetPeriod(); + unsigned GetPeriod() const; - std::size_t GetBufferSize(); + std::size_t GetBufferSize() const; - std::string GetName(); + std::string GetName() const; - static std::shared_ptr Create(); + static std::unique_ptr Create(); }; -using AudioDevicePtr = std::shared_ptr; +using AudioDevicePtr = std::unique_ptr; diff --git a/src/audio/AudioManager.cpp b/src/audio/AudioManager.cpp index faceb38..81a40e4 100644 --- a/src/audio/AudioManager.cpp +++ b/src/audio/AudioManager.cpp @@ -1,10 +1,11 @@ #include "AudioManager.h" +#include "AlsaInterface.h" AudioManager::AudioManager() : mAudioDevices(), mAudioInterface() { - mAudioInterface = AudioInterface::Create(); + mAudioInterface = AlsaInterface::Create(); } AudioManager::~AudioManager() @@ -22,7 +23,7 @@ void AudioManager::AddAudioDevice(AudioDevicePtr device) mAudioDevices.push_back(device); } -AudioInterface* AudioManager::GetAudioInterface() +IAudioInterface* AudioManager::GetAudioInterface() { return mAudioInterface.get(); } diff --git a/src/audio/AudioManager.h b/src/audio/AudioManager.h index 850d68f..de13da2 100644 --- a/src/audio/AudioManager.h +++ b/src/audio/AudioManager.h @@ -3,7 +3,7 @@ #include #include -#include "AudioInterface.h" +#include "audio_interfaces/IAudioInterface.h" #include "AudioDevice.h" class AudioManager @@ -12,7 +12,7 @@ class AudioManager private: std::vector mAudioDevices; - AudioInterfaceUPtr mAudioInterface; + IAudioInterfaceUPtr mAudioInterface; public: @@ -26,7 +26,7 @@ public: std::vector GetAudioDevices(); - AudioInterface* GetAudioInterface(); + IAudioInterface* GetAudioInterface(); }; using AudioManagerUPtr = std::unique_ptr; diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 649631f..026b168 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -5,7 +5,7 @@ list(APPEND linux_HEADERS list(APPEND audio_HEADERS AudioDevice.h AudioManager.h - audio_interfaces/AudioInterface.h + audio_interfaces/IAudioInterface.h midi/MidiReader.h midi/MidiTrack.h midi/MidiDocument.h @@ -20,7 +20,6 @@ list(APPEND linux_INCLUDES list(APPEND audio_LIB_INCLUDES AudioDevice.cpp AudioManager.cpp - audio_interfaces/AudioInterface.cpp midi/MidiReader.cpp midi/MidiTrack.cpp midi/MidiDocument.cpp @@ -28,7 +27,7 @@ list(APPEND audio_LIB_INCLUDES midi/MetaMidiEvent.cpp midi/MidiChannelEvent.cpp) -add_library(audio SHARED ${audio_LIB_INCLUDES} ${audio_HEADERS}) +add_library(audio SHARED ${audio_LIB_INCLUDES} ${linux_INCLUDES} ${audio_HEADERS} ${linux_HEADERS}) target_include_directories(audio PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/src/core/file_utilities" @@ -41,6 +40,6 @@ list(APPEND linux_LIBS asound ) -target_link_libraries(audio PUBLIC core) +target_link_libraries(audio PUBLIC core ${linux_LIBS}) set_target_properties( audio PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) set_property(TARGET audio PROPERTY FOLDER src) \ No newline at end of file diff --git a/src/audio/audio_interfaces/AlsaInterface.cpp b/src/audio/audio_interfaces/AlsaInterface.cpp index 8c05f4d..2f71027 100644 --- a/src/audio/audio_interfaces/AlsaInterface.cpp +++ b/src/audio/audio_interfaces/AlsaInterface.cpp @@ -19,7 +19,7 @@ std::shared_ptr AlsaInterface::Create() return std::make_shared(); } -void AlsaInterface::OpenDevice(AudioDevicePtr device) +void AlsaInterface::OpenDevice(const AudioDevicePtr& device) { MLOG_INFO("Opening Device"); snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; @@ -115,7 +115,7 @@ void AlsaInterface::SetChannelNumber(AudioDevicePtr device) } } -void AlsaInterface::Play(AudioDevicePtr device) +void AlsaInterface::Play(const AudioDevicePtr& device) { MLOG_INFO("Playing audio"); unsigned char *data = (unsigned char *)malloc(mPeriodSize); diff --git a/src/audio/audio_interfaces/AlsaInterface.h b/src/audio/audio_interfaces/AlsaInterface.h index 3d9a1d0..2a733f7 100644 --- a/src/audio/audio_interfaces/AlsaInterface.h +++ b/src/audio/audio_interfaces/AlsaInterface.h @@ -3,37 +3,39 @@ #include #include +#include "IAudioInterface.h" + #include "AudioDevice.h" -class AlsaInterface +class AlsaInterface : public IAudioInterface { - snd_pcm_t* mHandle; - snd_pcm_hw_params_t* mHardwareParams; - snd_pcm_uframes_t mPeriodSize; + snd_pcm_t* mHandle; + snd_pcm_hw_params_t* mHardwareParams; + snd_pcm_uframes_t mPeriodSize; public: - AlsaInterface(); + AlsaInterface(); - ~AlsaInterface(); + ~AlsaInterface(); - static std::shared_ptr Create(); + static std::shared_ptr Create(); - void OpenDevice(AudioDevicePtr device); + void OpenDevice(const AudioDevicePtr& device) override; - void SetAccessType(AudioDevicePtr device); + void SetAccessType(AudioDevicePtr device); - void SetSampleFormat(AudioDevicePtr device); + void SetSampleFormat(AudioDevicePtr device); - void SetSampleRate(AudioDevicePtr device); + void SetSampleRate(AudioDevicePtr device); - void SetPeriod(AudioDevicePtr device); + void SetPeriod(AudioDevicePtr device); - void SetBufferSize(AudioDevicePtr device); + void SetBufferSize(AudioDevicePtr device); - void SetChannelNumber(AudioDevicePtr device); + void SetChannelNumber(AudioDevicePtr device); - void Play(AudioDevicePtr device); + void Play(const AudioDevicePtr& device) override; }; using AlsaInterfacePtr = std::shared_ptr; diff --git a/src/audio/audio_interfaces/AudioInterface.cpp b/src/audio/audio_interfaces/AudioInterface.cpp deleted file mode 100644 index bf4b24b..0000000 --- a/src/audio/audio_interfaces/AudioInterface.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "AudioInterface.h" -#include "AudioDevice.h" - -AudioInterface::AudioInterface() -{ - -} - -std::unique_ptr AudioInterface::Create() -{ - return std::make_unique(); -} - -void AudioInterface::OpenDevice(AudioDevicePtr device) -{ - -} - -void AudioInterface::Play(AudioDevicePtr device) -{ - -} \ No newline at end of file diff --git a/src/audio/audio_interfaces/IAudioInterface.h b/src/audio/audio_interfaces/IAudioInterface.h index 5698e65..1cfad01 100644 --- a/src/audio/audio_interfaces/IAudioInterface.h +++ b/src/audio/audio_interfaces/IAudioInterface.h @@ -3,19 +3,19 @@ #include class AudioDevice; -using AudioDevicePtr = std::shared_ptr; +using AudioDevicePtr = std::unique_ptr; -class AudioInterface +class IAudioInterface { public: - AudioInterface(); + IAudioInterface() = default; - static std::unique_ptr Create(); + virtual ~IAudioInterface() = default; - void OpenDevice(AudioDevicePtr device); + virtual void OpenDevice(const AudioDevicePtr& device) = 0; - void Play(AudioDevicePtr device); + virtual void Play(const AudioDevicePtr& device) = 0; }; -using AudioInterfaceUPtr = std::unique_ptr; \ No newline at end of file +using IAudioInterfaceUPtr = std::unique_ptr; diff --git a/src/console/MainApplication.cpp b/src/console/MainApplication.cpp index b2ec240..01e604f 100644 --- a/src/console/MainApplication.cpp +++ b/src/console/MainApplication.cpp @@ -20,7 +20,7 @@ MainApplication::~MainApplication() void MainApplication::Initialize(CommandLineArgsUPtr commandLineArgs) { mCommandLineArgs = std::move(commandLineArgs); - std::string launch_path = mCommandLineArgs->GetLaunchPath().string(); + const auto launch_path = mCommandLineArgs->GetLaunchPath().string(); FileLogger::GetInstance().SetWorkDirectory(launch_path); FileLogger::GetInstance().Open(); diff --git a/src/core/ByteUtils.h b/src/core/ByteUtils.h index 31008e7..63ab3bb 100644 --- a/src/core/ByteUtils.h +++ b/src/core/ByteUtils.h @@ -4,78 +4,78 @@ class ByteUtils { public: - using Word = int; - using DWord = int; + using Word = int; + using DWord = int; - static int GetWordFirstBit(const Word word) - { - return word & ByteUtils::WORD_FIRST_BIT; - }; + static int GetWordFirstBit(const Word word) + { + return word & ByteUtils::WORD_FIRST_BIT; + }; - static int GetWordLastByte(const Word word) - { - return word & ByteUtils::WORD_LAST_BYTE; - } + static int GetWordLastByte(const Word word) + { + return word & ByteUtils::WORD_LAST_BYTE; + } - static void ReverseBuffer(char* buffer, char* reverse, unsigned size) - { - for(unsigned idx=0; idx Database::Create() +std::unique_ptr Database::Create() { - return std::make_shared(); + return std::make_unique(); } void Database::SetPath(const std::string& path) { - mPath = path; + mPath = path; } -std::string Database::GetPath() +std::string Database::GetPath() const { - return mPath; + return mPath; } diff --git a/src/database/Database.h b/src/database/Database.h index ebbe8f7..a645ec1 100644 --- a/src/database/Database.h +++ b/src/database/Database.h @@ -6,19 +6,19 @@ class Database { - std::string mPath; + std::string mPath; public: - Database(); + Database(); - ~Database(); + ~Database(); - static std::shared_ptr Create(); + static std::unique_ptr Create(); - void SetPath(const std::string& path); + void SetPath(const std::string& path); - std::string GetPath(); + std::string GetPath() const; }; -using DatabasePtr = std::shared_ptr; +using DatabasePtr = std::unique_ptr; diff --git a/src/database/DatabaseManager.cpp b/src/database/DatabaseManager.cpp index 6f3b552..e89653b 100644 --- a/src/database/DatabaseManager.cpp +++ b/src/database/DatabaseManager.cpp @@ -1,8 +1,8 @@ #include "DatabaseManager.h" DatabaseManager::DatabaseManager() - : mDatabase(), - mDatabaseInterface() + : mDatabase(), + mDatabaseInterface() { } @@ -12,25 +12,25 @@ DatabaseManager::~DatabaseManager() } -std::shared_ptr DatabaseManager::Create() +std::unique_ptr DatabaseManager::Create() { - return std::make_shared(); + return std::make_unique(); } void DatabaseManager::CreateDatabase(const std::string& path) { - mDatabase = Database::Create(); - mDatabase->SetPath(path); + mDatabase = Database::Create(); + mDatabase->SetPath(path); - mDatabaseInterface = SqliteInterface::Create(); - mDatabaseInterface->Open(mDatabase); + mDatabaseInterface = SqliteInterface::Create(); + mDatabaseInterface->Open(mDatabase); } void DatabaseManager::OnShutDown() { - if(mDatabaseInterface) - { - mDatabaseInterface->Close(); - } + if(mDatabaseInterface) + { + mDatabaseInterface->Close(); + } } diff --git a/src/database/DatabaseManager.h b/src/database/DatabaseManager.h index cb1b401..fa156fa 100644 --- a/src/database/DatabaseManager.h +++ b/src/database/DatabaseManager.h @@ -8,20 +8,20 @@ class DatabaseManager { - DatabasePtr mDatabase; - SqliteInterfacePtr mDatabaseInterface; + DatabasePtr mDatabase; + SqliteInterfacePtr mDatabaseInterface; public: - DatabaseManager(); + DatabaseManager(); - ~DatabaseManager(); + ~DatabaseManager(); - static std::shared_ptr Create(); + static std::unique_ptr Create(); - void CreateDatabase(const std::string& path); + void CreateDatabase(const std::string& path); - void OnShutDown(); + void OnShutDown(); }; -using DatabaseManagerPtr = std::shared_ptr; +using DatabaseManagerPtr = std::unique_ptr; diff --git a/src/database/database_interfaces/SqliteInterface.cpp b/src/database/database_interfaces/SqliteInterface.cpp index acddac2..5a6508a 100644 --- a/src/database/database_interfaces/SqliteInterface.cpp +++ b/src/database/database_interfaces/SqliteInterface.cpp @@ -3,7 +3,7 @@ #include SqliteInterface::SqliteInterface() - : mSqliteDb(nullptr) + : mSqliteDb(nullptr) { } @@ -13,44 +13,44 @@ SqliteInterface::~SqliteInterface() } -std::shared_ptr SqliteInterface::Create() +std::unique_ptr SqliteInterface::Create() { - return std::make_shared(); + return std::make_unique(); } -void SqliteInterface::Open(DatabasePtr db) +void SqliteInterface::Open(const DatabasePtr& db) { - int rc = sqlite3_open(db->GetPath().c_str(), &mSqliteDb); - if( rc ) - { - std::cout << "Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb) << std::endl; - sqlite3_close(mSqliteDb); - } + int rc = sqlite3_open(db->GetPath().c_str(), &mSqliteDb); + if( rc ) + { + std::cout << "Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb) << std::endl; + sqlite3_close(mSqliteDb); + } } static int callback(void *NotUsed, int argc, char **argv, char **azColName) { - for(int i=0; i Create(); + static std::unique_ptr Create(); - void Open(DatabasePtr db); + void Open(const DatabasePtr& db); - void Close(); + void Close(); - void Run(const std::string& statement); + void Run(const std::string& statement); }; -using SqliteInterfacePtr = std::shared_ptr; +using SqliteInterfacePtr = std::unique_ptr; diff --git a/src/network/CMakeLists.txt b/src/network/CMakeLists.txt index 0a0b067..bf5c499 100644 --- a/src/network/CMakeLists.txt +++ b/src/network/CMakeLists.txt @@ -10,10 +10,9 @@ list(APPEND network_HEADERS list(APPEND network_LIB_INCLUDES NetworkManager.cpp sockets/Socket.cpp - sockets/SocketInterface.cpp ) -add_library(network SHARED ${network_LIB_INCLUDES} ${network_HEADERS}) +add_library(network SHARED ${network_LIB_INCLUDES} ${linux_INCLUDES} ${network_HEADERS}) target_include_directories(network PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" diff --git a/src/network/NetworkManager.cpp b/src/network/NetworkManager.cpp index 6a60f12..7eb6e1a 100644 --- a/src/network/NetworkManager.cpp +++ b/src/network/NetworkManager.cpp @@ -1,8 +1,11 @@ #include "NetworkManager.h" +#ifdef __linux__ +#include "UnixSocketInterface.h" +#endif NetworkManager::NetworkManager() : mActiveSockets(), - mSocketInterface() + mSocketInterface() { } @@ -19,13 +22,18 @@ std::unique_ptr NetworkManager::Create() void NetworkManager::Initialize() { - mSocketInterface = SocketInterface::Create(); + mSocketInterface = UnixSocketInterface::Create(); } void NetworkManager::RunHttpServer() { + if (!mSocketInterface) + { + Initialize(); + } + auto socket = Socket::Create(); - mSocketInterface->CreateSocket(socket); + mSocketInterface->InitializeSocket(socket); mSocketInterface->Listen(socket); mSocketInterface->Run(socket); } diff --git a/src/network/NetworkManager.h b/src/network/NetworkManager.h index 71afc11..0e6c99d 100644 --- a/src/network/NetworkManager.h +++ b/src/network/NetworkManager.h @@ -1,15 +1,15 @@ #pragma once -#include -#include - #include "Socket.h" #include "SocketInterface.h" +#include +#include + class NetworkManager { std::vector mActiveSockets; - SocketInterfaceUPtr mSocketInterface; + ISocketInterfaceUPtr mSocketInterface; public: @@ -21,10 +21,6 @@ public: void Initialize(); - void OpenSocket(SocketPtr socket); - - void CloseSocket(SocketPtr socket); - void RunHttpServer(); void ShutDown(); diff --git a/src/network/sockets/Socket.cpp b/src/network/sockets/Socket.cpp index 88277c0..f273021 100644 --- a/src/network/sockets/Socket.cpp +++ b/src/network/sockets/Socket.cpp @@ -1,8 +1,9 @@ #include "Socket.h" Socket::Socket() - : mPort(8888), - mMessage() + : mPort(8888), + mMessage(), + mHandle(-1) { } @@ -12,27 +13,37 @@ Socket::~Socket() } -std::shared_ptr Socket::Create() +void Socket::SetHandle(SocketHandle handle) { - return std::make_shared(); + mHandle = handle; } -std::string Socket::GetMessage() +Socket::SocketHandle Socket::GetHandle() const { - return mMessage; + return mHandle; +} + +std::unique_ptr Socket::Create() +{ + return std::make_unique(); +} + +std::string Socket::GetMessage() const +{ + return mMessage; } void Socket::SetMessage(const std::string& message) { - mMessage = message; + mMessage = message; } void Socket::SetPort(unsigned port) { - mPort = port; + mPort = port; } -unsigned Socket::GetPort() +unsigned Socket::GetPort() const { - return mPort; + return mPort; } diff --git a/src/network/sockets/Socket.h b/src/network/sockets/Socket.h index 093512e..9bc421b 100644 --- a/src/network/sockets/Socket.h +++ b/src/network/sockets/Socket.h @@ -5,24 +5,32 @@ class Socket { - unsigned mPort; - std::string mMessage; - + using SocketHandle = int; public: - Socket(); + Socket(); - ~Socket(); + ~Socket(); - static std::shared_ptr Create(); + static std::unique_ptr Create(); - void SetPort(unsigned port); + void SetPort(unsigned port); - unsigned GetPort(); + void SetHandle(SocketHandle handle); - std::string GetMessage(); + SocketHandle GetHandle() const; - void SetMessage(const std::string& message); + unsigned GetPort() const; + + std::string GetMessage() const; + + void SetMessage(const std::string& message); + +private: + + SocketHandle mHandle; + unsigned mPort; + std::string mMessage; }; -using SocketPtr = std::shared_ptr; +using SocketPtr = std::unique_ptr; diff --git a/src/network/sockets/SocketInterface.cpp b/src/network/sockets/SocketInterface.cpp deleted file mode 100644 index e929d79..0000000 --- a/src/network/sockets/SocketInterface.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "SocketInterface.h" -#include "Socket.h" - -SocketInterface::SocketInterface() -{ - -} - -std::unique_ptr SocketInterface::Create() -{ - return std::make_unique(); -} - -void SocketInterface::CreateSocket(SocketPtr socket) -{ - -} - -void SocketInterface::Listen(SocketPtr socket) -{ - -} - -void SocketInterface::Run(SocketPtr socket) -{ - -} \ No newline at end of file diff --git a/src/network/sockets/SocketInterface.h b/src/network/sockets/SocketInterface.h index 6004be2..d5046b3 100644 --- a/src/network/sockets/SocketInterface.h +++ b/src/network/sockets/SocketInterface.h @@ -1,20 +1,21 @@ -#pragma +#pragma once #include class Socket; -using SocketPtr = std::shared_ptr; +using SocketPtr = std::unique_ptr; -class SocketInterface +class ISocketInterface { public: - SocketInterface(); + ISocketInterface() = default; - static std::unique_ptr Create(); - void CreateSocket(SocketPtr socket); - void Listen(SocketPtr socket); - void Run(SocketPtr socket); + virtual ~ISocketInterface() = default; + + virtual void InitializeSocket(const SocketPtr& socket) = 0; + virtual void Listen(const SocketPtr& socket) = 0; + virtual void Run(const SocketPtr& socket) = 0; }; -using SocketInterfaceUPtr = std::unique_ptr; \ No newline at end of file +using ISocketInterfaceUPtr = std::unique_ptr; diff --git a/src/network/sockets/UnixSocketInterface.cpp b/src/network/sockets/UnixSocketInterface.cpp index b693951..5a77ff6 100644 --- a/src/network/sockets/UnixSocketInterface.cpp +++ b/src/network/sockets/UnixSocketInterface.cpp @@ -1,86 +1,82 @@ #include "UnixSocketInterface.h" +#include "HttpResponse.h" + #include #include #include #include #include #include -#include "HttpResponse.h" UnixSocketInterface::UnixSocketInterface() - : mOpeningHandles(), - mBufferSize(1024) + : mBufferSize(1024) { } -UnixSocketInterface::~UnixSocketInterface() +std::unique_ptr UnixSocketInterface::Create() { - + return std::make_unique(); } -std::shared_ptr UnixSocketInterface::Create() +void UnixSocketInterface::InitializeSocket(const SocketPtr& socketPtr) { - return std::make_shared(); + auto handle = socket(AF_INET, SOCK_STREAM, 0); + socketPtr->SetHandle(handle); } -void UnixSocketInterface::CreateSocket(SocketPtr socketPtr) +void UnixSocketInterface::Listen(const SocketPtr& socket) { - mOpeningHandles[socketPtr] = socket(AF_INET, SOCK_STREAM, 0); -} - -void UnixSocketInterface::Listen(SocketPtr socket) -{ - if(mOpeningHandles[socket] < 0) + if(socket->GetHandle() < 0) { - std::cerr << "Error opening socket" << std::endl; - return; + std::cerr << "Error opening socket" << std::endl; + return; } - int port = static_cast(socket->GetPort()); + int port = static_cast(socket->GetPort()); struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(port); - int result = bind(mOpeningHandles[socket], (struct sockaddr *)&serv_addr, sizeof(serv_addr)); + int result = bind(socket->GetHandle(), (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if(result< 0) { - std::cerr << "Error binding socket" << std::endl; - return; + std::cerr << "Error binding socket" << std::endl; + return; } - listen(mOpeningHandles[socket], 5); + listen(socket->GetHandle(), 5); } -void UnixSocketInterface::Run(SocketPtr socket) +void UnixSocketInterface::Run(const SocketPtr& socket) { - if(mOpeningHandles[socket] < 0) + if(socket->GetHandle() < 0) { - std::cerr << "Error opening socket" << std::endl; - return; + std::cerr << "Error opening socket" << std::endl; + return; } struct sockaddr_in cli_addr; socklen_t clilen = sizeof(cli_addr); while(true) { - SocketHandle new_socket_handle = accept(mOpeningHandles[socket], - (struct sockaddr *) &cli_addr, &clilen); + auto new_socket_handle = accept(socket->GetHandle(), + (struct sockaddr *) &cli_addr, &clilen); if (new_socket_handle < 0) { - std::cerr << "Error on accept" << std::endl; - return; + std::cerr << "Error on accept" << std::endl; + return; } char buffer[mBufferSize] = {0}; int n = read(new_socket_handle, buffer, mBufferSize); if (n < 0) { - std::cerr << "Error on read" << std::endl; - return; + std::cerr << "Error on read" << std::endl; + return; } socket->SetMessage(buffer); std::cout << "Here is the message: " << buffer << std::endl; @@ -88,12 +84,12 @@ void UnixSocketInterface::Run(SocketPtr socket) HttpResponse response; response.SetBody("Hello world!"); - std::string response_message = response.ToString(); + auto response_message = response.ToString(); n = write(new_socket_handle, response_message.c_str(), response_message.length()); if (n < 0) { - std::cerr << "Error on write" << std::endl; - return; + std::cerr << "Error on write" << std::endl; + return; } close(new_socket_handle); } diff --git a/src/network/sockets/UnixSocketInterface.h b/src/network/sockets/UnixSocketInterface.h index b131a61..829ae8a 100644 --- a/src/network/sockets/UnixSocketInterface.h +++ b/src/network/sockets/UnixSocketInterface.h @@ -1,30 +1,31 @@ #pragma once +#include "Socket.h" +#include "SocketInterface.h" + #include #include -#include -#include "Socket.h" - -class UnixSocketInterface +class UnixSocketInterface : public ISocketInterface { - using SocketHandle = int; - std::map mOpeningHandles; - std::size_t mBufferSize; public: - UnixSocketInterface(); + UnixSocketInterface(); - ~UnixSocketInterface(); + virtual ~UnixSocketInterface() = default; - static std::shared_ptr Create(); + static std::unique_ptr Create(); - void CreateSocket(SocketPtr socket); + void InitializeSocket(const SocketPtr& socket) override; - void Listen(SocketPtr socket); + void Listen(const SocketPtr& socket) override; - void Run(SocketPtr socket); + void Run(const SocketPtr& socket) override; + +private: + + std::size_t mBufferSize { 0 }; }; -using UnixSocketInterfacePtr = std::shared_ptr; +using UnixSocketInterfacePtr = std::unique_ptr;