|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <thread> |
| 4 | +#include <atomic> |
| 5 | +#include <unordered_map> |
| 6 | +#include <vector> |
| 7 | +#include <mutex> |
| 8 | + |
| 9 | +#include <WinSock2.h> |
| 10 | + |
| 11 | +#define MAX_BUFF_SIZE __CPP_REDIS_READ_SIZE |
| 12 | +#define MAX_WORKER_THREADS 16 |
| 13 | + |
| 14 | +namespace cpp_redis { |
| 15 | + |
| 16 | +namespace network { |
| 17 | + |
| 18 | +typedef enum _enIoOperation { |
| 19 | + //IO_OP_ACCEPT, |
| 20 | + IO_OP_READ, |
| 21 | + IO_OP_WRITE |
| 22 | + } enIoOperation; |
| 23 | + |
| 24 | + |
| 25 | +class io_service { |
| 26 | +public: |
| 27 | + //! instance getter (singleton pattern) |
| 28 | + static const std::shared_ptr<io_service>& get_instance(void); |
| 29 | + io_service(size_t max_worker_threads = MAX_WORKER_THREADS); |
| 30 | + ~io_service(void); |
| 31 | + |
| 32 | + void shutdown(); |
| 33 | + |
| 34 | +private: |
| 35 | + //! copy ctor & assignment operator |
| 36 | + io_service(const io_service&) = delete; |
| 37 | + io_service& operator=(const io_service&) = delete; |
| 38 | + |
| 39 | +public: |
| 40 | + //! disconnection handler declaration |
| 41 | + typedef std::function<void(io_service&)> disconnection_handler_t; |
| 42 | + |
| 43 | + //! add or remove a given socket from the io service |
| 44 | + //! untrack should never be called from inside a callback |
| 45 | + void track(SOCKET sock, const disconnection_handler_t& handler); |
| 46 | + void untrack(SOCKET sock); |
| 47 | + |
| 48 | + //! asynchronously read read_size bytes and append them to the given buffer |
| 49 | + //! on completion, call the read_callback to notify of the success or failure of the operation |
| 50 | + //! return false if another async_read operation is in progress or socket is not registered |
| 51 | + typedef std::function<void(std::size_t)> read_callback_t; |
| 52 | + bool async_read(SOCKET socket, std::vector<char>& buffer, std::size_t read_size, const read_callback_t& callback); |
| 53 | + |
| 54 | + //! asynchronously write write_size bytes from buffer to the specified fd |
| 55 | + //!on completion, call the write_callback to notify of the success or failure of the operation |
| 56 | + //! return false if another async_write operation is in progress or socket is not registered |
| 57 | + typedef std::function<void(std::size_t)> write_callback_t; |
| 58 | + bool async_write(SOCKET socket, const std::vector<char>& buffer, std::size_t write_size, const write_callback_t& callback); |
| 59 | + |
| 60 | +private: |
| 61 | + |
| 62 | + struct io_context_info : OVERLAPPED { |
| 63 | + WSAOVERLAPPED overlapped; |
| 64 | + enIoOperation eOperation; |
| 65 | + }; |
| 66 | + |
| 67 | + //! simple struct to keep track of ongoing operations on a given sockeet |
| 68 | + class sock_info |
| 69 | + { |
| 70 | + public: |
| 71 | + sock_info(void) = default; |
| 72 | + virtual ~sock_info(void) |
| 73 | + { |
| 74 | + std::lock_guard<std::recursive_mutex> socklock(sock_info_mutex); |
| 75 | + for (auto it = io_contexts_pool.begin(); it != io_contexts_pool.end(); it++) |
| 76 | + delete *it; |
| 77 | + |
| 78 | + io_contexts_pool.clear(); |
| 79 | + } |
| 80 | + |
| 81 | + SOCKET hsock; |
| 82 | + std::size_t sent_bytes; |
| 83 | + |
| 84 | + //Must protect the members of our structure from access by multiple threads during IO Completion |
| 85 | + std::recursive_mutex sock_info_mutex; |
| 86 | + |
| 87 | + //We keep a simple vector of io_context_info structs to reuse for overlapped WSARecv and WSASend operations |
| 88 | + //Since each must have its OWN struct if we issue them at the same time. |
| 89 | + //othewise things get tangled up and borked. |
| 90 | + std::vector<io_context_info*> io_contexts_pool; |
| 91 | + |
| 92 | + disconnection_handler_t disconnection_handler; |
| 93 | + |
| 94 | + std::vector<char>* read_buffer; |
| 95 | + read_callback_t read_callback; |
| 96 | + |
| 97 | + std::vector<char> write_buffer; |
| 98 | + std::size_t write_size; |
| 99 | + write_callback_t write_callback; |
| 100 | + |
| 101 | + io_context_info* get_pool_io_context() { |
| 102 | + io_context_info* pInfo = NULL; |
| 103 | + std::lock_guard<std::recursive_mutex> socklock(sock_info_mutex); |
| 104 | + if (!io_contexts_pool.empty()) |
| 105 | + { |
| 106 | + pInfo = io_contexts_pool.back(); |
| 107 | + io_contexts_pool.pop_back(); |
| 108 | + } |
| 109 | + if (!pInfo) |
| 110 | + pInfo = new io_context_info(); |
| 111 | + //MUST clear the overlapped structure between IO calls! |
| 112 | + memset(&pInfo->overlapped, 0, sizeof(OVERLAPPED)); |
| 113 | + return pInfo; |
| 114 | + } |
| 115 | + |
| 116 | + void return_pool_io_context(io_context_info* p_io) { |
| 117 | + std::lock_guard<std::recursive_mutex> socklock(sock_info_mutex); |
| 118 | + io_contexts_pool.push_back(p_io); |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | +typedef std::function<void()> callback_t; |
| 123 | + |
| 124 | + //! wait for incoming events and notify |
| 125 | + int process_io(void); |
| 126 | + |
| 127 | + HANDLE m_completion_port; |
| 128 | + unsigned int m_worker_thread_pool_size; |
| 129 | + std::vector<std::thread> m_worker_threads; //vector containing all the threads we start to service our i/o requests |
| 130 | + |
| 131 | +private: |
| 132 | + //! whether the worker should terminate or not |
| 133 | + std::atomic_bool m_should_stop; |
| 134 | + |
| 135 | + //! tracked sockets |
| 136 | + std::unordered_map<SOCKET, sock_info> m_sockets; |
| 137 | + |
| 138 | + //! mutex to protect m_notify_socket access against race condition |
| 139 | + //! |
| 140 | + //! specific mutex for untrack: we dont want someone to untrack a socket while we process it |
| 141 | + //! this behavior could cause some issues when executing callbacks in another thread |
| 142 | + //! for example, obj is destroyed, in its dtor it untracks the socket, but at the same time |
| 143 | + //! a callback is executed from within another thread: the untrack mutex avoid this without being costly |
| 144 | + std::recursive_mutex m_socket_mutex; |
| 145 | +}; |
| 146 | + |
| 147 | +} //! network |
| 148 | + |
| 149 | +} //! cpp_redis |
0 commit comments