├── .editorconfig ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── include ├── uv └── uv++ │ ├── Async.hpp │ ├── Check.hpp │ ├── Connect.hpp │ ├── DynamicLibrary.hpp │ ├── Error.hpp │ ├── Exception.hpp │ ├── FileHandle.hpp │ ├── FileStream.hpp │ ├── FileStreamEvent.hpp │ ├── FileStreamHandle.hpp │ ├── FileStreamPoll.hpp │ ├── GetAddrInfo.hpp │ ├── Handle.hpp │ ├── Idle.hpp │ ├── Loop.hpp │ ├── Noncopyable.hpp │ ├── Pipe.hpp │ ├── Poll.hpp │ ├── Prepare.hpp │ ├── Req.hpp │ ├── Shutdown.hpp │ ├── Signal.hpp │ ├── Stream.hpp │ ├── Tcp.hpp │ ├── Timer.hpp │ ├── Tty.hpp │ ├── Udp.hpp │ ├── UdpSend.hpp │ ├── Utility.hpp │ ├── Work.hpp │ ├── Write.hpp │ ├── thread │ ├── Barrier.hpp │ ├── Cond.hpp │ ├── Mutex.hpp │ ├── Rwlock.hpp │ └── Sem.hpp │ └── uv.hpp └── samples ├── CMakeLists.txt ├── daytimeclient.cpp ├── interrupt_signal.cpp └── timer.cpp /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | # all files 5 | [*] 6 | indent_style = tab 7 | indent_size = 4 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | *.smod 19 | 20 | # Compiled Static libraries 21 | *.lai 22 | *.la 23 | *.a 24 | *.lib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(uv) 3 | 4 | if (UNIX) 5 | install (FILES include/uv DESTINATION /usr/local/include) 6 | install (DIRECTORY include/uv++ DESTINATION /usr/local/include) 7 | endif (UNIX) 8 | 9 | add_subdirectory(samples) 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 feihongmeilian 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # uv 2 | libuv的简单封装 3 | 4 | linux:
5 |     mkdir build
6 |     cd build
7 |     cmake ..
8 |     sudo make install
9 | 10 | windows:
11 |      直接使用 12 | 13 | good luck 14 | -------------------------------------------------------------------------------- /include/uv: -------------------------------------------------------------------------------- 1 | #ifndef UV_UV 2 | #define UV_UV 3 | 4 | #include "uv++/uv.hpp" 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /include/uv++/Async.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_ASYNC_HPP 2 | #define UV_ASYNC_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "Loop.hpp" 11 | #include "Handle.hpp" 12 | 13 | namespace uv 14 | { 15 | class Async : public Handle 16 | { 17 | public: 18 | Async(); 19 | 20 | void init(uv::Loop &loop, const std::function &handler, std::error_code &ec); 21 | void init(uv::Loop &loop, const std::function &handler); 22 | void send(std::error_code &ec); 23 | void send(); 24 | 25 | private: 26 | std::function callbackHandler_ = []() {}; 27 | }; 28 | 29 | 30 | 31 | 32 | 33 | inline Async::Async() 34 | { 35 | handle_.data = this; 36 | } 37 | 38 | inline void Async::init(uv::Loop &loop, const std::function &handler, std::error_code & ec) 39 | { 40 | callbackHandler_ = handler; 41 | auto status = uv_async_init(loop.value(), &handle_, [](uv_async_t *a) { 42 | auto &async = *reinterpret_cast(a->data); 43 | async.callbackHandler_(); 44 | }); 45 | 46 | if (status != 0) { 47 | ec = makeErrorCode(status); 48 | } 49 | } 50 | 51 | inline void Async::init(uv::Loop &loop, const std::function &handler) 52 | { 53 | std::error_code ec; 54 | 55 | init(loop, handler, ec); 56 | if (ec) { 57 | throw uv::Exception(ec); 58 | } 59 | } 60 | 61 | inline void Async::send(std::error_code &ec) 62 | { 63 | const auto status = uv_async_send(&handle_); 64 | 65 | if (status != 0) { 66 | ec = makeErrorCode(status); 67 | } 68 | } 69 | 70 | inline void Async::send() 71 | { 72 | std::error_code ec; 73 | 74 | send(ec); 75 | if (ec) { 76 | throw uv::Exception(ec); 77 | } 78 | } 79 | } 80 | 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /include/uv++/Check.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_CHECK_HPP 2 | #define UV_CHECK_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "Loop.hpp" 11 | #include "Handle.hpp" 12 | 13 | namespace uv 14 | { 15 | class Check : public Handle 16 | { 17 | public: 18 | Check(); 19 | 20 | void init(uv::Loop &loop, std::error_code &ec); 21 | void init(uv::Loop &loop); 22 | void start(const std::function &handler, std::error_code &ec); 23 | void start(const std::function &handler); 24 | void stop(std::error_code &ec); 25 | void stop(); 26 | 27 | private: 28 | std::function startHandler_ = []() {}; 29 | }; 30 | 31 | 32 | 33 | 34 | 35 | inline Check::Check() 36 | { 37 | handle_.data = this; 38 | } 39 | 40 | inline void Check::init(uv::Loop &loop, std::error_code &ec) 41 | { 42 | auto status = uv_check_init(loop.value(), &handle_); 43 | 44 | if (status != 0) { 45 | ec = makeErrorCode(status); 46 | } 47 | } 48 | 49 | inline void Check::init(uv::Loop &loop) 50 | { 51 | std::error_code ec; 52 | 53 | init(loop, ec); 54 | if (ec) { 55 | throw uv::Exception(ec); 56 | } 57 | } 58 | 59 | inline void Check::start(const std::function &handler, std::error_code &ec) 60 | { 61 | startHandler_ = handler; 62 | auto status = uv_check_start(&handle_, [](uv_check_t *handle) { 63 | auto &check = *reinterpret_cast(handle->data); 64 | check.startHandler_(); 65 | }); 66 | 67 | if (status != 0) { 68 | ec = makeErrorCode(status); 69 | } 70 | } 71 | 72 | inline void Check::start(const std::function &handler) 73 | { 74 | std::error_code ec; 75 | 76 | start(handler, ec); 77 | if (ec) { 78 | throw uv::Exception(ec); 79 | } 80 | } 81 | 82 | inline void Check::stop(std::error_code &ec) 83 | { 84 | auto status = uv_check_stop(&handle_); 85 | 86 | if (status != 0) { 87 | ec = makeErrorCode(status); 88 | } 89 | } 90 | 91 | inline void Check::stop() 92 | { 93 | std::error_code ec; 94 | 95 | stop(ec); 96 | if (ec) { 97 | throw uv::Exception(ec); 98 | } 99 | } 100 | } 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /include/uv++/Connect.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_CONNECT_HPP 2 | #define UV_CONNECT_HPP 3 | 4 | #include 5 | 6 | #include "Req.hpp" 7 | 8 | namespace uv 9 | { 10 | class Tcp; 11 | class Pipe; 12 | 13 | class Connect : public Req 14 | { 15 | public: 16 | Connect(); 17 | 18 | private: 19 | friend class Tcp; 20 | friend class Pipe; 21 | }; 22 | 23 | 24 | 25 | 26 | 27 | inline Connect::Connect() 28 | { 29 | handle_.data = this; 30 | } 31 | } 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/uv++/DynamicLibrary.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_DYNAMIC_LIBRARY_HPP 2 | #define UV_DYNAMIC_LIBRARY_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | #include "Exception.hpp" 11 | 12 | namespace uv 13 | { 14 | class DynamicLibrary 15 | { 16 | public: 17 | void open(const std::string &filename, std::error_code &ec); 18 | void open(const std::string &filename); 19 | void sym(const std::string &name, void** ptr, std::error_code &ec); 20 | void sym(const std::string &name, void** ptr); 21 | void close(); 22 | 23 | private: 24 | uv_lib_t lib_; 25 | }; 26 | 27 | 28 | 29 | 30 | 31 | inline void DynamicLibrary::open(const std::string &filename, std::error_code &ec) 32 | { 33 | auto status = uv_dlopen(filename.c_str(), &lib_); 34 | 35 | if (status != 0) { 36 | ec = makeErrorCode(status); 37 | } 38 | } 39 | 40 | inline void DynamicLibrary::open(const std::string &filename) 41 | { 42 | std::error_code ec; 43 | 44 | open(filename, ec); 45 | if (ec) { 46 | throw uv::Exception(uv_dlerror(&lib_)); 47 | } 48 | } 49 | 50 | inline void DynamicLibrary::sym(const std::string &name, void **ptr, std::error_code &ec) 51 | { 52 | auto status = uv_dlsym(&lib_, name.c_str(), ptr); 53 | 54 | if (status != 0) { 55 | ec = makeErrorCode(status); 56 | } 57 | } 58 | 59 | inline void DynamicLibrary::sym(const std::string &name, void **ptr) 60 | { 61 | std::error_code ec; 62 | 63 | sym(name, ptr, ec); 64 | if (ec) { 65 | throw uv::Exception(uv_dlerror(&lib_)); 66 | } 67 | } 68 | 69 | inline void DynamicLibrary::close() 70 | { 71 | uv_dlclose(&lib_); 72 | } 73 | 74 | 75 | 76 | } 77 | #endif 78 | -------------------------------------------------------------------------------- /include/uv++/Error.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_ERROR_HPP 2 | #define UV_ERROR_HPP 3 | 4 | #include 5 | #include 6 | 7 | namespace uv 8 | { 9 | class Category : public std::error_category 10 | { 11 | public: 12 | Category() = default; 13 | 14 | char const *name() const noexcept { 15 | return "uv"; 16 | } 17 | 18 | std::string message(int value) const { 19 | return uv_strerror(value); 20 | } 21 | }; 22 | 23 | inline const std::error_category& getCategory() { 24 | static Category instance; 25 | return instance; 26 | } 27 | 28 | inline std::error_code makeErrorCode(int e) { 29 | return std::error_code(e, getCategory()); 30 | } 31 | } 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/uv++/Exception.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_EXCEPTION_HPP 2 | #define UV_EXCEPTION_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | 11 | namespace uv 12 | { 13 | class Exception : public std::exception 14 | { 15 | public: 16 | Exception(const std::string &msg, std::error_code ec = makeErrorCode(0)) 17 | : msg_(msg.empty() ? ec.message() : msg), code_(ec) 18 | {} 19 | 20 | explicit Exception(std::error_code ec) 21 | : msg_(ec.message()), code_(ec) 22 | {} 23 | 24 | ~Exception() throw() {} 25 | 26 | virtual char const * what() const throw() 27 | { 28 | return msg_.c_str(); 29 | } 30 | 31 | std::error_code code() const throw() 32 | { 33 | return code_; 34 | } 35 | 36 | const std::string msg_; 37 | std::error_code code_; 38 | }; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /include/uv++/FileHandle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_FILE_HANDLE_HPP 2 | #define UV_FILE_HANDLE_HPP 3 | 4 | #include 5 | 6 | #include "Error.hpp" 7 | #include "Exception.hpp" 8 | #include "Handle.hpp" 9 | 10 | namespace uv 11 | { 12 | template 13 | class FileHandle : public Handle 14 | { 15 | public: 16 | void fileno(uv_os_fd_t &fd, std::error_code &ec); 17 | void fileno(uv_os_fd_t &fd); 18 | 19 | protected: 20 | using Handle::handle_; 21 | }; 22 | 23 | 24 | 25 | 26 | template 27 | inline void FileHandle::fileno(uv_os_fd_t &fd, std::error_code &ec) 28 | { 29 | auto status = uv_fileno(reinterpret_cast(&handle_), &fd); 30 | 31 | if (status != 0) { 32 | ec = makeErrorCode(status); 33 | } 34 | } 35 | 36 | template 37 | inline void FileHandle::fileno(uv_os_fd_t &fd) 38 | { 39 | std::error_code ec; 40 | 41 | fileno(fd, ec); 42 | if (ec) { 43 | throw uv::Exception(ec); 44 | } 45 | } 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /include/uv++/FileStream.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_FILE_STREAM_HPP 2 | #define UV_FILE_STREAM_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Req.hpp" 10 | #include "Loop.hpp" 11 | 12 | namespace uv 13 | { 14 | class FileStream : public Req 15 | { 16 | public: 17 | explicit FileStream(uv::Loop &loop); 18 | 19 | void reqCleanup(); 20 | int close(uv_file file, const std::function &handler); 21 | int open(const std::string &path, int flags, int mode, const std::function &handler); 22 | int read(uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, const std::function &handler); 23 | int write(uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, const std::function &handler); 24 | int unlink(const std::string &path, const std::function &handler); 25 | int mkdir(const std::string &path, int mode, const std::function &handler); 26 | int mkdtemp(const std::string &tpl, const std::function &handler); 27 | int rmdir(const std::string &path, const std::function &handler); 28 | int scandir(const std::string &path, int flags, const std::function &handler); 29 | int scandirNext(uv_dirent_t* ent); 30 | int stat(const std::string &path, const std::function &handler); 31 | int fstat(uv_file file,const std::function &handler); 32 | int rename(const std::string &path, const std::string &new_path, const std::function &handler); 33 | int fsync(uv_file file, const std::function &handler); 34 | int fdatasync(uv_file file, const std::function &handler); 35 | int ftruncate(uv_file file, int64_t offset, const std::function &handler); 36 | int sendfile(uv_file out_fd, uv_file in_fd, int64_t in_offset, size_t length, const std::function &handler); 37 | int access(const std::string &path, int mode, const std::function &handler); 38 | int chmod(const std::string &path, int mode, const std::function &handler); 39 | int utime(const std::string &path, double atime, double mtime, const std::function &handler); 40 | int futime(uv_file file, double atime, double mtime, const std::function &handler); 41 | int lstat(const std::string &path, const std::function &handler); 42 | int link(const std::string &path, const std::string &newPath, const std::function &handler); 43 | int symlink(const std::string &path, const std::string &newPath, int flags, const std::function &handler); 44 | int readlink(const std::string &path, const std::function &handler); 45 | int realpath(const std::string &path, const std::function &handler); 46 | int fchmod(uv_file file, int mode, const std::function &handler); 47 | int chown(const std::string &path, uv_uid_t uid, uv_gid_t gid, const std::function &handler); 48 | int fchown(uv_file file, uv_uid_t uid, uv_gid_t gid, const std::function &handler); 49 | private: 50 | uv::Loop &loop_; 51 | std::function callbackHandler_ = []() {}; 52 | }; 53 | 54 | 55 | 56 | 57 | 58 | inline FileStream::FileStream(uv::Loop &loop) 59 | : loop_(loop) 60 | { 61 | handle_.data = this; 62 | } 63 | 64 | inline void FileStream::reqCleanup() 65 | { 66 | uv_fs_req_cleanup(&handle_); 67 | } 68 | 69 | inline int FileStream::close(uv_file file, const std::function &handler) 70 | { 71 | callbackHandler_ = handler; 72 | return uv_fs_close(loop_.value(), &handle_, file, [](uv_fs_t *req) { 73 | auto &fs = *reinterpret_cast(req->data); 74 | fs.callbackHandler_(); 75 | }); 76 | } 77 | 78 | inline int FileStream::open(const std::string &path, int flags, int mode, const std::function &handler) 79 | { 80 | callbackHandler_ = handler; 81 | return uv_fs_open(loop_.value(), &handle_, path.c_str(), flags, mode, [](uv_fs_t *req) { 82 | auto &fs = *reinterpret_cast(req->data); 83 | fs.callbackHandler_(); 84 | }); 85 | } 86 | 87 | inline int FileStream::read(uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, const std::function &handler) 88 | { 89 | callbackHandler_ = handler; 90 | return uv_fs_read(loop_.value(), &handle_, file, bufs, nbufs, offset, [](uv_fs_t *req) { 91 | auto &fs = *reinterpret_cast(req->data); 92 | fs.callbackHandler_(); 93 | }); 94 | } 95 | 96 | inline int FileStream::write(uv_file file, const uv_buf_t bufs[], unsigned int nbufs, int64_t offset, const std::function &handler) 97 | { 98 | callbackHandler_ = handler; 99 | return uv_fs_write(loop_.value(), &handle_, file, bufs, nbufs, offset, [](uv_fs_t *req) { 100 | auto &fs = *reinterpret_cast(req->data); 101 | fs.callbackHandler_(); 102 | }); 103 | } 104 | 105 | inline int FileStream::unlink(const std::string &path, const std::function &handler) 106 | { 107 | callbackHandler_ = handler; 108 | return uv_fs_unlink(loop_.value(), &handle_, path.c_str(), [](uv_fs_t *req) { 109 | auto &fs = *reinterpret_cast(req->data); 110 | fs.callbackHandler_(); 111 | }); 112 | } 113 | 114 | inline int FileStream::mkdir(const std::string &path, int mode, const std::function &handler) 115 | { 116 | callbackHandler_ = handler; 117 | return uv_fs_mkdir(loop_.value(), &handle_, path.c_str(), mode, [](uv_fs_t *req) { 118 | auto &fs = *reinterpret_cast(req->data); 119 | fs.callbackHandler_(); 120 | }); 121 | } 122 | 123 | inline int FileStream::mkdtemp(const std::string &tpl, const std::function &handler) 124 | { 125 | callbackHandler_ = handler; 126 | return uv_fs_mkdtemp(loop_.value(), &handle_, tpl.c_str(), [](uv_fs_t *req) { 127 | auto &fs = *reinterpret_cast(req->data); 128 | fs.callbackHandler_(); 129 | }); 130 | } 131 | 132 | inline int FileStream::rmdir(const std::string &path, const std::function &handler) 133 | { 134 | callbackHandler_ = handler; 135 | return uv_fs_rmdir(loop_.value(), &handle_, path.c_str(), [](uv_fs_t *req) { 136 | auto &fs = *reinterpret_cast(req->data); 137 | fs.callbackHandler_(); 138 | }); 139 | } 140 | 141 | inline int FileStream::scandir(const std::string &path, int flags, const std::function &handler) 142 | { 143 | callbackHandler_ = handler; 144 | return uv_fs_scandir(loop_.value(), &handle_, path.c_str(), flags, [](uv_fs_t *req) { 145 | auto &fs = *reinterpret_cast(req->data); 146 | fs.callbackHandler_(); 147 | }); 148 | } 149 | 150 | inline int FileStream::scandirNext(uv_dirent_t *ent) 151 | { 152 | return uv_fs_scandir_next(&handle_, ent); 153 | } 154 | 155 | inline int FileStream::stat(const std::string &path, const std::function &handler) 156 | { 157 | callbackHandler_ = handler; 158 | return uv_fs_stat(loop_.value(), &handle_, path.c_str(), [](uv_fs_t *req) { 159 | auto &fs = *reinterpret_cast(req->data); 160 | fs.callbackHandler_(); 161 | }); 162 | } 163 | 164 | inline int FileStream::fstat(uv_file file, const std::function &handler) 165 | { 166 | callbackHandler_ = handler; 167 | return uv_fs_fstat(loop_.value(), &handle_, file, [](uv_fs_t *req) { 168 | auto &fs = *reinterpret_cast(req->data); 169 | fs.callbackHandler_(); 170 | }); 171 | } 172 | 173 | inline int FileStream::rename(const std::string &path, const std::string &new_path, const std::function &handler) 174 | { 175 | callbackHandler_ = handler; 176 | return uv_fs_rename(loop_.value(), &handle_, path.c_str(), new_path.c_str(), [](uv_fs_t *req) { 177 | auto &fs = *reinterpret_cast(req->data); 178 | fs.callbackHandler_(); 179 | }); 180 | } 181 | 182 | inline int FileStream::fsync(uv_file file, const std::function &handler) 183 | { 184 | callbackHandler_ = handler; 185 | return uv_fs_fsync(loop_.value(), &handle_, file, [](uv_fs_t *req) { 186 | auto &fs = *reinterpret_cast(req->data); 187 | fs.callbackHandler_(); 188 | }); 189 | } 190 | 191 | inline int FileStream::fdatasync(uv_file file, const std::function &handler) 192 | { 193 | callbackHandler_ = handler; 194 | return uv_fs_fdatasync(loop_.value(), &handle_, file, [](uv_fs_t *req) { 195 | auto &fs = *reinterpret_cast(req->data); 196 | fs.callbackHandler_(); 197 | }); 198 | } 199 | 200 | inline int FileStream::ftruncate(uv_file file, int64_t offset, const std::function &handler) 201 | { 202 | callbackHandler_ = handler; 203 | return uv_fs_ftruncate(loop_.value(), &handle_, file, offset, [](uv_fs_t *req) { 204 | auto &fs = *reinterpret_cast(req->data); 205 | fs.callbackHandler_(); 206 | }); 207 | } 208 | 209 | inline int FileStream::sendfile(uv_file out_fd, uv_file in_fd, int64_t in_offset, size_t length, const std::function &handler) 210 | { 211 | callbackHandler_ = handler; 212 | return uv_fs_sendfile(loop_.value(), &handle_, out_fd, in_fd, in_offset, length, [](uv_fs_t *req) { 213 | auto &fs = *reinterpret_cast(req->data); 214 | fs.callbackHandler_(); 215 | }); 216 | } 217 | 218 | inline int FileStream::access(const std::string &path, int mode, const std::function &handler) 219 | { 220 | callbackHandler_ = handler; 221 | return uv_fs_access(loop_.value(), &handle_, path.c_str(), mode, [](uv_fs_t *req) { 222 | auto &fs = *reinterpret_cast(req->data); 223 | fs.callbackHandler_(); 224 | }); 225 | } 226 | 227 | inline int FileStream::chmod(const std::string &path, int mode, const std::function &handler) 228 | { 229 | callbackHandler_ = handler; 230 | return uv_fs_chmod(loop_.value(), &handle_, path.c_str(), mode, [](uv_fs_t *req) { 231 | auto &fs = *reinterpret_cast(req->data); 232 | fs.callbackHandler_(); 233 | }); 234 | } 235 | 236 | inline int FileStream::utime(const std::string &path, double atime, double mtime, const std::function &handler) 237 | { 238 | callbackHandler_ = handler; 239 | return uv_fs_utime(loop_.value(), &handle_, path.c_str(), atime, mtime, [](uv_fs_t *req) { 240 | auto &fs = *reinterpret_cast(req->data); 241 | fs.callbackHandler_(); 242 | }); 243 | } 244 | 245 | inline int FileStream::futime(uv_file file, double atime, double mtime, const std::function &handler) 246 | { 247 | callbackHandler_ = handler; 248 | return uv_fs_futime(loop_.value(), &handle_, file, atime, mtime, [](uv_fs_t *req) { 249 | auto &fs = *reinterpret_cast(req->data); 250 | fs.callbackHandler_(); 251 | }); 252 | } 253 | 254 | inline int FileStream::lstat(const std::string &path, const std::function &handler) 255 | { 256 | callbackHandler_ = handler; 257 | return uv_fs_lstat(loop_.value(), &handle_, path.c_str(), [](uv_fs_t *req) { 258 | auto &fs = *reinterpret_cast(req->data); 259 | fs.callbackHandler_(); 260 | }); 261 | } 262 | 263 | inline int FileStream::link(const std::string &path, const std::string &newPath, const std::function &handler) 264 | { 265 | callbackHandler_ = handler; 266 | return uv_fs_link(loop_.value(), &handle_, path.c_str(), newPath.c_str(), [](uv_fs_t *req) { 267 | auto &fs = *reinterpret_cast(req->data); 268 | fs.callbackHandler_(); 269 | }); 270 | } 271 | 272 | inline int FileStream::symlink(const std::string &path, const std::string &newPath, int flags, const std::function &handler) 273 | { 274 | callbackHandler_ = handler; 275 | return uv_fs_symlink(loop_.value(), &handle_, path.c_str(), newPath.c_str(), flags, [](uv_fs_t *req) { 276 | auto &fs = *reinterpret_cast(req->data); 277 | fs.callbackHandler_(); 278 | }); 279 | } 280 | 281 | inline int FileStream::readlink(const std::string &path, const std::function &handler) 282 | { 283 | callbackHandler_ = handler; 284 | return uv_fs_readlink(loop_.value(), &handle_, path.c_str(), [](uv_fs_t *req) { 285 | auto &fs = *reinterpret_cast(req->data); 286 | fs.callbackHandler_(); 287 | }); 288 | } 289 | 290 | inline int FileStream::realpath(const std::string &path, const std::function &handler) 291 | { 292 | callbackHandler_ = handler; 293 | return uv_fs_realpath(loop_.value(), &handle_, path.c_str(),[](uv_fs_t *req) { 294 | auto &fs = *reinterpret_cast(req->data); 295 | fs.callbackHandler_(); 296 | }); 297 | } 298 | 299 | inline int FileStream::fchmod(uv_file file, int mode, const std::function &handler) 300 | { 301 | callbackHandler_ = handler; 302 | return uv_fs_fchmod(loop_.value(), &handle_, file, mode, [](uv_fs_t *req) { 303 | auto &fs = *reinterpret_cast(req->data); 304 | fs.callbackHandler_(); 305 | }); 306 | } 307 | 308 | inline int FileStream::chown(const std::string &path, uv_uid_t uid, uv_gid_t gid, const std::function &handler) 309 | { 310 | callbackHandler_ = handler; 311 | return uv_fs_chown(loop_.value(), &handle_, path.c_str(), uid, gid, [](uv_fs_t *req) { 312 | auto &fs = *reinterpret_cast(req->data); 313 | fs.callbackHandler_(); 314 | }); 315 | } 316 | 317 | inline int FileStream::fchown(uv_file file, uv_uid_t uid, uv_gid_t gid, const std::function &handler) 318 | { 319 | callbackHandler_ = handler; 320 | return uv_fs_fchown(loop_.value(), &handle_, file, uid, gid, [](uv_fs_t *req) { 321 | auto &fs = *reinterpret_cast(req->data); 322 | fs.callbackHandler_(); 323 | }); 324 | } 325 | 326 | } 327 | 328 | 329 | #endif 330 | -------------------------------------------------------------------------------- /include/uv++/FileStreamEvent.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_FILE_STREAM_EVENT_HPP 2 | #define UV_FILE_STREAM_EVENT_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | #include "Exception.hpp" 11 | #include "Loop.hpp" 12 | #include "Handle.hpp" 13 | 14 | namespace uv 15 | { 16 | class FileStreamEvent : public Handle 17 | { 18 | public: 19 | FileStreamEvent(); 20 | 21 | void init(uv::Loop &loop, std::error_code &ec); 22 | void init(uv::Loop &loop); 23 | void start(const std::string &path, unsigned int flags, 24 | const std::function &handler, std::error_code &ec); 25 | void start(const std::string &path, unsigned int flags, 26 | const std::function &handler); 27 | void stop(std::error_code &ec); 28 | void stop(); 29 | void getpath(char *buffer, size_t &size, std::error_code &ecr); 30 | void getpath(char *buffer, size_t &size); 31 | 32 | private: 33 | std::function callbackHandler_ 34 | = [](const std::string &filename, int events, const std::error_code &ec) {}; 35 | }; 36 | 37 | 38 | 39 | 40 | 41 | inline FileStreamEvent::FileStreamEvent() 42 | { 43 | handle_.data = this; 44 | } 45 | 46 | inline void FileStreamEvent::init(uv::Loop &loop, std::error_code &ec) 47 | { 48 | auto status = uv_fs_event_init(loop.value(), &handle_); 49 | 50 | if (status != 0) { 51 | ec = makeErrorCode(status); 52 | } 53 | } 54 | 55 | inline void FileStreamEvent::init(uv::Loop &loop) 56 | { 57 | std::error_code ec; 58 | 59 | init(loop, ec); 60 | if (ec) { 61 | throw uv::Exception(ec); 62 | } 63 | } 64 | 65 | inline void FileStreamEvent::start(const std::string &path, unsigned int flags, 66 | const std::function &handler, std::error_code &ec) 67 | { 68 | callbackHandler_ = handler; 69 | auto status = uv_fs_event_start(&handle_, 70 | [](uv_fs_event_t* handle, const char *filename, int events, int status) { 71 | auto &fe = *reinterpret_cast(handle->data); 72 | fe.callbackHandler_(filename, events, makeErrorCode(status)); 73 | }, path.c_str(), flags); 74 | 75 | if (status != 0) { 76 | ec = makeErrorCode(status); 77 | } 78 | } 79 | 80 | inline void FileStreamEvent::start(const std::string &path, unsigned int flags, 81 | const std::function &handler) 82 | { 83 | std::error_code ec; 84 | 85 | start(path, flags, handler, ec); 86 | if (ec) { 87 | throw uv::Exception(ec); 88 | } 89 | } 90 | 91 | inline void FileStreamEvent::stop(std::error_code &ec) 92 | { 93 | auto status = uv_fs_event_stop(&handle_); 94 | 95 | if (status != 0) { 96 | ec = makeErrorCode(status); 97 | } 98 | } 99 | 100 | inline void FileStreamEvent::stop() 101 | { 102 | std::error_code ec; 103 | 104 | stop(ec); 105 | if (ec) { 106 | throw uv::Exception(ec); 107 | } 108 | } 109 | 110 | inline void FileStreamEvent::getpath(char *buffer, size_t &size, std::error_code &ec) 111 | { 112 | auto status = uv_fs_event_getpath(&handle_, buffer, &size); 113 | 114 | if (status != 0) { 115 | ec = makeErrorCode(status); 116 | } 117 | } 118 | 119 | inline void FileStreamEvent::getpath(char *buffer, size_t &size) 120 | { 121 | std::error_code ec; 122 | 123 | getpath(buffer, size, ec); 124 | if (ec) { 125 | throw uv::Exception(ec); 126 | } 127 | } 128 | } 129 | 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /include/uv++/FileStreamHandle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_FILE_STREAhandle__HPP 2 | #define UV_FILE_STREAhandle__HPP 3 | 4 | #include 5 | 6 | #include "Error.hpp" 7 | #include "Exception.hpp" 8 | #include "Stream.hpp" 9 | 10 | namespace uv 11 | { 12 | template 13 | class FileStreamHandle : public Stream 14 | { 15 | public: 16 | void sendBufferSize(int &value, std::error_code &ec); 17 | void sendBufferSize(int &value); 18 | void recvBufferSize(int &value, std::error_code &ec); 19 | void recvBufferSize(int &value); 20 | 21 | protected: 22 | using Handle::handle_; 23 | }; 24 | 25 | 26 | 27 | 28 | template 29 | inline void FileStreamHandle::sendBufferSize(int &value, std::error_code &ec) 30 | { 31 | auto status = uv_send_buffer_size(reinterpret_cast(&handle_), &value); 32 | 33 | if (status != 0) { 34 | ec = makeErrorCode(status); 35 | } 36 | } 37 | 38 | template 39 | inline void FileStreamHandle::sendBufferSize(int &value) 40 | { 41 | std::error_code ec; 42 | 43 | sendBufferSize(value, ec); 44 | if (ec) { 45 | throw uv::Exception(ec); 46 | } 47 | } 48 | 49 | template 50 | inline void FileStreamHandle::recvBufferSize(int &value, std::error_code &ec) 51 | { 52 | auto status = uv_recv_buffer_size(reinterpret_cast(&handle_), &value); 53 | 54 | if (status != 0) { 55 | ec = makeErrorCode(status); 56 | } 57 | } 58 | 59 | template 60 | inline void FileStreamHandle::recvBufferSize(int &value) 61 | { 62 | std::error_code ec; 63 | 64 | recvBufferSize(value, ec); 65 | if (ec) { 66 | throw uv::Exception(ec); 67 | } 68 | } 69 | 70 | } 71 | #endif 72 | -------------------------------------------------------------------------------- /include/uv++/FileStreamPoll.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_FILE_STREAM_POLL_HPP 2 | #define UV_FILE_STREAM_POLL_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | #include "Exception.hpp" 11 | #include "Loop.hpp" 12 | #include "Handle.hpp" 13 | 14 | namespace uv 15 | { 16 | class FileStreamPoll : public Handle 17 | { 18 | public: 19 | FileStreamPoll(); 20 | 21 | void init(uv::Loop &loop, std::error_code &ec); 22 | void init(uv::Loop &loop); 23 | void start(const std::string &path, unsigned int interval, 24 | const std::function &handler, std::error_code &ec); 25 | void start(const std::string &path, unsigned int interval, 26 | const std::function &handler); 27 | void stop(std::error_code &ec); 28 | void stop(); 29 | void getpath(char *buffer, size_t &size, std::error_code &ec); 30 | void getpath(char *buffer, size_t &size); 31 | 32 | //just use in callback function 33 | const uv_stat_t *getPrevStat() const; 34 | const uv_stat_t *getCurrStat() const; 35 | 36 | private: 37 | const uv_stat_t *prevStat_; 38 | const uv_stat_t *currStat_; 39 | std::function callbackHandler_ 40 | = [](const std::error_code &ec) {}; 41 | }; 42 | 43 | 44 | 45 | 46 | 47 | inline FileStreamPoll::FileStreamPoll() 48 | { 49 | handle_.data = this; 50 | } 51 | 52 | inline void FileStreamPoll::init(uv::Loop &loop, std::error_code &ec) 53 | { 54 | auto status = uv_fs_poll_init(loop.value(), &handle_); 55 | 56 | if (status != 0) { 57 | ec = makeErrorCode(status); 58 | } 59 | } 60 | 61 | inline void FileStreamPoll::init(uv::Loop &loop) 62 | { 63 | std::error_code ec; 64 | 65 | init(loop, ec); 66 | if (ec) { 67 | throw uv::Exception(ec); 68 | } 69 | } 70 | 71 | inline void FileStreamPoll::start(const std::string &path, unsigned int interval, 72 | const std::function &handler, std::error_code &ec) 73 | { 74 | callbackHandler_ = handler; 75 | auto status = uv_fs_poll_start(&handle_, 76 | [](uv_fs_poll_t* handle, int status, const uv_stat_t *prev, const uv_stat_t *curr) { 77 | auto &fp = *reinterpret_cast(handle->data); 78 | fp.prevStat_ = prev; 79 | fp.currStat_ = curr; 80 | fp.callbackHandler_(makeErrorCode(status)); 81 | }, path.c_str(), interval); 82 | 83 | if (status != 0) { 84 | ec = makeErrorCode(status); 85 | } 86 | } 87 | 88 | inline void FileStreamPoll::start(const std::string &path, unsigned int interval, 89 | const std::function &handler) 90 | { 91 | std::error_code ec; 92 | 93 | start(path, interval, handler, ec); 94 | if (ec) { 95 | throw uv::Exception(ec); 96 | } 97 | } 98 | 99 | inline void FileStreamPoll::stop(std::error_code &ec) 100 | { 101 | auto status = uv_fs_poll_stop(&handle_); 102 | 103 | if (status != 0) { 104 | ec = makeErrorCode(status); 105 | } 106 | } 107 | 108 | inline void FileStreamPoll::stop() 109 | { 110 | std::error_code ec; 111 | 112 | stop(ec); 113 | if (ec) { 114 | throw uv::Exception(ec); 115 | } 116 | } 117 | 118 | inline void FileStreamPoll::getpath(char *buffer, size_t &size, std::error_code &ec) 119 | { 120 | auto status = uv_fs_poll_getpath(&handle_, buffer, &size); 121 | 122 | if (status != 0) { 123 | ec = makeErrorCode(status); 124 | } 125 | } 126 | 127 | inline void FileStreamPoll::getpath(char *buffer, size_t &size) 128 | { 129 | std::error_code ec; 130 | 131 | getpath(buffer, size, ec); 132 | if (ec) { 133 | throw uv::Exception(ec); 134 | } 135 | } 136 | 137 | inline const uv_stat_t *FileStreamPoll::getPrevStat() const 138 | { 139 | return prevStat_; 140 | } 141 | 142 | inline const uv_stat_t *FileStreamPoll::getCurrStat() const 143 | { 144 | return currStat_; 145 | } 146 | } 147 | 148 | 149 | #endif 150 | -------------------------------------------------------------------------------- /include/uv++/GetAddrInfo.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_GET_ADDR_INFO_HPP 2 | #define UV_GET_ADDR_INFO_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | #include "Exception.hpp" 11 | #include "Loop.hpp" 12 | #include "Req.hpp" 13 | 14 | namespace uv 15 | { 16 | class GetAddrInfo : public Req 17 | { 18 | public: 19 | explicit GetAddrInfo(uv::Loop &loop); 20 | void get(const std::string &node, const std::string &service, const struct addrinfo &hints, 21 | const std::function &handler, std::error_code &ec); 22 | void get(const std::string &node, const std::string &service, const struct addrinfo &hints, 23 | const std::function &handler); 24 | 25 | static void freeaddrinfo(struct addrinfo *ai); 26 | private: 27 | uv::Loop &loop_; 28 | std::function callbackHandler_ 29 | = [] (const std::error_code &ec,struct addrinfo *) {}; 30 | }; 31 | 32 | 33 | 34 | 35 | 36 | inline GetAddrInfo::GetAddrInfo(uv::Loop &loop) 37 | : loop_(loop) 38 | { 39 | handle_.data = this; 40 | } 41 | 42 | inline void GetAddrInfo::get(const std::string &node, const std::string &service, const struct addrinfo &hints, 43 | const std::function &handler, std::error_code &ec) 44 | { 45 | callbackHandler_ = handler; 46 | auto status = uv_getaddrinfo(loop_.value(), &handle_, [](uv_getaddrinfo_t* req, int status, struct addrinfo *res) { 47 | auto &addr = *reinterpret_cast(req->data); 48 | addr.callbackHandler_(makeErrorCode(status), res); 49 | }, node.c_str(), service.c_str(), &hints); 50 | 51 | if (status != 0) { 52 | ec = makeErrorCode(status); 53 | } 54 | } 55 | 56 | inline void GetAddrInfo::get(const std::string &node, const std::string &service, const addrinfo &hints, 57 | const std::function &handler) 58 | { 59 | std::error_code ec; 60 | 61 | get(node, service, hints, handler, ec); 62 | if (ec) { 63 | throw uv::Exception(ec); 64 | } 65 | } 66 | 67 | inline void GetAddrInfo::freeaddrinfo(struct addrinfo *ai) 68 | { 69 | uv_freeaddrinfo(ai); 70 | } 71 | } 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /include/uv++/Handle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_HANDLE_HPP 2 | #define UV_HANDLE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Loop.hpp" 9 | #include "Noncopyable.hpp" 10 | 11 | namespace uv 12 | { 13 | template 14 | class Handle : public Noncopyable 15 | { 16 | public: 17 | bool isActive(); 18 | bool isClosing(); 19 | void ref(); 20 | void unref(); 21 | bool hasRef(); 22 | uv::Loop &loop(); 23 | void close(const std::function &handler); 24 | 25 | protected: 26 | T handle_; 27 | 28 | std::function closeHandler_ = []() {}; 29 | }; 30 | 31 | 32 | 33 | 34 | 35 | template 36 | inline bool Handle::isActive() 37 | { 38 | return uv_is_active(reinterpret_cast(&handle_)); 39 | } 40 | 41 | template 42 | inline bool Handle::isClosing() 43 | { 44 | return uv_is_closing(reinterpret_cast(&handle_)); 45 | } 46 | 47 | template 48 | inline void Handle::ref() 49 | { 50 | uv_ref(reinterpret_cast(&handle_)); 51 | } 52 | 53 | template 54 | inline void Handle::unref() 55 | { 56 | uv_unref(reinterpret_cast(&handle_)); 57 | } 58 | 59 | template 60 | inline bool Handle::hasRef() 61 | { 62 | return uv_has_ref(reinterpret_cast(&handle_)); 63 | } 64 | 65 | template 66 | inline uv::Loop &Handle::loop() 67 | { 68 | return *reinterpret_cast( 69 | reinterpret_cast(&handle_)->loop->data); 70 | } 71 | 72 | template 73 | inline void Handle::close(const std::function &handler) 74 | { 75 | closeHandler_ = handler; 76 | uv_close(reinterpret_cast(&handle_), [](uv_handle_t *h) { 77 | auto &handle = *reinterpret_cast *>(h->data); 78 | handle.closeHandler_(); 79 | }); 80 | } 81 | } 82 | 83 | 84 | #endif -------------------------------------------------------------------------------- /include/uv++/Idle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_IDLE_HPP 2 | #define UV_IDLE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "Loop.hpp" 11 | #include "Handle.hpp" 12 | 13 | namespace uv 14 | { 15 | class Idle : public Handle 16 | { 17 | public: 18 | Idle(); 19 | 20 | void init(uv::Loop &loop, std::error_code &ec); 21 | void init(uv::Loop &loop); 22 | void start(const std::function &handler, std::error_code &ec); 23 | void start(const std::function &handler); 24 | void stop(std::error_code &ec); 25 | void stop(); 26 | 27 | private: 28 | std::function startHandler_ = []() {}; 29 | }; 30 | 31 | 32 | 33 | 34 | 35 | inline Idle::Idle() 36 | { 37 | handle_.data = this; 38 | } 39 | 40 | inline void Idle::init(uv::Loop &loop, std::error_code &ec) 41 | { 42 | const auto status = uv_idle_init(loop.value(), &handle_); 43 | 44 | if (status != 0) { 45 | ec = makeErrorCode(status); 46 | } 47 | } 48 | 49 | inline void Idle::init(uv::Loop &loop) 50 | { 51 | std::error_code ec; 52 | 53 | init(loop, ec); 54 | if (ec) { 55 | throw uv::Exception(ec); 56 | } 57 | } 58 | 59 | inline void Idle::start(const std::function &handler, std::error_code &ec) 60 | { 61 | startHandler_ = handler; 62 | auto status = uv_idle_start(&handle_, [](uv_idle_t *handle) { 63 | auto &idle = *reinterpret_cast(handle->data); 64 | idle.startHandler_(); 65 | }); 66 | 67 | if (status != 0) { 68 | ec = makeErrorCode(status); 69 | } 70 | } 71 | 72 | inline void Idle::start(const std::function &handler) 73 | { 74 | std::error_code ec; 75 | 76 | start(handler, ec); 77 | if (ec) { 78 | throw uv::Exception(ec); 79 | } 80 | } 81 | 82 | inline void Idle::stop(std::error_code &ec) 83 | { 84 | const auto status = uv_idle_stop(&handle_); 85 | 86 | if (status != 0) { 87 | ec = makeErrorCode(status); 88 | } 89 | } 90 | 91 | inline void Idle::stop() 92 | { 93 | std::error_code ec; 94 | 95 | stop(ec); 96 | if (ec) { 97 | throw uv::Exception(ec); 98 | } 99 | } 100 | } 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /include/uv++/Loop.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_LOOP_HPP 2 | #define UV_LOOP_HPP 3 | 4 | #include 5 | 6 | #include "Error.hpp" 7 | #include "Exception.hpp" 8 | #include "Noncopyable.hpp" 9 | 10 | namespace uv 11 | { 12 | class Loop : public Noncopyable 13 | { 14 | public: 15 | Loop() = default; 16 | ~Loop(); 17 | 18 | void init(bool is_default, std::error_code &ec); 19 | void init(bool is_default = false); 20 | void run(std::error_code &ec); 21 | void run(); 22 | void runOnce(std::error_code &ec); 23 | void runOnce(); 24 | void runNowait(std::error_code &ec); 25 | void runNowait(); 26 | void stop(); 27 | void fork(std::error_code &ec); 28 | void fork(); 29 | uint64_t now() const; 30 | void updateTime(); 31 | bool alive() const; 32 | int backendFd() const; 33 | int backendTimeout() const; 34 | void printAllHandles(FILE *stream); 35 | void printActiveHandles(FILE *stream); 36 | uv_loop_t &operator()(); 37 | uv_loop_t *value(); 38 | 39 | private: 40 | uv_loop_t loop_; //not use 41 | uv_loop_t *loop_ptr_; 42 | }; 43 | 44 | 45 | 46 | 47 | 48 | inline Loop::~Loop() 49 | { 50 | uv_loop_close(loop_ptr_); 51 | } 52 | 53 | inline void Loop::init(bool is_default, std::error_code & ec) 54 | { 55 | if (is_default) { 56 | loop_ptr_ = uv_default_loop(); 57 | } 58 | else { 59 | loop_ptr_ = &loop_; 60 | auto status = uv_loop_init(loop_ptr_); 61 | if (status != 0) { 62 | ec = makeErrorCode(status); 63 | return; 64 | } 65 | } 66 | 67 | if (loop_ptr_ == nullptr) { 68 | ec = makeErrorCode(ENOMEM); 69 | return; 70 | } 71 | 72 | loop_ptr_->data = this; 73 | } 74 | 75 | inline void Loop::init(bool is_default) 76 | { 77 | std::error_code ec; 78 | 79 | init(is_default, ec); 80 | if (ec) { 81 | throw uv::Exception(ec); 82 | } 83 | } 84 | 85 | inline void Loop::run(std::error_code &ec) 86 | { 87 | auto status = uv_run(loop_ptr_, UV_RUN_DEFAULT); 88 | 89 | if (status != 0) { 90 | ec = makeErrorCode(status); 91 | } 92 | } 93 | 94 | inline void Loop::run() 95 | { 96 | std::error_code ec; 97 | 98 | run(ec); 99 | if (ec) { 100 | throw uv::Exception(ec); 101 | } 102 | } 103 | 104 | inline void Loop::runOnce(std::error_code &ec) 105 | { 106 | auto status = uv_run(loop_ptr_, UV_RUN_ONCE); 107 | 108 | if (status != 0) { 109 | ec = makeErrorCode(status); 110 | } 111 | } 112 | 113 | inline void Loop::runOnce() 114 | { 115 | std::error_code ec; 116 | 117 | runOnce(ec); 118 | if (ec) { 119 | throw uv::Exception(ec); 120 | } 121 | } 122 | 123 | inline void Loop::runNowait(std::error_code &ec) 124 | { 125 | auto status = uv_run(loop_ptr_, UV_RUN_NOWAIT); 126 | 127 | if (status != 0) { 128 | ec = makeErrorCode(status); 129 | } 130 | } 131 | 132 | inline void Loop::runNowait() 133 | { 134 | std::error_code ec; 135 | 136 | runNowait(ec); 137 | if (ec) { 138 | throw uv::Exception(ec); 139 | } 140 | } 141 | 142 | inline void Loop::stop() 143 | { 144 | uv_stop(loop_ptr_); 145 | } 146 | 147 | inline void Loop::fork(std::error_code &ec) 148 | { 149 | auto status = uv_loop_fork(loop_ptr_); 150 | 151 | if (status != 0) { 152 | ec = makeErrorCode(status); 153 | } 154 | } 155 | 156 | inline void Loop::fork() 157 | { 158 | std::error_code ec; 159 | 160 | fork(ec); 161 | if (ec) { 162 | throw uv::Exception(ec); 163 | } 164 | } 165 | 166 | inline uint64_t Loop::now() const 167 | { 168 | return uv_now(loop_ptr_); 169 | } 170 | 171 | inline void Loop::updateTime() 172 | { 173 | uv_update_time(loop_ptr_); 174 | } 175 | 176 | inline bool Loop::alive() const 177 | { 178 | return uv_loop_alive(loop_ptr_); 179 | } 180 | 181 | inline int Loop::backendFd() const 182 | { 183 | return uv_backend_fd(loop_ptr_); 184 | } 185 | 186 | inline int Loop::backendTimeout() const 187 | { 188 | return uv_backend_timeout(loop_ptr_); 189 | } 190 | 191 | inline void Loop::printAllHandles(FILE *stream) 192 | { 193 | uv_print_all_handles(loop_ptr_, stream); 194 | } 195 | 196 | inline void Loop::printActiveHandles(FILE *stream) 197 | { 198 | uv_print_active_handles(loop_ptr_, stream); 199 | } 200 | 201 | inline uv_loop_t &Loop::operator()() 202 | { 203 | return *loop_ptr_; 204 | } 205 | inline uv_loop_t * Loop::value() 206 | { 207 | return loop_ptr_; 208 | } 209 | } 210 | 211 | #endif 212 | -------------------------------------------------------------------------------- /include/uv++/Noncopyable.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NONCOPYABLE_HPP 2 | #define NONCOPYABLE_HPP 3 | 4 | class Noncopyable 5 | { 6 | protected: 7 | Noncopyable() = default; 8 | ~Noncopyable() = default; 9 | 10 | private: 11 | Noncopyable(const Noncopyable&) = delete; 12 | Noncopyable& operator=(const Noncopyable&) = delete; 13 | }; 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/uv++/Pipe.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_PIPE_HPP 2 | #define UV_PIPE_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | #include "Exception.hpp" 11 | #include "Loop.hpp" 12 | #include "Connect.hpp" 13 | 14 | #if ( defined(WIN32) || defined(WIN64) ) 15 | #include "Stream.hpp" 16 | #else 17 | #include "FileStreamHandle.hpp" 18 | #endif 19 | 20 | namespace uv 21 | { 22 | #if ( defined(WIN32) || defined(WIN64) ) 23 | class Pipe : public Stream 24 | #else 25 | class Pipe : public FileStreamHandle 26 | #endif 27 | { 28 | public: 29 | Pipe(); 30 | 31 | void init(uv::Loop &loop, int ipc, std::error_code &ec); 32 | void init(uv::Loop &loop, int ipc); 33 | void open(uv_file file, std::error_code &ec); 34 | void open(uv_file file); 35 | void bind(const std::string &name, std::error_code &ec); 36 | void bind(const std::string &name); 37 | void connect(const std::string &name, const std::function &handler, std::error_code &ec); 38 | void connect(const std::string &name, const std::function &handler); 39 | void getsockname(char *buffer, size_t &size, std::error_code &ecr) const; 40 | void getsockname(char *buffer, size_t &size) const; 41 | void getpeername(char *buffer, size_t &size, std::error_code &ec) const; 42 | void getpeername(char *buffer, size_t &size) const; 43 | void pendingInstances(int count); 44 | int pendingCount(); 45 | 46 | uv_handle_type pendingType(); 47 | 48 | private: 49 | std::function connectHandler_ 50 | = [](const std::error_code &ec) {}; 51 | }; 52 | 53 | 54 | 55 | 56 | 57 | inline Pipe::Pipe() 58 | { 59 | handle_.data = this; 60 | } 61 | 62 | inline void Pipe::init(uv::Loop &loop, int ipc, std::error_code &ec) 63 | { 64 | auto status = uv_pipe_init(loop.value(), &handle_, ipc); 65 | 66 | if (status != 0) { 67 | ec = makeErrorCode(status); 68 | } 69 | } 70 | 71 | inline void Pipe::init(uv::Loop &loop, int ipc) 72 | { 73 | std::error_code ec; 74 | 75 | init(loop, ipc, ec); 76 | if (ec) { 77 | throw uv::Exception(ec); 78 | } 79 | } 80 | 81 | inline void Pipe::open(uv_file file, std::error_code &ec) 82 | { 83 | auto status = uv_pipe_open(&handle_, file); 84 | 85 | if (status != 0) { 86 | ec = makeErrorCode(status); 87 | } 88 | } 89 | 90 | inline void Pipe::open(uv_file file) 91 | { 92 | std::error_code ec; 93 | 94 | open(file, ec); 95 | if (ec) { 96 | throw uv::Exception(ec); 97 | } 98 | } 99 | 100 | inline void Pipe::bind(const std::string &name, std::error_code &ec) 101 | { 102 | auto status = uv_pipe_bind(&handle_, name.c_str()); 103 | 104 | if (status != 0) { 105 | ec = makeErrorCode(status); 106 | } 107 | } 108 | 109 | inline void Pipe::bind(const std::string &name) 110 | { 111 | std::error_code ec; 112 | 113 | bind(name, ec); 114 | if (ec) { 115 | throw uv::Exception(ec); 116 | } 117 | } 118 | 119 | inline void Pipe::connect(const std::string &name, const std::function &handler, std::error_code &ec) 120 | { 121 | auto connect = new (std::nothrow) Connect; 122 | if (connect == nullptr) { 123 | ec = makeErrorCode(ENOMEM); 124 | return; 125 | } 126 | 127 | connectHandler_ = handler; 128 | uv_pipe_connect(&connect->handle_, &handle_, name.c_str(), 129 | [](uv_connect_t *req, int status) { 130 | 131 | std::shared_ptr connect(reinterpret_cast(req->data)); 132 | 133 | auto &pipe = *reinterpret_cast(req->handle->data); 134 | pipe.connectHandler_(makeErrorCode(status)); 135 | }); 136 | } 137 | 138 | inline void Pipe::connect(const std::string &name, const std::function &handler) 139 | { 140 | std::error_code ec; 141 | 142 | connect(name, handler, ec); 143 | if (ec) { 144 | throw uv::Exception(ec); 145 | } 146 | } 147 | 148 | inline void Pipe::getsockname(char *buffer, size_t &size, std::error_code &ec) const 149 | { 150 | auto status = uv_pipe_getsockname(&handle_, buffer, &size); 151 | 152 | if (status != 0) { 153 | ec = makeErrorCode(status); 154 | } 155 | } 156 | 157 | inline void Pipe::getsockname(char *buffer, size_t &size) const 158 | { 159 | std::error_code ec; 160 | 161 | getsockname(buffer, size, ec); 162 | if (ec) { 163 | throw uv::Exception(ec); 164 | } 165 | } 166 | 167 | inline void Pipe::getpeername(char *buffer, size_t &size, std::error_code &ec) const 168 | { 169 | auto status = uv_pipe_getpeername(&handle_, buffer, &size); 170 | 171 | if (status != 0) { 172 | ec = makeErrorCode(status); 173 | } 174 | } 175 | 176 | inline void Pipe::getpeername(char *buffer, size_t &size) const 177 | { 178 | std::error_code ec; 179 | 180 | getpeername(buffer, size, ec); 181 | if (ec) { 182 | throw uv::Exception(ec); 183 | } 184 | } 185 | 186 | inline void Pipe::pendingInstances(int count) 187 | { 188 | uv_pipe_pending_instances(&handle_, count); 189 | } 190 | 191 | inline int Pipe::pendingCount() 192 | { 193 | return uv_pipe_pending_count(&handle_); 194 | } 195 | 196 | inline uv_handle_type Pipe::pendingType() 197 | { 198 | return uv_pipe_pending_type(&handle_); 199 | } 200 | } 201 | 202 | 203 | #endif 204 | -------------------------------------------------------------------------------- /include/uv++/Poll.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_POLL_HPP 2 | #define UV_POLL_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "Loop.hpp" 11 | #include "FileHandle.hpp" 12 | 13 | namespace uv 14 | { 15 | class Poll : public FileHandle 16 | { 17 | public: 18 | Poll(); 19 | 20 | void init(uv::Loop &loop, int fd, std::error_code &ec); 21 | void init(uv::Loop &loop, int fd); 22 | void initSocket(uv::Loop &loop, uv_os_sock_t socket, std::error_code &ec); 23 | void initSocket(uv::Loop &loop, uv_os_sock_t socket); 24 | void start(std::error_code &ec); 25 | void start(); 26 | void stop(std::error_code &ec); 27 | void stop(); 28 | 29 | void onReadable(const std::function &handler); 30 | void onWritable(const std::function &handler); 31 | void onDisconnect(const std::function &handler); 32 | void onPrioritized(const std::function &handler); 33 | 34 | private: 35 | int events_ = 0; 36 | std::function readableHandler_ = [](const std::error_code &ec) {}; 37 | std::function writableHandler_ = [](const std::error_code &ec) {}; 38 | std::function disconnectHandler_ = [](const std::error_code &ec) {}; 39 | std::function prioritizedHandler_ = [](const std::error_code &ec) {}; 40 | }; 41 | 42 | 43 | 44 | 45 | 46 | inline Poll::Poll() 47 | { 48 | handle_.data = this; 49 | } 50 | 51 | inline void Poll::init(uv::Loop &loop, int fd, std::error_code &ec) 52 | { 53 | auto status = uv_poll_init(loop.value(), &handle_, fd); 54 | 55 | if (status != 0) { 56 | ec = makeErrorCode(status); 57 | } 58 | } 59 | 60 | inline void Poll::init(uv::Loop &loop, int fd) 61 | { 62 | std::error_code ec; 63 | 64 | init(loop, fd, ec); 65 | if (ec) { 66 | throw uv::Exception(ec); 67 | } 68 | } 69 | 70 | inline void Poll::initSocket(uv::Loop &loop, uv_os_sock_t socket, std::error_code &ec) 71 | { 72 | auto status = uv_poll_init_socket(loop.value(), &handle_, socket); 73 | 74 | if (status != 0) { 75 | ec = makeErrorCode(status); 76 | } 77 | } 78 | 79 | inline void Poll::initSocket(uv::Loop &loop, uv_os_sock_t socket) 80 | { 81 | std::error_code ec; 82 | 83 | initSocket(loop, socket, ec); 84 | if (ec) { 85 | throw uv::Exception(ec); 86 | } 87 | } 88 | 89 | inline void Poll::start(std::error_code &ec) 90 | { 91 | auto status = uv_poll_start(&handle_, events_, [](uv_poll_t* handle, int status, int events) { 92 | auto &poll = *reinterpret_cast(handle->data); 93 | if (events & UV_READABLE) { 94 | poll.readableHandler_(makeErrorCode(status)); 95 | } 96 | if (events & UV_WRITABLE) { 97 | poll.writableHandler_(makeErrorCode(status)); 98 | } 99 | if (events & UV_DISCONNECT) { 100 | poll.disconnectHandler_(makeErrorCode(status)); 101 | } 102 | if (events & UV_PRIORITIZED) { 103 | poll.prioritizedHandler_(makeErrorCode(status)); 104 | } 105 | }); 106 | 107 | if (status != 0) { 108 | ec = makeErrorCode(status); 109 | } 110 | } 111 | 112 | inline void Poll::start() 113 | { 114 | std::error_code ec; 115 | 116 | start(ec); 117 | if (ec) { 118 | throw uv::Exception(ec); 119 | } 120 | } 121 | 122 | inline void Poll::stop(std::error_code &ec) 123 | { 124 | auto status = uv_poll_stop(&handle_); 125 | 126 | if (status != 0) { 127 | ec = makeErrorCode(status); 128 | } 129 | } 130 | 131 | inline void Poll::stop() 132 | { 133 | std::error_code ec; 134 | 135 | stop(ec); 136 | if (ec) { 137 | throw uv::Exception(ec); 138 | } 139 | } 140 | 141 | inline void Poll::onReadable(const std::function &handler) 142 | { 143 | events_ += UV_READABLE; 144 | readableHandler_ = handler; 145 | } 146 | 147 | inline void Poll::onWritable(const std::function &handler) 148 | { 149 | events_ += UV_WRITABLE; 150 | writableHandler_ = handler; 151 | } 152 | 153 | inline void Poll::onDisconnect(const std::function &handler) 154 | { 155 | events_ += UV_DISCONNECT; 156 | disconnectHandler_ = handler; 157 | } 158 | 159 | inline void Poll::onPrioritized(const std::function &handler) 160 | { 161 | events_ += UV_PRIORITIZED; 162 | prioritizedHandler_ = handler; 163 | } 164 | } 165 | 166 | 167 | #endif 168 | -------------------------------------------------------------------------------- /include/uv++/Prepare.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_PREPARE_HPP 2 | #define UV_PREPARE_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "Loop.hpp" 11 | #include "Handle.hpp" 12 | 13 | namespace uv 14 | { 15 | class Prepare : public Handle 16 | { 17 | public: 18 | Prepare(); 19 | 20 | void init(uv::Loop &loop, std::error_code &ec); 21 | void init(uv::Loop &loop); 22 | void start(const std::function &handler, std::error_code &ec); 23 | void start(const std::function &handler); 24 | void stop(std::error_code &ec); 25 | void stop(); 26 | 27 | private: 28 | std::function startHandler_ = []() {}; 29 | }; 30 | 31 | 32 | 33 | 34 | 35 | inline Prepare::Prepare() 36 | { 37 | handle_.data = this; 38 | } 39 | 40 | inline void Prepare::init(uv::Loop &loop, std::error_code &ec) 41 | { 42 | auto status = uv_prepare_init(loop.value(), &handle_); 43 | 44 | if (status != 0) { 45 | ec = makeErrorCode(status); 46 | } 47 | } 48 | 49 | inline void Prepare::init(uv::Loop &loop) 50 | { 51 | std::error_code ec; 52 | 53 | init(loop, ec); 54 | if (ec) { 55 | throw uv::Exception(ec); 56 | } 57 | } 58 | 59 | inline void Prepare::start(const std::function &handler, std::error_code &ec) 60 | { 61 | startHandler_ = handler; 62 | auto status = uv_prepare_start(&handle_, [](uv_prepare_t *handle) { 63 | auto &prepare = *reinterpret_cast(handle->data); 64 | prepare.startHandler_(); 65 | }); 66 | 67 | if (status != 0) { 68 | ec = makeErrorCode(status); 69 | } 70 | } 71 | 72 | inline void Prepare::start(const std::function &handler) 73 | { 74 | std::error_code ec; 75 | 76 | start(handler, ec); 77 | if (ec) { 78 | throw uv::Exception(ec); 79 | } 80 | } 81 | 82 | inline void Prepare::stop(std::error_code &ec) 83 | { 84 | auto status = uv_prepare_stop(&handle_); 85 | 86 | if (status != 0) { 87 | ec = makeErrorCode(status); 88 | } 89 | } 90 | 91 | inline void Prepare::stop() 92 | { 93 | std::error_code ec; 94 | 95 | stop(ec); 96 | if (ec) { 97 | throw uv::Exception(ec); 98 | } 99 | } 100 | } 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /include/uv++/Req.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_REQ_HPP 2 | #define UV_REQ_HPP 3 | 4 | #include 5 | 6 | #include "Error.hpp" 7 | #include "Exception.hpp" 8 | #include "Noncopyable.hpp" 9 | 10 | namespace uv 11 | { 12 | template 13 | class Req : public Noncopyable 14 | { 15 | public: 16 | void cancel(std::error_code &ec); 17 | void cancel(); 18 | 19 | protected: 20 | T handle_; 21 | }; 22 | 23 | 24 | 25 | 26 | template 27 | inline void Req::cancel(std::error_code &ec) 28 | { 29 | const auto status = uv_cancel(reinterpret_cast(&handle_)); 30 | 31 | if (status != 0) { 32 | ec = makeErrorCode(status); 33 | } 34 | } 35 | 36 | template 37 | inline void Req::cancel() 38 | { 39 | std::error_code ec; 40 | 41 | cancel(ec); 42 | if (ec) { 43 | throw uv::Exception(ec); 44 | } 45 | } 46 | } 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/uv++/Shutdown.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_SHUTDOWN_HPP 2 | #define UV_SHUTDOWN_HPP 3 | 4 | #include 5 | 6 | #include "Req.hpp" 7 | 8 | namespace uv 9 | { 10 | template class Stream; 11 | 12 | class Shutdown : public Req 13 | { 14 | public: 15 | Shutdown(); 16 | 17 | private: 18 | template friend class Stream; 19 | }; 20 | 21 | 22 | 23 | 24 | 25 | inline Shutdown::Shutdown() 26 | { 27 | handle_.data = this; 28 | } 29 | } 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/uv++/Signal.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_SIGNAL_HPP 2 | #define UV_SIGNAL_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "Loop.hpp" 11 | #include "Handle.hpp" 12 | 13 | namespace uv 14 | { 15 | class Signal : public Handle 16 | { 17 | public: 18 | Signal(); 19 | 20 | void init(uv::Loop &loop, std::error_code &ec); 21 | void init(uv::Loop &loop); 22 | void start(const std::function &handler, int sigNum, std::error_code &ec); 23 | void start(const std::function &handler, int sigNum); 24 | void stop(std::error_code &ec); 25 | void stop(); 26 | 27 | private: 28 | std::function startHandler_ = [](int signum) {}; 29 | }; 30 | 31 | 32 | 33 | 34 | 35 | inline Signal::Signal() 36 | { 37 | handle_.data = this; 38 | } 39 | 40 | inline void Signal::init(uv::Loop &loop, std::error_code &ec) 41 | { 42 | auto status = uv_signal_init(loop.value(), &handle_); 43 | 44 | if (status != 0) { 45 | ec = makeErrorCode(status); 46 | } 47 | } 48 | 49 | inline void Signal::init(uv::Loop &loop) 50 | { 51 | std::error_code ec; 52 | 53 | init(loop, ec); 54 | if (ec) { 55 | throw uv::Exception(ec); 56 | } 57 | } 58 | 59 | inline void Signal::start(const std::function &handler, int sigNum, std::error_code &ec) 60 | { 61 | startHandler_ = handler; 62 | auto status = uv_signal_start(&handle_, [](uv_signal_t *handle, int num) { 63 | auto &signal = *reinterpret_cast(handle->data); 64 | signal.startHandler_(num); 65 | }, sigNum); 66 | 67 | if (status != 0) { 68 | ec = makeErrorCode(status); 69 | } 70 | } 71 | 72 | inline void Signal::start(const std::function &handler, int sigNum) 73 | { 74 | std::error_code ec; 75 | 76 | start(handler, sigNum, ec); 77 | if (ec) { 78 | throw uv::Exception(ec); 79 | } 80 | } 81 | 82 | inline void Signal::stop(std::error_code &ec) 83 | { 84 | auto status = uv_signal_stop(&handle_); 85 | 86 | if (status != 0) { 87 | ec = makeErrorCode(status); 88 | } 89 | } 90 | 91 | inline void Signal::stop() 92 | { 93 | std::error_code ec; 94 | 95 | stop(ec); 96 | if (ec) { 97 | throw uv::Exception(ec); 98 | } 99 | } 100 | } 101 | 102 | #endif 103 | -------------------------------------------------------------------------------- /include/uv++/Stream.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_STREAM_HPP 2 | #define UV_STREAM_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | #include "Error.hpp" 12 | #include "Write.hpp" 13 | #include "Shutdown.hpp" 14 | #include "FileHandle.hpp" 15 | 16 | namespace uv 17 | { 18 | template 19 | class Stream : public FileHandle 20 | { 21 | public: 22 | void listen(const std::function &handler, int backlog, std::error_code &ec); 23 | void listen(const std::function &handler, int backlog = SOMAXCONN); 24 | void accept(uv::Stream &client, std::error_code &ec); 25 | void accept(uv::Stream &client); 26 | void readStart(const std::function &handler, std::error_code &ec); 27 | void readStart(const std::function &handler); 28 | void readStop(std::error_code &ec); 29 | void readStop(); 30 | void write(const char *p, ssize_t len, std::error_code &ec); 31 | void write(const char *p, ssize_t len); 32 | void write(std::stringstream &ss, std::error_code &ec); 33 | void write(std::stringstream &ss); 34 | void onWrite(const std::function &handler); 35 | void shutdown(const std::function &handler, std::error_code &ec); 36 | void shutdown(const std::function &handler); 37 | bool isReadable(); 38 | bool isWritable(); 39 | void setBlocking(bool blocking, std::error_code &ec); 40 | void setBlocking(bool blocking = true); 41 | private: 42 | void writeNewArrayAndDeleteIt(const char *p, ssize_t len, std::error_code &ec); 43 | 44 | protected: 45 | using Handle::handle_; 46 | 47 | private: 48 | std::function listenHandler_ = [](const std::error_code &ec) {}; 49 | std::function shutdownHandler_ = [](const std::error_code &ec) {}; 50 | std::function writeHandler_ = [](const std::error_code &ec) {}; 51 | std::function readHandler_ = [](char *data, ssize_t len) {}; 52 | 53 | }; 54 | 55 | 56 | 57 | 58 | 59 | template 60 | inline void Stream::listen(const std::function &handler, int backlog, std::error_code &ec) 61 | { 62 | listenHandler_ = handler; 63 | auto status = uv_listen(reinterpret_cast(&handle_), backlog, [](uv_stream_t *st, int status) { 64 | auto &stream = *reinterpret_cast *>(st->data); 65 | stream.listenHandler_(makeErrorCode(status)); 66 | }); 67 | 68 | if (status != 0) { 69 | ec = makeErrorCode(status); 70 | } 71 | } 72 | 73 | template 74 | inline void Stream::listen(const std::function &handler, int backlog) 75 | { 76 | std::error_code ec; 77 | 78 | listen(handler, backlog, ec); 79 | if (ec) { 80 | throw uv::Exception(ec); 81 | } 82 | } 83 | 84 | template 85 | inline void Stream::accept(uv::Stream &client, std::error_code &ec) 86 | { 87 | auto status = uv_accept(reinterpret_cast(&handle_), 88 | reinterpret_cast(&(client.handle_))); 89 | 90 | if (status != 0) { 91 | ec = makeErrorCode(status); 92 | } 93 | } 94 | 95 | template 96 | inline void Stream::accept(uv::Stream &client) 97 | { 98 | std::error_code ec; 99 | 100 | accept(client, ec); 101 | if (ec) { 102 | throw uv::Exception(ec); 103 | } 104 | } 105 | 106 | template 107 | inline void Stream::readStart(const std::function &handler, std::error_code &ec) 108 | { 109 | readHandler_ = handler; 110 | auto status = uv_read_start(reinterpret_cast(&handle_), 111 | [](uv_handle_t* handle, size_t suggested_size, uv_buf_t *buff) 112 | { 113 | buff->base = new char[suggested_size]; 114 | buff->len = suggested_size; 115 | }, 116 | [](uv_stream_t *handle, ssize_t nread, const uv_buf_t *buff) 117 | { 118 | std::unique_ptr bytes(buff->base); 119 | auto &stream = *reinterpret_cast *>(handle->data); 120 | 121 | if (nread < 0) { 122 | stream.readHandler_(nullptr, nread); 123 | } 124 | else { 125 | stream.readHandler_(buff->base, nread); 126 | } 127 | }); 128 | 129 | if (status != 0) { 130 | ec = makeErrorCode(status); 131 | } 132 | } 133 | 134 | template 135 | inline void Stream::readStart(const std::function &handler) 136 | { 137 | std::error_code ec; 138 | 139 | readStart(handler, ec); 140 | if (ec) { 141 | throw uv::Exception(ec); 142 | } 143 | } 144 | 145 | template 146 | inline void Stream::readStop(std::error_code &ec) 147 | { 148 | auto status = uv_read_stop(reinterpret_cast(&handle_)); 149 | 150 | if (status != 0) { 151 | ec = makeErrorCode(status); 152 | } 153 | } 154 | 155 | template 156 | inline void Stream::readStop() 157 | { 158 | std::error_code ec; 159 | 160 | readStop(ec); 161 | if (ec) { 162 | throw uv::Exception(ec); 163 | } 164 | } 165 | 166 | template 167 | inline void Stream::write(const char *p, ssize_t len, std::error_code &ec) 168 | { 169 | if (len == 0) return; 170 | 171 | auto sendbuf = new char[len]; 172 | std::memcpy(sendbuf, p, len); 173 | writeNewArrayAndDeleteIt(sendbuf, len, ec); 174 | } 175 | 176 | template 177 | inline void Stream::write(const char *p, ssize_t len) 178 | { 179 | std::error_code ec; 180 | 181 | write(p, len, ec); 182 | if (ec) { 183 | throw uv::Exception(ec); 184 | } 185 | } 186 | 187 | template 188 | inline void Stream::write(std::stringstream &ss, std::error_code &ec) 189 | { 190 | auto len = ss.str().length(); 191 | if (len == 0) return; 192 | 193 | auto sendbuf = new char[len]; 194 | ss.read(sendbuf, len); 195 | writeNewArrayAndDeleteIt(sendbuf, len, ec); 196 | } 197 | 198 | template 199 | inline void Stream::write(std::stringstream &ss) 200 | { 201 | std::error_code ec; 202 | 203 | write(ss, ec); 204 | if (ec) { 205 | throw uv::Exception(ec); 206 | } 207 | } 208 | 209 | template 210 | inline void Stream::onWrite(const std::function &handler) 211 | { 212 | writeHandler_ = handler; 213 | } 214 | 215 | template 216 | inline void Stream::shutdown(const std::function &handler, std::error_code &ec) 217 | { 218 | shutdownHandler_ = handler; 219 | 220 | auto req = new (std::nothrow) Shutdown; 221 | if (req == nullptr) { 222 | ec = makeErrorCode(ENOMEM); 223 | return; 224 | } 225 | 226 | auto status = uv_shutdown(&req->handle_, reinterpret_cast(&handle_), 227 | [](uv_shutdown_t *req, int status) { 228 | std::unique_ptr shutdown(reinterpret_cast(req->data)); 229 | 230 | auto &stream = *reinterpret_cast *>(req->handle->data); 231 | stream.shutdownHandler_(makeErrorCode(status)); 232 | }); 233 | 234 | 235 | if (status != 0) { 236 | ec = makeErrorCode(status); 237 | } 238 | } 239 | 240 | template 241 | inline void Stream::shutdown(const std::function &handler) 242 | { 243 | std::error_code ec; 244 | 245 | shutdown(handler, ec); 246 | if (ec) { 247 | throw uv::Exception(ec); 248 | } 249 | } 250 | 251 | template 252 | inline bool Stream::isReadable() 253 | { 254 | return uv_is_readable(reinterpret_cast(&handle_)); 255 | } 256 | 257 | template 258 | inline bool Stream::isWritable() 259 | { 260 | return uv_is_writable(reinterpret_cast(&handle_)); 261 | } 262 | 263 | template 264 | inline void Stream::setBlocking(bool blocking, std::error_code &ec) 265 | { 266 | auto status = uv_stream_set_blocking(reinterpret_cast(&handle_), blocking); 267 | 268 | 269 | if (status != 0) { 270 | ec = makeErrorCode(status); 271 | } 272 | } 273 | template 274 | inline void Stream::setBlocking(bool blocking) 275 | { 276 | std::error_code ec; 277 | 278 | setBlocking(blocking, ec); 279 | if (ec) { 280 | throw uv::Exception(ec); 281 | } 282 | } 283 | 284 | template 285 | inline void Stream::writeNewArrayAndDeleteIt(const char * p, ssize_t len, std::error_code & ec) 286 | { 287 | auto writeHandler = new (std::nothrow) uv::Write(p, len); 288 | if (writeHandler == nullptr) { 289 | ec = makeErrorCode(ENOMEM); 290 | return; 291 | } 292 | 293 | auto status = uv_write(&writeHandler->handle_, reinterpret_cast(&handle_), 294 | &writeHandler->buf_, 1, [](uv_write_t *req, int status) { 295 | std::unique_ptr writeHandler(reinterpret_cast(req->data)); 296 | std::unique_ptr bytes(writeHandler->buf_.base); 297 | 298 | auto &stream = *reinterpret_cast *>(req->handle->data); 299 | stream.writeHandler_(makeErrorCode(status)); 300 | }); 301 | 302 | if (status != 0) { 303 | ec = makeErrorCode(status); 304 | } 305 | } 306 | 307 | } 308 | 309 | 310 | #endif 311 | -------------------------------------------------------------------------------- /include/uv++/Tcp.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_TCP_HPP 2 | #define UV_TCP_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | #include "Exception.hpp" 11 | #include "Loop.hpp" 12 | #include "Connect.hpp" 13 | #include "FileStreamHandle.hpp" 14 | 15 | namespace uv 16 | { 17 | class Tcp : public FileStreamHandle 18 | { 19 | public: 20 | Tcp(); 21 | 22 | void init(uv::Loop &loop, std::error_code &ec); 23 | void init(uv::Loop &loop); 24 | void init(uv::Loop &loop, unsigned int flags, std::error_code &ec); 25 | void init(uv::Loop &loop, unsigned int flags); 26 | 27 | void open(uv_os_sock_t sock, std::error_code &ec); 28 | void open(uv_os_sock_t sock); 29 | void nodelay(int enable, std::error_code &ec); 30 | void nodelay(int enable); 31 | void keepalive(int enable, unsigned int delay, std::error_code &ec); 32 | void keepalive(int enable, unsigned int delay); 33 | void simultaneousAccepts(int enable, std::error_code &ec); 34 | void simultaneousAccepts(int enable); 35 | void bind(const std::string &ip, int port, unsigned int flags, std::error_code &ec); 36 | void bind(const std::string &ip, int port, unsigned int flags); 37 | void getsockname(struct sockaddr &name, int &namelen, std::error_code &ec) const; 38 | void getsockname(struct sockaddr &name, int &namelenr) const; 39 | void getpeername(struct sockaddr &name, int &namelen, std::error_code &ec) const; 40 | void getpeername(struct sockaddr &name, int &namelen) const; 41 | void connect(const std::string &ip, int port, 42 | const std::function &handler, std::error_code &ec); 43 | void connect(const std::string &ip, int port, 44 | const std::function &handler); 45 | 46 | private: 47 | std::function connectHandler_ = [](const std::error_code &ec) {}; 48 | }; 49 | 50 | 51 | 52 | 53 | 54 | inline Tcp::Tcp() 55 | { 56 | handle_.data = this; 57 | } 58 | 59 | inline void Tcp::init(uv::Loop &loop, std::error_code &ec) 60 | { 61 | auto status = uv_tcp_init(loop.value(), &handle_); 62 | 63 | if (status != 0) { 64 | ec = makeErrorCode(status); 65 | } 66 | } 67 | 68 | inline void Tcp::init(uv::Loop &loop) 69 | { 70 | std::error_code ec; 71 | 72 | init(loop, ec); 73 | if (ec) { 74 | throw uv::Exception(ec); 75 | } 76 | } 77 | 78 | inline void Tcp::init(uv::Loop &loop, unsigned int flags, std::error_code &ec) 79 | { 80 | auto status = uv_tcp_init_ex(loop.value(), &handle_, flags); 81 | 82 | if (status != 0) { 83 | ec = makeErrorCode(status); 84 | } 85 | } 86 | 87 | inline void Tcp::init(uv::Loop &loop, unsigned int flags) 88 | { 89 | std::error_code ec; 90 | 91 | init(loop, ec); 92 | if (ec) { 93 | throw uv::Exception(ec); 94 | } 95 | } 96 | 97 | inline void Tcp::open(uv_os_sock_t sock, std::error_code &ec) 98 | { 99 | auto status = uv_tcp_open(&handle_, sock); 100 | 101 | if (status != 0) { 102 | ec = makeErrorCode(status); 103 | } 104 | } 105 | 106 | inline void Tcp::open(uv_os_sock_t sock) 107 | { 108 | std::error_code ec; 109 | 110 | open(sock, ec); 111 | if (ec) { 112 | throw uv::Exception(ec); 113 | } 114 | } 115 | 116 | inline void Tcp::nodelay(int enable, std::error_code &ec) 117 | { 118 | auto status = uv_tcp_nodelay(&handle_, enable); 119 | 120 | if (status != 0) { 121 | ec = makeErrorCode(status); 122 | } 123 | } 124 | 125 | inline void Tcp::nodelay(int enable) 126 | { 127 | std::error_code ec; 128 | 129 | nodelay(enable, ec); 130 | if (ec) { 131 | throw uv::Exception(ec); 132 | } 133 | } 134 | 135 | inline void Tcp::keepalive(int enable, unsigned int delay, std::error_code &ec) 136 | { 137 | auto status = uv_tcp_keepalive(&handle_, enable, delay); 138 | 139 | if (status != 0) { 140 | ec = makeErrorCode(status); 141 | } 142 | } 143 | 144 | inline void Tcp::keepalive(int enable, unsigned int delay) 145 | { 146 | std::error_code ec; 147 | 148 | keepalive(enable, delay, ec); 149 | if (ec) { 150 | throw uv::Exception(ec); 151 | } 152 | } 153 | 154 | inline void Tcp::simultaneousAccepts(int enable, std::error_code &ec) 155 | { 156 | auto status = uv_tcp_simultaneous_accepts(&handle_, enable); 157 | 158 | if (status != 0) { 159 | ec = makeErrorCode(status); 160 | } 161 | } 162 | 163 | inline void Tcp::simultaneousAccepts(int enable) 164 | { 165 | std::error_code ec; 166 | 167 | simultaneousAccepts(enable, ec); 168 | if (ec) { 169 | throw uv::Exception(ec); 170 | } 171 | } 172 | 173 | inline void Tcp::bind(const std::string &ip, int port, unsigned int flags, std::error_code &ec) 174 | { 175 | struct sockaddr *p_addr; 176 | 177 | struct sockaddr_in addr4; 178 | auto status = uv_ip4_addr(ip.c_str(), port, &addr4); 179 | if (status) { 180 | ec = makeErrorCode(status); 181 | struct sockaddr_in6 addr6; 182 | auto status = uv_ip6_addr(ip.c_str(), port, &addr6); 183 | if (status) { 184 | return; 185 | 186 | } 187 | p_addr = reinterpret_cast(&addr6); 188 | } 189 | else { 190 | p_addr = reinterpret_cast(&addr4); 191 | } 192 | 193 | status = uv_tcp_bind(&handle_, p_addr, flags); 194 | 195 | if (status != 0) { 196 | ec = makeErrorCode(status); 197 | } 198 | } 199 | 200 | inline void Tcp::bind(const std::string &ip, int port, unsigned int flags) 201 | { 202 | std::error_code ec; 203 | 204 | bind(ip, port, flags, ec); 205 | if (ec) { 206 | throw uv::Exception(ec); 207 | } 208 | } 209 | 210 | inline void Tcp::getsockname(struct sockaddr &name, int &namelen, std::error_code &ec) const 211 | { 212 | auto status = uv_tcp_getsockname(&handle_, &name, &namelen); 213 | 214 | if (status != 0) { 215 | ec = makeErrorCode(status); 216 | } 217 | } 218 | 219 | inline void Tcp::getsockname(sockaddr &name, int &namelen) const 220 | { 221 | std::error_code ec; 222 | 223 | getsockname(name, namelen, ec); 224 | if (ec) { 225 | throw uv::Exception(ec); 226 | } 227 | } 228 | 229 | inline void Tcp::getpeername(struct sockaddr &name, int &namelen, std::error_code &ec) const 230 | { 231 | auto status = uv_tcp_getpeername(&handle_, &name, &namelen); 232 | 233 | if (status != 0) { 234 | ec = makeErrorCode(status); 235 | } 236 | } 237 | 238 | inline void Tcp::getpeername(sockaddr &name, int &namelen) const 239 | { 240 | std::error_code ec; 241 | 242 | getpeername(name, namelen, ec); 243 | if (ec) { 244 | throw uv::Exception(ec); 245 | } 246 | } 247 | 248 | inline void Tcp::connect(const std::string &ip, int port, 249 | const std::function &handler, std::error_code &ec) 250 | { 251 | connectHandler_ = handler; 252 | 253 | auto connect = new (std::nothrow) Connect; 254 | if (connect == nullptr) { 255 | ec = makeErrorCode(ENOMEM); 256 | return; 257 | } 258 | 259 | struct sockaddr *p_addr; 260 | 261 | struct sockaddr_in addr4; 262 | auto status = uv_ip4_addr(ip.c_str(), port, &addr4); 263 | if (status != 0) { 264 | ec = makeErrorCode(status); 265 | struct sockaddr_in6 addr6; 266 | auto status = uv_ip6_addr(ip.c_str(), port, &addr6); 267 | if (status) { 268 | return; 269 | 270 | } 271 | p_addr = reinterpret_cast(&addr6); 272 | } 273 | else { 274 | p_addr = reinterpret_cast(&addr4); 275 | } 276 | 277 | status = uv_tcp_connect(&connect->handle_, &handle_, p_addr, 278 | [](uv_connect_t *req, int status) { 279 | std::unique_ptr connect(reinterpret_cast(req->data)); 280 | 281 | auto &tcp = *reinterpret_cast(req->handle->data); 282 | tcp.connectHandler_(makeErrorCode(status)); 283 | }); 284 | 285 | if (status != 0) { 286 | ec = makeErrorCode(status); 287 | } 288 | } 289 | 290 | inline void Tcp::connect(const std::string &ip, int port, 291 | const std::function &handler) 292 | { 293 | std::error_code ec; 294 | 295 | connect(ip, port, handler, ec); 296 | if (ec) { 297 | throw uv::Exception(ec); 298 | } 299 | } 300 | 301 | } 302 | 303 | 304 | #endif 305 | -------------------------------------------------------------------------------- /include/uv++/Timer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_TIMER_HPP 2 | #define UV_TIMER_HPP 3 | 4 | #include 5 | 6 | #include "Error.hpp" 7 | #include "Exception.hpp" 8 | #include "Loop.hpp" 9 | #include "Handle.hpp" 10 | 11 | namespace uv 12 | { 13 | class Timer : public Handle 14 | { 15 | public: 16 | Timer(); 17 | 18 | void init(uv::Loop &loop, std::error_code &ec); 19 | void init(uv::Loop &loop); 20 | void start(const std::function &handler, uint64_t timeout, uint64_t repeat, std::error_code &ec); 21 | void start(const std::function &handler, uint64_t timeout, uint64_t repeat); 22 | void stop(std::error_code &ec); 23 | void stop(); 24 | void again(std::error_code &ec); 25 | void again(); 26 | void setRepeat(uint64_t repeat); 27 | uint64_t getRepeat() const; 28 | 29 | private: 30 | std::function startHandler_ = []() {}; 31 | }; 32 | 33 | 34 | 35 | 36 | 37 | inline Timer::Timer() 38 | { 39 | handle_.data = this; 40 | } 41 | 42 | inline void Timer::init(uv::Loop &loop, std::error_code &ec) 43 | { 44 | auto status = uv_timer_init(loop.value(), &handle_); 45 | 46 | if (status != 0) { 47 | ec = makeErrorCode(status); 48 | } 49 | } 50 | 51 | inline void Timer::init(uv::Loop &loop) 52 | { 53 | std::error_code ec; 54 | 55 | init(loop, ec); 56 | if (ec) { 57 | throw uv::Exception(ec); 58 | } 59 | } 60 | 61 | inline void Timer::start(const std::function &handler, uint64_t timeout, uint64_t repeat, std::error_code &ec) 62 | { 63 | startHandler_ = handler; 64 | auto status = uv_timer_start(&handle_, [](uv_timer_t *handle) { 65 | auto &timer = *reinterpret_cast(handle->data); 66 | timer.startHandler_(); 67 | }, timeout, repeat); 68 | 69 | if (status != 0) { 70 | ec = makeErrorCode(status); 71 | } 72 | } 73 | 74 | inline void Timer::start(const std::function &handler, uint64_t timeout, uint64_t repeat) 75 | { 76 | std::error_code ec; 77 | 78 | start(handler, timeout, repeat, ec); 79 | if (ec) { 80 | throw uv::Exception(ec); 81 | } 82 | } 83 | 84 | inline void Timer::stop(std::error_code &ec) 85 | { 86 | auto status = uv_timer_stop(&handle_); 87 | 88 | if (status != 0) { 89 | ec = makeErrorCode(status); 90 | } 91 | } 92 | 93 | inline void Timer::stop() 94 | { 95 | std::error_code ec; 96 | 97 | stop(ec); 98 | if (ec) { 99 | throw uv::Exception(ec); 100 | } 101 | } 102 | 103 | inline void Timer::again(std::error_code &ec) 104 | { 105 | auto status = uv_timer_again(&handle_); 106 | 107 | if (status != 0) { 108 | ec = makeErrorCode(status); 109 | } 110 | } 111 | 112 | inline void Timer::again() 113 | { 114 | std::error_code ec; 115 | 116 | again(ec); 117 | if (ec) { 118 | throw uv::Exception(ec); 119 | } 120 | } 121 | 122 | inline void Timer::setRepeat(uint64_t repeat) 123 | { 124 | uv_timer_set_repeat(&handle_, repeat); 125 | } 126 | 127 | inline uint64_t Timer::getRepeat() const 128 | { 129 | return uv_timer_get_repeat(&handle_); 130 | } 131 | } 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /include/uv++/Tty.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_TTY_HPP 2 | #define UV_TTY_HPP 3 | 4 | #include 5 | 6 | #include "Error.hpp" 7 | #include "Exception.hpp" 8 | #include "Loop.hpp" 9 | #include "Stream.hpp" 10 | 11 | namespace uv 12 | { 13 | class Tty : public Stream 14 | { 15 | public: 16 | Tty(); 17 | 18 | void init(uv::Loop &loop, uv_file fd, int readable, std::error_code &ec); 19 | void init(uv::Loop &loop, uv_file fd, int readable); 20 | void setMode(uv_tty_mode_t mode, std::error_code &ec); 21 | void setMode(uv_tty_mode_t mode); 22 | void getWinsize(int &width, int &height, std::error_code &ec); 23 | void getWinsize(int &width, int &height); 24 | 25 | static void resetMode(std::error_code &ec); 26 | static void resetMode(); 27 | }; 28 | 29 | 30 | 31 | 32 | 33 | inline Tty::Tty() 34 | { 35 | handle_.data = this; 36 | } 37 | 38 | 39 | inline void Tty::init(uv::Loop &loop, uv_file fd, int readable, std::error_code &ec) 40 | { 41 | const auto status = uv_tty_init(loop.value(), &handle_, fd, readable); 42 | 43 | if (status != 0) { 44 | ec = makeErrorCode(status); 45 | } 46 | } 47 | 48 | inline void Tty::init(uv::Loop &loop, uv_file fd, int readable) 49 | { 50 | std::error_code ec; 51 | 52 | init(loop, fd, readable, ec); 53 | if (ec) { 54 | throw uv::Exception(ec); 55 | } 56 | } 57 | inline void Tty::setMode(uv_tty_mode_t mode, std::error_code &ec) 58 | { 59 | const auto status = uv_tty_set_mode(&handle_, mode); 60 | 61 | if (status != 0) { 62 | ec = makeErrorCode(status); 63 | } 64 | } 65 | 66 | inline void Tty::setMode(uv_tty_mode_t mode) 67 | { 68 | std::error_code ec; 69 | 70 | setMode(mode, ec); 71 | if (ec) { 72 | throw uv::Exception(ec); 73 | } 74 | } 75 | 76 | inline void Tty::resetMode(std::error_code &ec) 77 | { 78 | const auto status = uv_tty_reset_mode(); 79 | 80 | if (status != 0) { 81 | ec = makeErrorCode(status); 82 | } 83 | } 84 | 85 | inline void Tty::resetMode() 86 | { 87 | std::error_code ec; 88 | 89 | resetMode(ec); 90 | if (ec) { 91 | throw uv::Exception(ec); 92 | } 93 | } 94 | 95 | inline void Tty::getWinsize(int &width, int &height, std::error_code &ec) 96 | { 97 | const auto status = uv_tty_get_winsize(&handle_, &width, &height); 98 | 99 | if (status != 0) { 100 | ec = makeErrorCode(status); 101 | } 102 | } 103 | 104 | inline void Tty::getWinsize(int & width, int & height) 105 | { 106 | std::error_code ec; 107 | 108 | getWinsize(width, height, ec); 109 | if (ec) { 110 | throw uv::Exception(ec); 111 | } 112 | } 113 | } 114 | 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /include/uv++/Udp.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_UDP_HPP 2 | #define UV_UDP_HPP 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "Error.hpp" 10 | #include "Exception.hpp" 11 | #include "Loop.hpp" 12 | #include "FileStreamHandle.hpp" 13 | #include "UdpSend.hpp" 14 | 15 | namespace uv 16 | { 17 | class Udp : public FileStreamHandle 18 | { 19 | public: 20 | Udp(); 21 | 22 | void init(uv::Loop &loop, std::error_code &ec); 23 | void init(uv::Loop &loop); 24 | void init(uv::Loop &loop, unsigned int flags, std::error_code &ec); 25 | void init(uv::Loop &loop, unsigned int flags); 26 | void open(uv_os_sock_t sock, std::error_code &ec); 27 | void open(uv_os_sock_t sock); 28 | void bind(const struct sockaddr &addr, unsigned int flags, std::error_code &ec); 29 | void bind(const struct sockaddr &addr, unsigned int flags); 30 | void getsockname(struct sockaddr &name, int &namelen, std::error_code &ec) const; 31 | void getsockname(struct sockaddr &name, int &namelen) const; 32 | void setMembership(const std::string &multicast_addr, const std::string &interface_addr, 33 | uv_membership membership, std::error_code &ec); 34 | void setMembership(const std::string &multicast_addr, 35 | const std::string &interface_addr, uv_membership membership); 36 | void setMulticastLoop(int on, std::error_code &ec); 37 | void setMulticastLoop(int on); 38 | void setMulticastTtl(int ttl, std::error_code &ec); 39 | void setMulticastTtl(int ttl); 40 | void setMulticastInterface(const std::string &interface_addr, std::error_code &ec); 41 | void setMulticastInterface(const std::string &interface_addr); 42 | void setBroadcast(int on, std::error_code &ec); 43 | void setBroadcast(int on); 44 | void setTtl(int ttl, std::error_code &ec); 45 | void setTtl(int ttl); 46 | 47 | void recvStop(std::error_code &ec); 48 | void recvStop(); 49 | 50 | private: 51 | uv::UdpSend udpSend_; 52 | }; 53 | 54 | 55 | 56 | 57 | 58 | inline Udp::Udp() 59 | { 60 | handle_.data = this; 61 | } 62 | 63 | 64 | inline void Udp::init(uv::Loop &loop, std::error_code &ec) 65 | { 66 | const auto status = uv_udp_init(loop.value(), &handle_); 67 | 68 | if (status != 0) { 69 | ec = makeErrorCode(status); 70 | } 71 | } 72 | 73 | inline void Udp::init(uv::Loop &loop) 74 | { 75 | std::error_code ec; 76 | 77 | init(loop, ec); 78 | if (ec) { 79 | throw uv::Exception(ec); 80 | } 81 | } 82 | 83 | inline void Udp::init(uv::Loop &loop, unsigned int flags, std::error_code &ec) 84 | { 85 | const auto status = uv_udp_init_ex(loop.value(), &handle_, flags); 86 | 87 | if (status != 0) { 88 | ec = makeErrorCode(status); 89 | } 90 | } 91 | 92 | inline void Udp::init(uv::Loop &loop, unsigned int flags) 93 | { 94 | std::error_code ec; 95 | 96 | init(loop, ec); 97 | if (ec) { 98 | throw uv::Exception(ec); 99 | } 100 | } 101 | 102 | inline void Udp::open(uv_os_sock_t sock, std::error_code &ec) 103 | { 104 | const auto status = uv_udp_open(&handle_, sock); 105 | 106 | if (status != 0) { 107 | ec = makeErrorCode(status); 108 | } 109 | } 110 | 111 | inline void Udp::open(uv_os_sock_t sock) 112 | { 113 | std::error_code ec; 114 | 115 | open(sock, ec); 116 | if (ec) { 117 | throw uv::Exception(ec); 118 | } 119 | } 120 | 121 | inline void Udp::bind(const struct sockaddr &addr, unsigned int flags, std::error_code &ec) 122 | { 123 | const auto status = uv_udp_bind(&handle_, &addr, flags); 124 | 125 | if (status != 0) { 126 | ec = makeErrorCode(status); 127 | } 128 | } 129 | 130 | inline void Udp::bind(const sockaddr &addr, unsigned int flags) 131 | { 132 | std::error_code ec; 133 | 134 | bind(addr, flags, ec); 135 | if (ec) { 136 | throw uv::Exception(ec); 137 | } 138 | } 139 | 140 | inline void Udp::getsockname(struct sockaddr &name, int &namelen, std::error_code &ec) const 141 | { 142 | const auto status = uv_udp_getsockname(&handle_, &name, &namelen); 143 | 144 | if (status != 0) { 145 | ec = makeErrorCode(status); 146 | } 147 | } 148 | 149 | inline void Udp::getsockname(sockaddr &name, int &namelen) const 150 | { 151 | std::error_code ec; 152 | 153 | getsockname(name, namelen, ec); 154 | if (ec) { 155 | throw uv::Exception(ec); 156 | } 157 | } 158 | 159 | inline void Udp::setMembership(const std::string &multicast_addr, 160 | const std::string &interface_addr, uv_membership membership, std::error_code &ec) 161 | { 162 | const auto status = uv_udp_set_membership(&handle_, multicast_addr.c_str(), 163 | interface_addr.c_str(), membership); 164 | 165 | if (status != 0) { 166 | ec = makeErrorCode(status); 167 | } 168 | } 169 | 170 | inline void Udp::setMembership(const std::string &multicast_addr, 171 | const std::string &interface_addr, uv_membership membership) 172 | { 173 | std::error_code ec; 174 | 175 | setMembership(multicast_addr, interface_addr, membership, ec); 176 | if (ec) { 177 | throw uv::Exception(ec); 178 | } 179 | } 180 | 181 | inline void Udp::setMulticastLoop(int on, std::error_code &ec) 182 | { 183 | const auto status = uv_udp_set_multicast_loop(&handle_, on); 184 | 185 | if (status != 0) { 186 | ec = makeErrorCode(status); 187 | } 188 | } 189 | 190 | inline void Udp::setMulticastLoop(int on) 191 | { 192 | std::error_code ec; 193 | 194 | setMulticastLoop(on, ec); 195 | if (ec) { 196 | throw uv::Exception(ec); 197 | } 198 | } 199 | 200 | inline void Udp::setMulticastTtl(int ttl, std::error_code &ec) 201 | { 202 | const auto status = uv_udp_set_multicast_ttl(&handle_, ttl); 203 | 204 | if (status != 0) { 205 | ec = makeErrorCode(status); 206 | } 207 | } 208 | 209 | inline void Udp::setMulticastTtl(int ttl) 210 | { 211 | std::error_code ec; 212 | 213 | setMulticastTtl(ttl, ec); 214 | if (ec) { 215 | throw uv::Exception(ec); 216 | } 217 | } 218 | 219 | inline void Udp::setMulticastInterface(const std::string &interface_addr, std::error_code &ec) 220 | { 221 | const auto status = uv_udp_set_multicast_interface(&handle_, interface_addr.c_str()); 222 | 223 | if (status != 0) { 224 | ec = makeErrorCode(status); 225 | } 226 | } 227 | 228 | inline void Udp::setMulticastInterface(const std::string &interface_addr) 229 | { 230 | std::error_code ec; 231 | 232 | setMulticastInterface(interface_addr, ec); 233 | if (ec) { 234 | throw uv::Exception(ec); 235 | } 236 | } 237 | 238 | inline void Udp::setBroadcast(int on, std::error_code &ec) 239 | { 240 | const auto status = uv_udp_set_broadcast(&handle_, on); 241 | 242 | if (status != 0) { 243 | ec = makeErrorCode(status); 244 | } 245 | } 246 | 247 | inline void Udp::setBroadcast(int on) 248 | { 249 | std::error_code ec; 250 | 251 | setBroadcast(on, ec); 252 | if (ec) { 253 | throw uv::Exception(ec); 254 | } 255 | } 256 | 257 | inline void Udp::setTtl(int ttl, std::error_code &ec) 258 | { 259 | const auto status = uv_udp_set_ttl(&handle_, ttl); 260 | 261 | if (status != 0) { 262 | ec = makeErrorCode(status); 263 | } 264 | } 265 | 266 | inline void Udp::setTtl(int ttl) 267 | { 268 | std::error_code ec; 269 | 270 | setTtl(ttl, ec); 271 | if (ec) { 272 | throw uv::Exception(ec); 273 | } 274 | } 275 | 276 | inline void Udp::recvStop(std::error_code &ec) 277 | { 278 | const auto status = uv_udp_recv_stop(&handle_); 279 | 280 | if (status != 0) { 281 | ec = makeErrorCode(status); 282 | } 283 | } 284 | 285 | inline void Udp::recvStop() 286 | { 287 | std::error_code ec; 288 | 289 | recvStop(ec); 290 | if (ec) { 291 | throw uv::Exception(ec); 292 | } 293 | } 294 | } 295 | 296 | 297 | #endif 298 | -------------------------------------------------------------------------------- /include/uv++/UdpSend.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_UDP_SEND_HPP 2 | #define UV_UDP_SEND_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Req.hpp" 9 | 10 | namespace uv 11 | { 12 | class Udp; 13 | 14 | 15 | class UdpSend : public Req 16 | { 17 | public: 18 | UdpSend(); 19 | 20 | private: 21 | friend class Udp; 22 | }; 23 | 24 | 25 | 26 | 27 | 28 | inline UdpSend::UdpSend() 29 | { 30 | handle_.data = this; 31 | } 32 | } 33 | 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/uv++/Utility.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_UTILITY_HPP 2 | #define UV_UTILITY_HPP 3 | 4 | #include 5 | 6 | #if !(defined(WIN32) || defined(WIN64)) 7 | #define MAX_PATH PATH_MAX 8 | #endif 9 | 10 | #include 11 | 12 | #include "Error.hpp" 13 | 14 | namespace uv 15 | { 16 | class Utility 17 | { 18 | public: 19 | static void getExePath(std::string &path, std::error_code &ec); 20 | static void getExePath(std::string &path); 21 | static void getExeDirectory(std::string &path, std::error_code &ec); 22 | static void getExeDirectory(std::string &path); 23 | static void getCurrentWorkingDirectory(std::string &path, std::error_code& ec); 24 | static void getCurrentWorkingDirectory(std::string &path); 25 | static void getHomeDirectory(std::string &home, std::error_code &ec); 26 | static void getHomeDirectory(std::string &home); 27 | static void changeDirectory(const std::string &path, std::error_code &ec); 28 | static void changeDirectory(const std::string &path); 29 | }; 30 | 31 | 32 | 33 | 34 | 35 | 36 | inline void Utility::getExePath(std::string &path, std::error_code &ec) 37 | { 38 | char buffer[MAX_PATH]; 39 | size_t size = sizeof(buffer); 40 | 41 | auto status = uv_exepath(buffer, &size); 42 | path.clear(); 43 | path += buffer; 44 | 45 | if (status != 0) { 46 | ec = makeErrorCode(status); 47 | } 48 | } 49 | 50 | inline void Utility::getExePath(std::string &path) 51 | { 52 | std::error_code ec; 53 | 54 | getExePath(path, ec); 55 | if (ec) { 56 | throw uv::Exception(ec); 57 | } 58 | } 59 | 60 | inline void Utility::getExeDirectory(std::string &path, std::error_code &ec) 61 | { 62 | getExePath(path, ec); 63 | if (ec) return; 64 | 65 | auto index = path.rfind('/'); 66 | if (index == std::string::npos) 67 | { 68 | index = path.rfind('\\'); 69 | if (index == std::string::npos) 70 | { 71 | return; 72 | } 73 | } 74 | 75 | path.erase(index); 76 | } 77 | 78 | inline void Utility::getExeDirectory(std::string &path) 79 | { 80 | std::error_code ec; 81 | 82 | getExeDirectory(path, ec); 83 | if (ec) { 84 | throw uv::Exception(ec); 85 | } 86 | } 87 | 88 | inline void Utility::getCurrentWorkingDirectory(std::string &path, std::error_code &ec) 89 | { 90 | char buffer[MAX_PATH]; 91 | size_t size = sizeof(buffer); 92 | 93 | auto status = uv_cwd(buffer, &size); 94 | path.clear(); 95 | path += buffer; 96 | 97 | if (status != 0) { 98 | ec = makeErrorCode(status); 99 | } 100 | } 101 | 102 | inline void Utility::getCurrentWorkingDirectory(std::string &path) 103 | { 104 | std::error_code ec; 105 | 106 | getCurrentWorkingDirectory(path, ec); 107 | if (ec) { 108 | throw uv::Exception(ec); 109 | } 110 | } 111 | 112 | inline void Utility::changeDirectory(const std::string &path, std::error_code &ec) 113 | { 114 | auto status = uv_chdir(path.c_str()); 115 | 116 | if (status != 0) { 117 | ec = makeErrorCode(status); 118 | } 119 | } 120 | 121 | inline void Utility::changeDirectory(const std::string &path) 122 | { 123 | std::error_code ec; 124 | 125 | changeDirectory(path, ec); 126 | if (ec) { 127 | throw uv::Exception(ec); 128 | } 129 | } 130 | 131 | inline void Utility::getHomeDirectory(std::string &home, std::error_code &ec) 132 | { 133 | char buffer[MAX_PATH]; 134 | size_t size = sizeof(buffer); 135 | 136 | auto status = uv_os_homedir(buffer, &size); 137 | home.clear(); 138 | home += buffer; 139 | 140 | if (status != 0) { 141 | ec = makeErrorCode(status); 142 | } 143 | } 144 | 145 | inline void Utility::getHomeDirectory(std::string &home) 146 | { 147 | std::error_code ec; 148 | 149 | getHomeDirectory(home, ec); 150 | if (ec) { 151 | throw uv::Exception(ec); 152 | } 153 | } 154 | 155 | } 156 | 157 | #endif 158 | -------------------------------------------------------------------------------- /include/uv++/Work.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_WORK_HPP 2 | #define UV_WORK_HPP 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "Req.hpp" 11 | #include "Loop.hpp" 12 | 13 | namespace uv 14 | { 15 | class Work : public Req 16 | { 17 | public: 18 | Work(const std::function &wh, const std::function &ah); 19 | void queue(uv::Loop &loop, std::error_code &ec); 20 | void queue(uv::Loop &loop); 21 | 22 | private: 23 | std::function workHandler_ = []() {}; 24 | std::function afterWorkHandler_ = [](const std::error_code &ec) {}; 25 | }; 26 | 27 | 28 | 29 | 30 | 31 | inline Work::Work(const std::function &wh, const std::function &ah) 32 | { 33 | handle_.data = this; 34 | workHandler_ = wh; 35 | afterWorkHandler_ = ah; 36 | } 37 | 38 | inline void Work::queue(uv::Loop &loop, std::error_code &ec) 39 | { 40 | auto status = uv_queue_work(loop.value(), &handle_, 41 | [](uv_work_t *r) { 42 | auto &req = *reinterpret_cast(r->data); 43 | req.workHandler_(); 44 | }, 45 | [](uv_work_t *r,int status) { 46 | auto &req = *reinterpret_cast(r->data); 47 | req.afterWorkHandler_(makeErrorCode(status)); 48 | }); 49 | 50 | if (status != 0) { 51 | ec = makeErrorCode(status); 52 | } 53 | } 54 | 55 | inline void Work::queue(uv::Loop &loop) 56 | { 57 | std::error_code ec; 58 | 59 | queue(loop, ec); 60 | if (ec) { 61 | throw uv::Exception(ec); 62 | } 63 | } 64 | } 65 | 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /include/uv++/Write.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_WRITE_HPP 2 | #define UV_WRITE_HPP 3 | 4 | #include 5 | 6 | #include "Req.hpp" 7 | 8 | namespace uv 9 | { 10 | template class Stream; 11 | 12 | 13 | class Write : public Req 14 | { 15 | public: 16 | Write(const char *p, ssize_t len); 17 | 18 | private: 19 | template friend class Stream; 20 | private: 21 | uv_buf_t buf_; 22 | }; 23 | 24 | 25 | 26 | 27 | 28 | inline Write::Write(const char *p, ssize_t len) 29 | { 30 | buf_.base = const_cast(p); 31 | buf_.len = static_cast(len); 32 | handle_.data = this; 33 | } 34 | } 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /include/uv++/thread/Barrier.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_BARRIER_HPP 2 | #define UV_BARRIER_HPP 3 | 4 | #include 5 | 6 | #include "../Noncopyable.hpp" 7 | 8 | namespace uv 9 | { 10 | class Barrier : public Noncopyable 11 | { 12 | public: 13 | explicit Barrier(unsigned int count); 14 | ~Barrier(); 15 | 16 | int wait(); 17 | 18 | private: 19 | uv_barrier_t handle_; 20 | }; 21 | 22 | 23 | 24 | 25 | 26 | inline Barrier::Barrier(unsigned int count) 27 | { 28 | uv_barrier_init(&handle_, count); 29 | } 30 | 31 | inline Barrier::~Barrier() 32 | { 33 | uv_barrier_destroy(&handle_); 34 | } 35 | 36 | inline int Barrier::wait() 37 | { 38 | return uv_barrier_wait(&handle_); 39 | } 40 | } 41 | 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /include/uv++/thread/Cond.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_COND_HPP 2 | #define UV_COND_HPP 3 | 4 | #include 5 | 6 | #include "Mutex.hpp" 7 | #include "../Noncopyable.hpp" 8 | 9 | namespace uv 10 | { 11 | class Cond : public Noncopyable 12 | { 13 | public: 14 | Cond(); 15 | ~Cond(); 16 | 17 | void signal(); 18 | void broadcast(); 19 | void wait(uv::Mutex &mutex); 20 | int timedwait(uv::Mutex &mutex, uint64_t timeout); 21 | private: 22 | uv_cond_t handle_; 23 | }; 24 | 25 | 26 | 27 | 28 | 29 | inline Cond::Cond() 30 | { 31 | uv_cond_init(&handle_); 32 | } 33 | 34 | inline Cond::~Cond() 35 | { 36 | uv_cond_destroy(&handle_); 37 | } 38 | 39 | inline void Cond::signal() 40 | { 41 | uv_cond_signal(&handle_); 42 | } 43 | 44 | inline void Cond::broadcast() 45 | { 46 | uv_cond_broadcast(&handle_); 47 | } 48 | 49 | inline void Cond::wait(uv::Mutex &mutex) 50 | { 51 | uv_cond_wait(&handle_, &mutex.handle_); 52 | } 53 | 54 | inline int Cond::timedwait(uv::Mutex &mutex, uint64_t timeout) 55 | { 56 | return uv_cond_timedwait(&handle_, &mutex.handle_, timeout); 57 | } 58 | } 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /include/uv++/thread/Mutex.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_MUTEX_HPP 2 | #define UV_MUTEX_HPP 3 | 4 | #include 5 | 6 | #include "../Noncopyable.hpp" 7 | 8 | namespace uv 9 | { 10 | class Cond; 11 | 12 | 13 | class Mutex : public Noncopyable 14 | { 15 | public: 16 | Mutex(); 17 | ~Mutex(); 18 | 19 | void lock(); 20 | int trylock(); 21 | void unlock(); 22 | 23 | private: 24 | friend class Cond; 25 | private: 26 | uv_mutex_t handle_; 27 | }; 28 | 29 | 30 | 31 | 32 | 33 | inline Mutex::Mutex() 34 | { 35 | uv_mutex_init( &handle_); 36 | } 37 | 38 | inline Mutex::~Mutex() 39 | { 40 | uv_mutex_destroy(&handle_); 41 | } 42 | 43 | inline void Mutex::lock() 44 | { 45 | uv_mutex_lock(&handle_); 46 | } 47 | 48 | inline int Mutex::trylock() 49 | { 50 | return uv_mutex_trylock(&handle_); 51 | } 52 | 53 | inline void Mutex::unlock() 54 | { 55 | uv_mutex_unlock(&handle_); 56 | } 57 | } 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/uv++/thread/Rwlock.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_RWLOCK_HPP 2 | #define UV_RWLOCK_HPP 3 | 4 | #include 5 | 6 | #include "../Noncopyable.hpp" 7 | 8 | namespace uv 9 | { 10 | class Rwlock : public Noncopyable 11 | { 12 | public: 13 | Rwlock(); 14 | ~Rwlock(); 15 | 16 | void rdlock(); 17 | int tryrdlock(); 18 | void rdunlock(); 19 | void wrlock(); 20 | int trywrlock(); 21 | void wrunlock(); 22 | private: 23 | uv_rwlock_t handle_; 24 | }; 25 | 26 | 27 | 28 | 29 | 30 | inline Rwlock::Rwlock() 31 | { 32 | uv_rwlock_init(&handle_); 33 | } 34 | 35 | inline Rwlock::~Rwlock() 36 | { 37 | uv_rwlock_destroy(&handle_); 38 | } 39 | 40 | inline void Rwlock::rdlock() 41 | { 42 | uv_rwlock_rdlock(&handle_); 43 | } 44 | 45 | inline int Rwlock::tryrdlock() 46 | { 47 | return uv_rwlock_tryrdlock(&handle_); 48 | } 49 | 50 | inline void Rwlock::rdunlock() 51 | { 52 | uv_rwlock_rdunlock(&handle_); 53 | } 54 | 55 | inline void Rwlock::wrlock() 56 | { 57 | uv_rwlock_wrlock(&handle_); 58 | } 59 | 60 | inline int Rwlock::trywrlock() 61 | { 62 | return uv_rwlock_trywrlock(&handle_); 63 | } 64 | 65 | inline void Rwlock::wrunlock() 66 | { 67 | uv_rwlock_wrunlock(&handle_); 68 | } 69 | } 70 | 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /include/uv++/thread/Sem.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_SEM_HPP 2 | #define UV_SEM_HPP 3 | 4 | #include 5 | 6 | #include "../Noncopyable.hpp" 7 | 8 | namespace uv 9 | { 10 | class Sem : public Noncopyable 11 | { 12 | public: 13 | explicit Sem(unsigned int value); 14 | ~Sem(); 15 | 16 | void post(); 17 | void wait(); 18 | int trywait(); 19 | private: 20 | uv_sem_t handle_; 21 | }; 22 | 23 | 24 | 25 | 26 | 27 | inline Sem::Sem(unsigned int value) 28 | { 29 | uv_sem_init(&handle_, value); 30 | } 31 | 32 | inline Sem::~Sem() 33 | { 34 | uv_sem_destroy(&handle_); 35 | } 36 | 37 | inline void Sem::post() 38 | { 39 | uv_sem_post(&handle_); 40 | } 41 | 42 | inline void Sem::wait() 43 | { 44 | uv_sem_wait(&handle_); 45 | } 46 | 47 | inline int Sem::trywait() 48 | { 49 | return uv_sem_trywait(&handle_); 50 | } 51 | } 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /include/uv++/uv.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UV_UV_HPP 2 | #define UV_UV_HPP 3 | 4 | #include "Async.hpp" 5 | #include "Check.hpp" 6 | #include "Connect.hpp" 7 | #include "DynamicLibrary.hpp" 8 | #include "Error.hpp" 9 | #include "Exception.hpp" 10 | #include "FileStream.hpp" 11 | #include "FileStreamEvent.hpp" 12 | #include "FileStreamPoll.hpp" 13 | #include "GetAddrInfo.hpp" 14 | #include "Handle.hpp" 15 | #include "Idle.hpp" 16 | #include "Loop.hpp" 17 | #include "Noncopyable.hpp" 18 | #include "Pipe.hpp" 19 | #include "Poll.hpp" 20 | #include "Prepare.hpp" 21 | #include "Req.hpp" 22 | #include "Shutdown.hpp" 23 | #include "Signal.hpp" 24 | #include "Stream.hpp" 25 | #include "Tcp.hpp" 26 | #include "Timer.hpp" 27 | #include "Tty.hpp" 28 | #include "Udp.hpp" 29 | #include "UdpSend.hpp" 30 | #include "Utility.hpp" 31 | #include "Work.hpp" 32 | #include "Write.hpp" 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | add_compile_options(-std=c++11) 3 | 4 | include_directories(../include/) 5 | 6 | set(DEPS_LIBRARIES uv pthread rt) 7 | 8 | add_executable(timer timer.cpp) 9 | target_link_libraries(timer ${DEPS_LIBRARIES}) 10 | 11 | add_executable(daytimeclient daytimeclient.cpp) 12 | target_link_libraries(daytimeclient ${DEPS_LIBRARIES}) 13 | 14 | add_executable(interrupt_signal interrupt_signal.cpp) 15 | target_link_libraries(interrupt_signal ${DEPS_LIBRARIES}) 16 | -------------------------------------------------------------------------------- /samples/daytimeclient.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | uv::Loop loop; 8 | uv::Tcp tcp; 9 | 10 | try { 11 | loop.init(); 12 | tcp.init(loop); 13 | tcp.connect("127.0.0.1", 13, [&tcp](const std::error_code &ec) { 14 | if (ec) { 15 | std::cout << "链接失败" << std::endl; 16 | } 17 | else { 18 | try { 19 | tcp.readStart([](char *buf, ssize_t len) { 20 | if (len > 0) 21 | std::cout << std::string(buf, len) << std::endl; 22 | }); 23 | } 24 | catch (const uv::Exception &ex) { 25 | std::cout << ex.what() << std::endl; 26 | } 27 | } 28 | }); 29 | 30 | loop.run(); 31 | } 32 | catch (const uv::Exception &ex) { 33 | std::cout << ex.what() << std::endl; 34 | } 35 | } -------------------------------------------------------------------------------- /samples/interrupt_signal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | uv::Loop loop; 7 | uv::Signal signal; 8 | 9 | try { 10 | loop.init(); 11 | signal.init(loop); 12 | 13 | signal.start([](int signum) { 14 | std::cout << "Ctrl+C中断,信号量值为:" << signum << std::endl; 15 | }, SIGINT); 16 | 17 | loop.run(); 18 | } 19 | catch (const uv::Exception &ex) { 20 | std::cout << ex.what() << std::endl; 21 | } 22 | } -------------------------------------------------------------------------------- /samples/timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | uv::Loop loop; 7 | uv::Timer timer; 8 | 9 | try { 10 | loop.init(); 11 | timer.init(loop); 12 | 13 | timer.start([]() { 14 | std::cout << "Hello,World" << std::endl; 15 | }, 1000, 1000); 16 | 17 | loop.run(); 18 | } 19 | catch (const uv::Exception &ex) { 20 | std::cout << ex.what() << std::endl; 21 | } 22 | } 23 | 24 | --------------------------------------------------------------------------------