nlib
jsonrpc.h
1 
2 #ifndef SAMPLES_SOURCES_MSGPACK_JSONRPC_JSONRPC_H_
3 #define SAMPLES_SOURCES_MSGPACK_JSONRPC_JSONRPC_H_
4 
5 #include <stdlib.h>
6 #include <list>
7 #include <utility>
10 
11 #ifndef NLIB_SOCKET_ENABLED
12 #define DONT_USE_SOCKET
13 #endif
14 
15 class Tunnel {
16  public:
17  ~Tunnel() {
18  using nlib_ns::threading::SimpleCriticalSection;
19  using nlib_ns::threading::ScopedLock;
20  ScopedLock<SimpleCriticalSection> l(lock_);
21  BufType::iterator it = buf_.begin();
22  BufType::iterator end = buf_.end();
23  for (; it != end; ++it) {
24  nlib_free(it->first);
25  }
26  }
27  void Send(nlib_ns::ReallocOutputStream::UniquePtrType* data, size_t n) {
28  using nlib_ns::threading::SimpleCriticalSection;
29  using nlib_ns::threading::ScopedLock;
30  ScopedLock<SimpleCriticalSection> l(lock_);
31  buf_.push_back(BufItem(data->release(), n));
32  }
33  bool Receive(nlib_ns::ReallocOutputStream::UniquePtrType* data, size_t* n) {
34  using nlib_ns::threading::SimpleCriticalSection;
35  using nlib_ns::threading::ScopedLock;
36  ScopedLock<SimpleCriticalSection> l(lock_);
37  if (buf_.empty()) return false;
38  BufItem& item = buf_.front();
39  data->reset(item.first);
40  *n = item.second;
41  buf_.pop_front();
42  return true;
43  }
44 
45  private:
46  typedef std::pair<uint8_t*, size_t> BufItem;
47  typedef std::list<BufItem> BufType;
48  nlib_ns::threading::SimpleCriticalSection lock_;
49  BufType buf_;
50 };
51 
52 extern Tunnel g_c2s;
53 extern Tunnel g_s2c;
54 
55 errno_t InitServer();
56 void ServerThread();
57 extern volatile bool g_ShutdownServer;
58 
59 #endif // SAMPLES_SOURCES_MSGPACK_JSONRPC_JSONRPC_H_
ミューテックス, 再入可能ミューテックス, 再入とタイムアウトが可能なミューテックスを実装しています。 ...
UniquePtr< uint8_t[], ReallocDeleter > UniquePtrType
uint8_tの配列へのUniquePtrをtypedefしたものです。
void nlib_free(void *ptr)
C標準関数のfree()を呼び出すweak関数です。nlibはこの関数を経由してfree()を呼び出します。 ...
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24