Skip to content

Commit

Permalink
Optimize build (#416)
Browse files Browse the repository at this point in the history
* Optimize build

* fix windows and other errors

* fix windows
  • Loading branch information
MisterTea authored Apr 1, 2021
1 parent a655c78 commit 1c170a8
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 52 deletions.
7 changes: 1 addition & 6 deletions src/base/Headers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ inline int close(int fd) { return ::closesocket(fd); }
#include <array>
#include <atomic>
#include <ctime>
#include <cxxopts.hpp>
#include <deque>
#include <exception>
#include <fstream>
Expand All @@ -92,11 +91,9 @@ using namespace std::experimental;

#include "ET.pb.h"
#include "ETerminal.pb.h"
#include "SimpleIni.h"
#include "ThreadPool.h"
#include "base64.h"
#include "easylogging++.h"
#include "json.hpp"
#include "sago/platform_folders.h"
#include "sole.hpp"

Expand Down Expand Up @@ -131,8 +128,6 @@ typedef int ssize_t;

using namespace std;

using json = nlohmann::json;

// The ET protocol version supported by this binary
static const int PROTOCOL_VERSION = 6;

Expand Down Expand Up @@ -306,7 +301,7 @@ inline void HandleTerminate() {
if (eptr) {
try {
std::rethrow_exception(eptr);
} catch (const std::exception &e) {
} catch (const std::exception& e) {
STFATAL << "Uncaught c++ exception: " << e.what();
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/base/JsonLib.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "json.hpp"

using json = nlohmann::json;
8 changes: 4 additions & 4 deletions src/base/PipeSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace et {
PipeSocketHandler::PipeSocketHandler() {}

int PipeSocketHandler::connect(const SocketEndpoint& endpoint) {
lock_guard<std::recursive_mutex> mutexGuard(mutex);
lock_guard<std::recursive_mutex> mutexGuard(globalMutex);

string pipePath = endpoint.name();
sockaddr_un remote;
Expand Down Expand Up @@ -91,7 +91,7 @@ int PipeSocketHandler::connect(const SocketEndpoint& endpoint) {
}

set<int> PipeSocketHandler::listen(const SocketEndpoint& endpoint) {
lock_guard<std::recursive_mutex> guard(mutex);
lock_guard<std::recursive_mutex> guard(globalMutex);

string pipePath = endpoint.name();
if (pipeServerSockets.find(pipePath) != pipeServerSockets.end()) {
Expand All @@ -118,7 +118,7 @@ set<int> PipeSocketHandler::listen(const SocketEndpoint& endpoint) {
}

set<int> PipeSocketHandler::getEndpointFds(const SocketEndpoint& endpoint) {
lock_guard<std::recursive_mutex> guard(mutex);
lock_guard<std::recursive_mutex> guard(globalMutex);

string pipePath = endpoint.name();
if (pipeServerSockets.find(pipePath) == pipeServerSockets.end()) {
Expand All @@ -129,7 +129,7 @@ set<int> PipeSocketHandler::getEndpointFds(const SocketEndpoint& endpoint) {
}

void PipeSocketHandler::stopListening(const SocketEndpoint& endpoint) {
lock_guard<std::recursive_mutex> guard(mutex);
lock_guard<std::recursive_mutex> guard(globalMutex);

string pipePath = endpoint.name();
auto it = pipeServerSockets.find(pipePath);
Expand Down
28 changes: 28 additions & 0 deletions src/base/SocketHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "SocketHandler.hpp"

#include "base64.h"

namespace et {
#define SOCKET_DATA_TRANSFER_TIMEOUT (10)

Expand Down Expand Up @@ -98,4 +100,30 @@ void SocketHandler::writeAllOrThrow(int fd, const void* buf, size_t count,
}
}
}

void SocketHandler::writeB64(int fd, const char* buf, size_t count) {
size_t encodedLength = Base64::EncodedLength(count);
string s(encodedLength, '\0');
if (!Base64::Encode(buf, count, &s[0], s.length())) {
throw runtime_error("b64 decode failed");
}
writeAllOrThrow(fd, &s[0], s.length(), false);
}

void SocketHandler::readB64(int fd, char* buf, size_t count) {
size_t encodedLength = Base64::EncodedLength(count);
string s(encodedLength, '\0');
readAll(fd, &s[0], s.length(), false);
if (!Base64::Decode((const char*)&s[0], s.length(), buf, count)) {
throw runtime_error("b64 decode failed");
}
}

void SocketHandler::readB64EncodedLength(int fd, string* out, size_t encodedLength) {
string s(encodedLength, '\0');
readAll(fd, &s[0], s.length(), false);
if (!Base64::Decode(s, out)) {
throw runtime_error("b64 decode failed");
}
}
} // namespace et
28 changes: 3 additions & 25 deletions src/base/SocketHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,9 @@ class SocketHandler {
}
}

inline void writeB64(int fd, const char* buf, size_t count) {
size_t encodedLength = Base64::EncodedLength(count);
string s(encodedLength, '\0');
if (!Base64::Encode(buf, count, &s[0], s.length())) {
throw runtime_error("b64 decode failed");
}
writeAllOrThrow(fd, &s[0], s.length(), false);
}

inline void readB64(int fd, char* buf, size_t count) {
size_t encodedLength = Base64::EncodedLength(count);
string s(encodedLength, '\0');
readAll(fd, &s[0], s.length(), false);
if (!Base64::Decode((const char*)&s[0], s.length(), buf, count)) {
throw runtime_error("b64 decode failed");
}
}

inline void readB64EncodedLength(int fd, string* out, size_t encodedLength) {
string s(encodedLength, '\0');
readAll(fd, &s[0], s.length(), false);
if (!Base64::Decode(s, out)) {
throw runtime_error("b64 decode failed");
}
}
void writeB64(int fd, const char* buf, size_t count);
void readB64(int fd, char* buf, size_t count);
void readB64EncodedLength(int fd, string* out, size_t encodedLength);

virtual int connect(const SocketEndpoint& endpoint) = 0;
virtual set<int> listen(const SocketEndpoint& endpoint) = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/base/TcpSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace et {
TcpSocketHandler::TcpSocketHandler() {}

int TcpSocketHandler::connect(const SocketEndpoint &endpoint) {
lock_guard<std::recursive_mutex> guard(mutex);
lock_guard<std::recursive_mutex> guard(globalMutex);
int sockFd = -1;
addrinfo *results = NULL;
addrinfo *p = NULL;
Expand Down Expand Up @@ -160,7 +160,7 @@ int TcpSocketHandler::connect(const SocketEndpoint &endpoint) {
}

set<int> TcpSocketHandler::listen(const SocketEndpoint &endpoint) {
lock_guard<std::recursive_mutex> guard(mutex);
lock_guard<std::recursive_mutex> guard(globalMutex);

int port = endpoint.port();
if (portServerSockets.find(port) != portServerSockets.end()) {
Expand Down Expand Up @@ -246,7 +246,7 @@ set<int> TcpSocketHandler::listen(const SocketEndpoint &endpoint) {
}

set<int> TcpSocketHandler::getEndpointFds(const SocketEndpoint &endpoint) {
lock_guard<std::recursive_mutex> guard(mutex);
lock_guard<std::recursive_mutex> guard(globalMutex);

int port = endpoint.port();
if (portServerSockets.find(port) == portServerSockets.end()) {
Expand All @@ -257,7 +257,7 @@ set<int> TcpSocketHandler::getEndpointFds(const SocketEndpoint &endpoint) {
}

void TcpSocketHandler::stopListening(const SocketEndpoint &endpoint) {
lock_guard<std::recursive_mutex> guard(mutex);
lock_guard<std::recursive_mutex> guard(globalMutex);

int port = endpoint.port();
auto it = portServerSockets.find(port);
Expand Down
1 change: 1 addition & 0 deletions src/htm/HtmClientMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "PipeSocketHandler.hpp"
#include "RawSocketUtils.hpp"
#include "SubprocessToString.hpp"
#include <cxxopts.hpp>

using namespace et;

Expand Down
5 changes: 3 additions & 2 deletions src/htm/HtmServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "HtmHeaderCodes.hpp"
#include "LogHandler.hpp"
#include "MultiplexerState.hpp"
#include "base64.h"

namespace et {
HtmServer::HtmServer(shared_ptr<SocketHandler> _socketHandler,
Expand Down Expand Up @@ -67,7 +68,7 @@ void HtmServer::run() {
closeEndpoint();
}
if (data[0] == 'd') {
string jsonString = state.toJson().dump();
string jsonString = state.toJsonString();
LOG(INFO) << "Current State: " << jsonString;
}
break;
Expand Down Expand Up @@ -158,7 +159,7 @@ void HtmServer::recover() {

{
unsigned char header = INIT_STATE;
string jsonString = state.toJson().dump();
string jsonString = state.toJsonString();
int32_t length = jsonString.length();
VLOG(1) << "SENDING INIT: " << jsonString;
socketHandler->writeAllOrThrow(endpointFd, (const char *)&header, 1, false);
Expand Down
5 changes: 3 additions & 2 deletions src/htm/MultiplexerState.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "MultiplexerState.hpp"

#include "JsonLib.hpp"
#include "HtmHeaderCodes.hpp"

namespace et {
Expand Down Expand Up @@ -64,7 +65,7 @@ MultiplexerState::MultiplexerState(shared_ptr<SocketHandler> _socketHandler)
}
}

json MultiplexerState::toJson() {
string MultiplexerState::toJsonString() {
json state;
state["shell"] = string(::getenv("SHELL"));

Expand All @@ -80,7 +81,7 @@ json MultiplexerState::toJson() {
state["splits"][it.first] = it.second->toJson();
}

return state;
return state.dump();
}

void MultiplexerState::appendData(const string &uid, const string &data) {
Expand Down
2 changes: 1 addition & 1 deletion src/htm/MultiplexerState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MultiplexerState {

public:
MultiplexerState(shared_ptr<SocketHandler> _socketHandler);
json toJson();
string toJsonString();
void appendData(const string& uid, const string& data);
void newTab(const string& tabId, const string& paneId);
void newSplit(const string& sourceId, const string& paneId, bool vertical);
Expand Down
18 changes: 11 additions & 7 deletions src/terminal/TelemetryService.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "TelemetryService.hpp"

#include "JsonLib.hpp"
#include "SimpleIni.h"

#if defined(USE_SENTRY)
#include "sentry.h"
#endif
Expand Down Expand Up @@ -76,7 +79,8 @@ TelemetryService::TelemetryService(const bool _allow,
const string& _environment)
: allowed(_allow),
environment(_environment),
logHttpClient("https://browser-http-intake.logs.datadoghq.com"),
logHttpClient(new httplib::Client(
"https://browser-http-intake.logs.datadoghq.com")),
shuttingDown(false) {
char* disableTelementry = ::getenv("ET_NO_TELEMETRY");
if (disableTelementry) {
Expand Down Expand Up @@ -119,7 +123,7 @@ TelemetryService::TelemetryService(const bool _allow,

#ifdef USE_SENTRY
sentry_options_t* options = sentry_options_new();
logHttpClient.set_compress(true);
logHttpClient->set_compress(true);

// this is an example. for real usage, make sure to set this explicitly to
// an app specific cache location.
Expand Down Expand Up @@ -198,17 +202,17 @@ TelemetryService::TelemetryService(const bool _allow,
httplib::Headers headers;
headers.emplace("DD-API-KEY", "e5e757f30a9e567f95b16b7673b09253");

logHttpClient.set_connection_timeout(0,
300000); // 300 milliseconds
logHttpClient.set_read_timeout(1, 0); // 1 second
logHttpClient.set_write_timeout(1, 0); // 1 second
logHttpClient->set_connection_timeout(0,
300000); // 300 milliseconds
logHttpClient->set_read_timeout(1, 0); // 1 second
logHttpClient->set_write_timeout(1, 0); // 1 second

if (shuttingDown) {
// httplib isn't exit-safe, so we try our best to avoid calling it
// on shutdown
break;
}
logHttpClient.Post(
logHttpClient->Post(
"/v1/input/"
"pubfe47c2f8dfb3e8c26eb66ba4a456ec79?ddsource=browser&ddtags="
"sdk_version:2.1.1",
Expand Down
6 changes: 5 additions & 1 deletion src/terminal/TelemetryService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "Headers.hpp"

namespace httplib {
class Client;
}

namespace et {
class TelemetryService {
public:
Expand Down Expand Up @@ -40,7 +44,7 @@ class TelemetryService {
static shared_ptr<TelemetryService> telemetryServiceInstance;
bool allowed;
string environment;
httplib::Client logHttpClient;
unique_ptr<httplib::Client> logHttpClient;
recursive_mutex logMutex;
vector<map<string, string>> logBuffer;
bool shuttingDown;
Expand Down
2 changes: 2 additions & 0 deletions src/terminal/TerminalClientMain.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cxxopts.hpp>

#include "ParseConfigFile.hpp"
#include "PipeSocketHandler.hpp"
#include "PsuedoTerminalConsole.hpp"
Expand Down
2 changes: 2 additions & 0 deletions src/terminal/TerminalMain.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cxxopts.hpp>

#include "DaemonCreator.hpp"
#include "LogHandler.hpp"
#include "ParseConfigFile.hpp"
Expand Down
3 changes: 3 additions & 0 deletions src/terminal/TerminalServerMain.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "TelemetryService.hpp"
#include "TerminalServer.hpp"

#include "SimpleIni.h"
#include <cxxopts.hpp>

using namespace et;
namespace google {}
namespace gflags {}
Expand Down

0 comments on commit 1c170a8

Please sign in to comment.