BVB Source Codes

uWebSockets Show Networking.h Source code

Return Download uWebSockets: download Networking.h Source code - Download uWebSockets Source code - Type:.h
  1. // the purpose of this header should be to provide SSL and networking wrapped in a common interface
  2. // it should allow cross-platform networking and SSL and also easy usage of mTCP and similar tech
  3.  
  4. #ifndef NETWORKING_UWS_H
  5. #define NETWORKING_UWS_H
  6.  
  7. #include <openssl/opensslv.h>
  8. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  9. #define SSL_CTX_up_ref(x) x->references++
  10. #define SSL_up_ref(x) x->references++
  11. #endif
  12.  
  13. #ifndef __linux
  14. #define MSG_NOSIGNAL 0
  15. #else
  16. #include <endian.h>
  17. #endif
  18.  
  19. #ifdef __APPLE__
  20. #include <libkern/OSByteOrder.h>
  21. #define htobe64(x) OSSwapHostToBigInt64(x)
  22. #define be64toh(x) OSSwapBigToHostInt64(x)
  23. #endif
  24.  
  25. #ifdef _WIN32
  26. #define NOMINMAX
  27. #include <WinSock2.h>
  28. #include <Ws2tcpip.h>
  29. #pragma comment(lib, "ws2_32.lib")
  30. #define SHUT_WR SD_SEND
  31. #ifdef __MINGW32__
  32. // Windows has always been tied to LE
  33. #define htobe64(x) __builtin_bswap64(x)
  34. #define be64toh(x) __builtin_bswap64(x)
  35. #else
  36. #define __thread __declspec(thread)
  37. #define htobe64(x) htonll(x)
  38. #define be64toh(x) ntohll(x)
  39. #define pthread_t DWORD
  40. #define pthread_self GetCurrentThreadId
  41. #endif
  42. #define WIN32_EXPORT __declspec(dllexport)
  43.  
  44. inline void close(SOCKET fd) {closesocket(fd);}
  45. inline int setsockopt(SOCKET fd, int level, int optname, const void *optval, socklen_t optlen) {
  46.     return setsockopt(fd, level, optname, (const char *) optval, optlen);
  47. }
  48.  
  49. inline SOCKET dup(SOCKET socket) {
  50.     WSAPROTOCOL_INFOW pi;
  51.     if (WSADuplicateSocketW(socket, GetCurrentProcessId(), &pi) == SOCKET_ERROR) {
  52.         return INVALID_SOCKET;
  53.     }
  54.     return WSASocketW(pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
  55. }
  56. #else
  57. #include <sys/socket.h>
  58. #include <netinet/in.h>
  59. #include <netinet/tcp.h>
  60. #include <netdb.h>
  61. #include <unistd.h>
  62. #include <arpa/inet.h>
  63. #include <cstring>
  64. #define SOCKET_ERROR -1
  65. #define INVALID_SOCKET -1
  66. #define WIN32_EXPORT
  67. #endif
  68.  
  69. #include "Backend.h"
  70. #include <openssl/ssl.h>
  71. #include <csignal>
  72. #include <vector>
  73. #include <string>
  74. #include <mutex>
  75. #include <algorithm>
  76. #include <memory>
  77.  
  78. namespace uS {
  79.  
  80. // todo: mark sockets nonblocking in these functions
  81. // todo: probably merge this Context with the TLS::Context for same interface for SSL and non-SSL!
  82. struct Context {
  83.  
  84. #ifdef USE_MTCP
  85.     mtcp_context *mctx;
  86. #endif
  87.  
  88.     Context() {
  89.         // mtcp_create_context
  90. #ifdef USE_MTCP
  91.         mctx = mtcp_create_context(0); // cpu index?
  92. #endif
  93.     }
  94.  
  95.     ~Context() {
  96. #ifdef USE_MTCP
  97.         mtcp_destroy_context(mctx);
  98. #endif
  99.     }
  100.  
  101.     // returns INVALID_SOCKET on error
  102.     uv_os_sock_t acceptSocket(uv_os_sock_t fd) {
  103.         uv_os_sock_t acceptedFd;
  104. #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
  105.         // Linux, FreeBSD
  106.         acceptedFd = accept4(fd, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
  107. #else
  108.         // Windows, OS X
  109.         acceptedFd = accept(fd, nullptr, nullptr);
  110. #endif
  111.  
  112. #ifdef __APPLE__
  113.         if (acceptedFd != INVALID_SOCKET) {
  114.             int noSigpipe = 1;
  115.             setsockopt(acceptedFd, SOL_SOCKET, SO_NOSIGPIPE, &noSigpipe, sizeof(int));
  116.         }
  117. #endif
  118.         return acceptedFd;
  119.     }
  120.  
  121.     // returns INVALID_SOCKET on error
  122.     uv_os_sock_t createSocket(int domain, int type, int protocol) {
  123.         int flags = 0;
  124. #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
  125.         flags = SOCK_CLOEXEC | SOCK_NONBLOCK;
  126. #endif
  127.  
  128.         uv_os_sock_t createdFd = socket(domain, type | flags, protocol);
  129.  
  130. #ifdef __APPLE__
  131.         if (createdFd != INVALID_SOCKET) {
  132.             int noSigpipe = 1;
  133.             setsockopt(createdFd, SOL_SOCKET, SO_NOSIGPIPE, &noSigpipe, sizeof(int));
  134.         }
  135. #endif
  136.  
  137.         return createdFd;
  138.     }
  139.  
  140.     void closeSocket(uv_os_sock_t fd) {
  141. #ifdef _WIN32
  142.         closesocket(fd);
  143. #else
  144.         close(fd);
  145. #endif
  146.     }
  147.  
  148.     bool wouldBlock() {
  149. #ifdef _WIN32
  150.         return WSAGetLastError() == WSAEWOULDBLOCK;
  151. #else
  152.         return errno == EWOULDBLOCK;// || errno == EAGAIN;
  153. #endif
  154.     }
  155. };
  156.  
  157. namespace TLS {
  158.  
  159. class WIN32_EXPORT Context {
  160. private:
  161.     SSL_CTX *context = nullptr;
  162.     std::shared_ptr<std::string> password;
  163.  
  164.     static int passwordCallback(char *buf, int size, int rwflag, void *u)
  165.     {
  166.         std::string *password = (std::string *) u;
  167.         int length = std::min<int>(size, password->length());
  168.         memcpy(buf, password->data(), length);
  169.         buf[length] = '\0';
  170.         return length;
  171.     }
  172.  
  173. public:
  174.     friend Context WIN32_EXPORT createContext(std::string certChainFileName, std::string keyFileName, std::string keyFilePassword);
  175.     Context(SSL_CTX *context) : context(context) {
  176.  
  177.     }
  178.  
  179.     Context() = default;
  180.     Context(const Context &other);
  181.     Context &operator=(const Context &other);
  182.     ~Context();
  183.     operator bool() {
  184.         return context;
  185.     }
  186.  
  187.     SSL_CTX *getNativeContext() {
  188.         return context;
  189.     }
  190. };
  191.  
  192. Context WIN32_EXPORT createContext(std::string certChainFileName, std::string keyFileName, std::string keyFilePassword = std::string());
  193.  
  194. }
  195.  
  196. struct Socket;
  197.  
  198. // NodeData is like a Context, maybe merge them?
  199. struct WIN32_EXPORT NodeData {
  200.     char *recvBufferMemoryBlock;
  201.     char *recvBuffer;
  202.     int recvLength;
  203.     Loop *loop;
  204.     uS::Context *netContext;
  205.     void *user = nullptr;
  206.     static const int preAllocMaxSize = 1024;
  207.     char **preAlloc;
  208.     SSL_CTX *clientContext;
  209.  
  210.     Async *async = nullptr;
  211.     pthread_t tid;
  212.  
  213.     std::recursive_mutex *asyncMutex;
  214.     std::vector<Poll *> transferQueue;
  215.     std::vector<Poll *> changePollQueue;
  216.     static void asyncCallback(Async *async);
  217.  
  218.     static int getMemoryBlockIndex(size_t length) {
  219.         return (length >> 4) + bool(length & 15);
  220.     }
  221.  
  222.     char *getSmallMemoryBlock(int index) {
  223.         if (preAlloc[index]) {
  224.             char *memory = preAlloc[index];
  225.             preAlloc[index] = nullptr;
  226.             return memory;
  227.         } else {
  228.             return new char[index << 4];
  229.         }
  230.     }
  231.  
  232.     void freeSmallMemoryBlock(char *memory, int index) {
  233.         if (!preAlloc[index]) {
  234.             preAlloc[index] = memory;
  235.         } else {
  236.             delete [] memory;
  237.         }
  238.     }
  239.  
  240. public:
  241.     void addAsync() {
  242.         async = new Async(loop);
  243.         async->setData(this);
  244.         async->start(NodeData::asyncCallback);
  245.     }
  246.  
  247.     void clearPendingPollChanges(Poll *p) {
  248.         asyncMutex->lock();
  249.         changePollQueue.erase(
  250.             std::remove(changePollQueue.begin(), changePollQueue.end(), p),
  251.             changePollQueue.end()
  252.         );
  253.         asyncMutex->unlock();
  254.     }
  255. };
  256.  
  257. }
  258.  
  259. #endif // NETWORKING_UWS_H
  260.  
downloadNetworking.h Source code - Download uWebSockets Source code
Related Source Codes/Software:
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11

 Back to top