forked from jinq0123/grpc-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBindClientSyncReaderWriter.cpp
More file actions
52 lines (41 loc) · 1.42 KB
/
BindClientSyncReaderWriter.cpp
File metadata and controls
52 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "BindClientSyncReaderWriter.h"
#include "common/GetTimeoutMs.h"
#include <grpc_cb_core/client/client_sync_reader_writer.h> // for ClientSyncReaderWriter
#include <LuaIntf/LuaIntf.h>
#include <string>
using namespace grpc_cb_core;
using namespace LuaIntf;
namespace {
ClientSyncReaderWriter GetClientSyncReaderWriter(const ChannelSptr& pChannel,
const std::string& sMethod, const LuaRef& timeoutSec)
{
int64_t nTimeoutMs = util::GetTimeoutMs(timeoutSec);
return ClientSyncReaderWriter(pChannel, sMethod, nTimeoutMs);
}
// return string|nil, nil means error or end
LuaRef ReadOne(const ClientSyncReaderWriter& rw, lua_State* L)
{
assert(L);
std::string sMsg;
if (rw.ReadOne(&sMsg))
return LuaRef::fromValue(L, sMsg);
return LuaRef(L, nullptr);
}
} // namespace
namespace client {
void BindClientSyncReaderWriter(const LuaRef& mod)
{
lua_State* L = mod.state();
assert(L);
LuaBinding(mod).beginClass<ClientSyncReaderWriter>("ClientSyncReaderWriter")
.addFactory(&GetClientSyncReaderWriter)
.addFunction("read_one",
[L](const ClientSyncReaderWriter* pRdWr) {
assert(pRdWr);
return ReadOne(*pRdWr, L);
})
.addFunction("write", &ClientSyncReaderWriter::Write)
.addFunction("close_writing", &ClientSyncReaderWriter::CloseWriting)
.endClass();
} // BindClientSyncReaderWriter()
} // namespace client