nlib
jsonrpc.h
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #ifndef SAMPLES_SOURCES_MSGPACK_JSONRPC_JSONRPC_H_
16 #define SAMPLES_SOURCES_MSGPACK_JSONRPC_JSONRPC_H_
17 
18 #include <stdlib.h>
19 #include <list>
20 #include <utility>
23 
24 #ifndef NLIB_SOCKET_ENABLED
25 #define DONT_USE_SOCKET
26 #endif
27 
28 class Tunnel {
29  public:
30  ~Tunnel() {
31  using nlib_ns::threading::SimpleCriticalSection;
32  using nlib_ns::threading::ScopedLock;
33  ScopedLock<SimpleCriticalSection> l(lock_);
34  BufType::iterator it = buf_.begin();
35  BufType::iterator end = buf_.end();
36  for (; it != end; ++it) {
37  nlib_free(it->first);
38  }
39  }
40  void Send(nlib_ns::ReallocOutputStream::UniquePtrType* data, size_t n) {
41  using nlib_ns::threading::SimpleCriticalSection;
42  using nlib_ns::threading::ScopedLock;
43  ScopedLock<SimpleCriticalSection> l(lock_);
44  buf_.push_back(BufItem(data->release(), n));
45  }
46  bool Receive(nlib_ns::ReallocOutputStream::UniquePtrType* data, size_t* n) {
47  using nlib_ns::threading::SimpleCriticalSection;
48  using nlib_ns::threading::ScopedLock;
49  ScopedLock<SimpleCriticalSection> l(lock_);
50  if (buf_.empty()) return false;
51  BufItem& item = buf_.front();
52  data->reset(item.first);
53  *n = item.second;
54  buf_.pop_front();
55  return true;
56  }
57 
58  private:
59  typedef std::pair<uint8_t*, size_t> BufItem;
60  typedef std::list<BufItem> BufType;
61  nlib_ns::threading::SimpleCriticalSection lock_;
62  BufType buf_;
63 };
64 
65 extern Tunnel g_c2s;
66 extern Tunnel g_s2c;
67 
68 errno_t InitServer();
69 void ServerThread(void*);
70 extern volatile bool g_shutdown_server;
71 
72 #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:37