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_
Implements mutex, reentrant timeout mutex, and reentrant mutex.
UniquePtr< uint8_t[], ReallocDeleter > UniquePtrType
The typedefed UniquePtr to a uint8_t array.
void nlib_free(void *ptr)
A weak function that calls the C standard function free. nlib calls free via this function...
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24