-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format all code to prepare for CI actions
- Loading branch information
Showing
13 changed files
with
1,069 additions
and
1,114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
BasedOnStyle: LLVM | ||
IndentWidth: 4 | ||
PointerAlignment: Left |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,62 @@ | ||
#include <string> | ||
#include <cstdint> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <cstdint> | ||
#include <string> | ||
|
||
#define FAIL_RESPONSE "Fail" | ||
|
||
namespace mcpp { | ||
class SocketConnection { | ||
private: | ||
int socketHandle; | ||
std::string lastSent; | ||
|
||
static std::string resolveHostname(const std::string& hostname); | ||
|
||
public: | ||
SocketConnection(const std::string& address_str, uint16_t port); | ||
|
||
void send(const std::string& dataString); | ||
|
||
/** | ||
* Takes in parameters supporting std::stringstream conversion and a string prefix and transforms them into | ||
* format "prefix(arg1,arg2,arg3)\n" (e.g. "chat.post(test)\n") and sends command to the server. | ||
* @tparam Types | ||
* @param prefix | ||
* @param args | ||
*/ | ||
template<typename... Types> | ||
void sendCommand(const std::string& prefix, const Types& ...args) { | ||
std::stringstream ss; | ||
|
||
ss << prefix << "("; | ||
|
||
// Iterate over args pack | ||
((ss << args << ','), ...); | ||
// Remove trailing comma | ||
ss.seekp(-1, std::ios_base::end); | ||
|
||
ss << ")\n"; | ||
|
||
send(ss.str()); | ||
} | ||
|
||
/** | ||
* Sends via sendCommand() and returns the result from endpoint | ||
* @tparam T | ||
* @tparam Types | ||
* @param prefix | ||
* @param args | ||
* @return | ||
*/ | ||
template<typename T, typename... Types> | ||
std::string sendReceiveCommand(const T& prefix, const Types& ...args) { | ||
sendCommand(prefix, args...); | ||
auto result = recv(); | ||
return result; | ||
} | ||
|
||
[[nodiscard]] std::string recv() const; | ||
}; | ||
} | ||
class SocketConnection { | ||
private: | ||
int socketHandle; | ||
std::string lastSent; | ||
|
||
static std::string resolveHostname(const std::string& hostname); | ||
|
||
public: | ||
SocketConnection(const std::string& address_str, uint16_t port); | ||
|
||
void send(const std::string& dataString); | ||
|
||
/** | ||
* Takes in parameters supporting std::stringstream conversion and a string | ||
* prefix and transforms them into format "prefix(arg1,arg2,arg3)\n" (e.g. | ||
* "chat.post(test)\n") and sends command to the server. | ||
* @tparam Types | ||
* @param prefix | ||
* @param args | ||
*/ | ||
template <typename... Types> | ||
void sendCommand(const std::string& prefix, const Types&... args) { | ||
std::stringstream ss; | ||
|
||
ss << prefix << "("; | ||
|
||
// Iterate over args pack | ||
((ss << args << ','), ...); | ||
// Remove trailing comma | ||
ss.seekp(-1, std::ios_base::end); | ||
|
||
ss << ")\n"; | ||
|
||
send(ss.str()); | ||
} | ||
|
||
/** | ||
* Sends via sendCommand() and returns the result from endpoint | ||
* @tparam T | ||
* @tparam Types | ||
* @param prefix | ||
* @param args | ||
* @return | ||
*/ | ||
template <typename T, typename... Types> | ||
std::string sendReceiveCommand(const T& prefix, const Types&... args) { | ||
sendCommand(prefix, args...); | ||
auto result = recv(); | ||
return result; | ||
} | ||
|
||
[[nodiscard]] std::string recv() const; | ||
}; | ||
} // namespace mcpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.