Skip to content

Commit e198f16

Browse files
committed
print error message
1 parent 3ff68fb commit e198f16

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

emmy_core/emmy_facade.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,36 @@ EmmyFacade::~EmmyFacade() {
3737
delete transporter;
3838
}
3939

40+
int LuaError(lua_State* L) {
41+
std::string msg = lua_tostring(L, 1);
42+
msg = "[Emmy]" + msg;
43+
lua_getglobal(L, "error");
44+
lua_pushstring(L, msg.c_str());
45+
lua_call(L, 1, 0);
46+
return 0;
47+
}
48+
49+
int LuaPrint(lua_State* L) {
50+
std::string msg = lua_tostring(L, 1);
51+
msg = "[Emmy]" + msg;
52+
lua_getglobal(L, "print");
53+
lua_pushstring(L, msg.c_str());
54+
lua_call(L, 1, 0);
55+
return 0;
56+
}
57+
4058
bool EmmyFacade::TcpListen(lua_State* L, const std::string& host, int port, std::string& err) {
4159
Destroy();
4260
this->L = L;
4361
const auto s = new SocketServerTransporter();
4462
transporter = s;
4563
s->SetHandler(this);
4664
const auto suc = s->Listen(host, port, err);
65+
if (!suc) {
66+
lua_pushcfunction(L, LuaError);
67+
lua_pushstring(L, err.c_str());
68+
lua_call(L, 1, 0);
69+
}
4770
return suc;
4871
}
4972

@@ -56,6 +79,10 @@ bool EmmyFacade::TcpConnect(lua_State* L, const std::string& host, int port, std
5679
const auto suc = c->Connect(host, port, err);
5780
if (suc) {
5881
WaitIDE(true);
82+
} else {
83+
lua_pushcfunction(L, LuaError);
84+
lua_pushstring(L, err.c_str());
85+
lua_call(L, 1, 0);
5986
}
6087
return suc;
6188
}

emmy_core/proto/socket_client_transporter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ static void after_read(uv_stream_t* handle,
3838
SocketClientTransporter::SocketClientTransporter():
3939
Transporter(false),
4040
uvClient({}),
41-
connect_req({}) {
41+
connect_req({}),
42+
connectionStatus(0) {
4243
}
4344

4445
SocketClientTransporter::~SocketClientTransporter() {
@@ -68,10 +69,14 @@ bool SocketClientTransporter::Connect(const std::string& host, int port, std::st
6869
StartEventLoop();
6970
std::unique_lock<std::mutex> lock(mutex);
7071
cv.wait(lock);
72+
if (this->connectionStatus < 0) {
73+
err = uv_strerror(this->connectionStatus);
74+
}
7175
return IsConnected();
7276
}
7377

7478
void SocketClientTransporter::OnConnection(uv_connect_t* req, int status) {
79+
this->connectionStatus = status;
7580
if (status >= 0) {
7681
OnConnect(true);
7782
uv_read_start((uv_stream_t*)&uvClient, echo_alloc, after_read);

emmy_core/proto/socket_client_transporter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SocketClientTransporter : public Transporter {
2424
uv_connect_t connect_req;
2525
std::mutex mutex;
2626
std::condition_variable cv;
27+
int connectionStatus;
2728
public:
2829
SocketClientTransporter();
2930
~SocketClientTransporter();

0 commit comments

Comments
 (0)