Skip to content

Commit

Permalink
update stream state. change errorcode exception name.
Browse files Browse the repository at this point in the history
add lib example, add library compile for test.
  • Loading branch information
levalup committed Jul 9, 2024
1 parent 4c78990 commit 3531ec8
Show file tree
Hide file tree
Showing 26 changed files with 324 additions and 92 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

--------------------------------

## v0.1.4

> Since: 2024-07-09
### New features

## Bug fix

## Break changes

- Change error exception name.
- `E_EAGAIN` -> `E_AGAIN`
- `E_EADDRINUSE` -> `E_ADDRINUSE`
- `E_EBADF` -> `E_BADF`
- `E_ENOTSOCK` -> `E_NOTSOCK`

--------------------------------

## v0.1.3

> Date: 2024-07-02
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ list(APPEND HEADERS "${ENTRY_HEADER}")

add_subdirectory(examples)

# -------------------------------------------------------------------------------
# Build libraries
# -------------------------------------------------------------------------------

add_subdirectory(libs)

# -------------------------------------------------------------------------------
# Add tests
# -------------------------------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ foreach (path ${FILES})
endif ()
endforeach ()

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
foreach (target ${TARGETS})
set_target_properties(${target} PROPERTIES OUTPUT_NAME "debug-${target}")
endforeach ()
endif ()

if (WALL)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
foreach (target ${TARGETS})
Expand All @@ -71,12 +77,6 @@ if (WALL)
endif ()
endif ()

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
foreach (target ${TARGETS})
set_target_properties(${target} PROPERTIES OUTPUT_NAME "debug-${target}")
endforeach ()
endif ()

if (WERROR)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
foreach (target ${TARGETS})
Expand Down
4 changes: 4 additions & 0 deletions examples/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ int main() {
uv::fs::rmdir(nullptr, tmp, path, nullptr);
});

#if UVCXX_SATISFY_VERSION(1, 28, 0)

// readdir
uv::fs::opendir("tmp").then([](uv_dir_t *dir) {
uv::fs::readdir(dir, 1024).then([](uv_dirent_t *file, size_t size) {
Expand All @@ -64,6 +66,8 @@ int main() {
});
});

#endif

// scandir
uv::fs::scandir("tmp", 0).then([](const uv::fs::scan_next &next) {
uv_dirent_t file{};
Expand Down
33 changes: 33 additions & 0 deletions examples/lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by Levalup.
// L.eval: Let programmer get rid of only work jobs.
//

#include <iostream>

#include "uvcxx/utils/assert.h"
#include "uvcxx/utils/platform.h"
#include "uvcxx/lib.h"

int main() {
#if UVCXX_OS_MAC
auto libname = "libadd.dylib";
#elif UVCXX_OS_LINUX
auto libname = "libadd.so";
#elif UVCXX_OS_WINDOWS
auto libname = "add.dll";
#else
auto libname = "add";
#endif

uv::lib_t lib = nullptr;
lib.open(libname);

auto add = lib.sym<int(int, int)>("add");

uvcxx_assert(add(1, 2) == 3);

std::cout << "1 + 2 = " << add(1, 2) << std::endl;

return 0;
}
6 changes: 4 additions & 2 deletions examples/pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main() {
});
conn.read_start().call([=](ssize_t nread, const uv_buf_t *buf) mutable {
std::cout << "server read: " << std::string(buf->base, nread) << std::endl;
}).except<uvcxx::E_EAGAIN>([]() {
}).except<uvcxx::E_AGAIN>([]() {
}).except<uvcxx::E_EOF>([=]() mutable {
conn.close(nullptr);
}).except([=]() mutable {
Expand Down Expand Up @@ -63,7 +63,9 @@ int main() {
client.connect(pipe_name).then([=]() mutable {
client.write(msg).then([=]() {
std::cout << "client write: " << msg << std::endl;
}).finally([=]() mutable {
});
// wait write finish then shutdown
client.shutdown().finally([=]() mutable {
client.close(nullptr);
});
}).except([=]() mutable {
Expand Down
6 changes: 4 additions & 2 deletions examples/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int main() {
});
conn.read_start().call([=](ssize_t nread, const uv_buf_t *buf) mutable {
std::cout << "server read: " << std::string(buf->base, nread) << std::endl;
}).except<uvcxx::E_EAGAIN>([]() {
}).except<uvcxx::E_AGAIN>([]() {
}).except<uvcxx::E_EOF>([=]() mutable {
conn.close(nullptr);
}).except([=]() mutable {
Expand Down Expand Up @@ -89,7 +89,9 @@ int main() {
client.connect(addr).then([=]() mutable {
client.write(msg).then([=]() {
std::cout << "client write: " << msg << std::endl;
}).finally([=]() mutable {
});
// wait write finish then shutdown
client.shutdown().finally([=]() mutable {
client.close(nullptr);
});
}).except([=]() mutable {
Expand Down
10 changes: 1 addition & 9 deletions examples/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ uvcxx::any_address_t getsockname(const uv::udp_t &tcp) {
return name;
}

uvcxx::any_address_t getpeername(const uv::udp_t &tcp) {
uvcxx::any_address_t name;
auto len = name.len();
(void) tcp.getpeername(name, &len);
return name;
}

int main() {
// setting
std::string localhost = "127.0.0.1";
Expand Down Expand Up @@ -80,8 +73,7 @@ int main() {
auto msg = uvcxx::catstr("hello~", i);

uv::udp_t client(client_loop, false);
client.connect(addr);
client.send(msg, nullptr).then([=]() {
client.send(msg, addr).then([=]() {
std::cout << "client write: " << msg << std::endl;
}).finally([=]() mutable {
client.close(nullptr);
Expand Down
1 change: 1 addition & 0 deletions include/uvcxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "uvcxx/cxx/buffer.h"
#include "uvcxx/cxx/except.h"
#include "uvcxx/cxx/ref.h"
#include "uvcxx/cxx/sockaddr.h"
#include "uvcxx/cxx/string.h"
#include "uvcxx/cxx/to_string.h"
#include "uvcxx/cxx/version.h"
Expand Down
2 changes: 1 addition & 1 deletion include/uvcxx/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace uv {
std::free(this->base);
}

explicit operator bool() const { return this->base; }
explicit operator bool() const { return bool(this->base); }

void free() {
if (this->base) std::free(this->base);
Expand Down
10 changes: 6 additions & 4 deletions include/uvcxx/cxx/except.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ namespace uvcxx {
name() : supper(code) {} \
}

uvcxx_define_errcode(E_CANCELED, UV_ECANCELED);

uvcxx_define_errcode(E_EOF, UV_EOF);

uvcxx_define_errcode(E_EAGAIN, UV_EAGAIN);
uvcxx_define_errcode(E_AGAIN, UV_EAGAIN);

uvcxx_define_errcode(E_EADDRINUSE, UV_EADDRINUSE);
uvcxx_define_errcode(E_ADDRINUSE, UV_EADDRINUSE);

uvcxx_define_errcode(E_EBADF, UV_EBADF);
uvcxx_define_errcode(E_BADF, UV_EBADF);

uvcxx_define_errcode(E_ENOTSOCK, UV_ENOTSOCK);
uvcxx_define_errcode(E_NOTSOCK, UV_ENOTSOCK);
}

#if defined(UVCXX_NO_EXCEPTION)
Expand Down
4 changes: 2 additions & 2 deletions include/uvcxx/cxx/ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace uvcxx {
m_handle.reset();
}

explicit operator bool() const { return m_handle; }
explicit operator bool() const { return bool(m_handle); }

Handle &operator*() { return *m_handle; }

Expand Down Expand Up @@ -114,7 +114,7 @@ namespace uvcxx {
m_handle.reset();
}

explicit operator bool() const { return m_handle; }
explicit operator bool() const { return bool(m_handle); }

Handle &operator*() { return **m_handle; }

Expand Down
4 changes: 2 additions & 2 deletions include/uvcxx/inner/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace uvcxx {

shared_raw_base_t(std::nullptr_t) {}

operator bool() const { return m_raw; }
operator bool() const { return bool(m_raw); }

operator raw_t *() { return m_raw.get(); }

Expand Down Expand Up @@ -109,7 +109,7 @@ namespace uvcxx {
delete m_raw;
}

operator bool() const { return m_raw; }
operator bool() const { return bool(m_raw); }

operator raw_t *() { return m_raw; }

Expand Down
4 changes: 2 additions & 2 deletions include/uvcxx/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace uv {
/**
* This class is not thread-safe.
*/
class lib_t : uvcxx::pointer_raw_base_t<uv_lib_t> {
class lib_t : public uvcxx::pointer_raw_base_t<uv_lib_t> {
public:
using self = lib_t;
using supper = uvcxx::pointer_raw_base_t<uv_lib_t>;
Expand Down Expand Up @@ -82,7 +82,7 @@ namespace uv {
UVCXX_NODISCARD
FUNC *sym(uvcxx::string name) const {
FUNC *func;
auto err = sym(name, &func);
auto err = sym(name, (void **)&func);
if (err < 0) return nullptr;
return func;
}
Expand Down
8 changes: 2 additions & 6 deletions include/uvcxx/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ namespace uv {
if (*this) uv_os_free_passwd(*this);
}

explicit operator bool() const {
return raw()->username;
}
explicit operator bool() const { return bool(raw()->username); }
};

inline passwd_t get_passwd() {
Expand Down Expand Up @@ -159,9 +157,7 @@ namespace uv {
if (*this) uv_os_free_group(*this);
}

explicit operator bool() const {
return raw()->members;
}
explicit operator bool() const { return bool(raw()->members); }
};

inline group_t get_group(uv_uid_t gid) {
Expand Down
37 changes: 24 additions & 13 deletions include/uvcxx/pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace uv {

UVCXX_APPLY(uv_accept(*this, client), nullptr);

client.data<data_t>()->work_mode = WorkMode::Agent;
return client;
}

Expand All @@ -57,57 +58,67 @@ namespace uv {
}

int bind(uvcxx::string name) {
UVCXX_PROXY(uv_pipe_bind(*this, name));
UVCXX_APPLY(uv_pipe_bind(*this, name), status);

data<data_t>()->work_mode = WorkMode::Server;
return 0;
}

#if UVCXX_SATISFY_VERSION(1, 46, 0)

int bind2(const char *name, size_t namelen, unsigned int flags) {
UVCXX_PROXY(uv_pipe_bind2(*this, name, namelen, flags));
UVCXX_APPLY(uv_pipe_bind2(*this, name, namelen, flags), status);

data<data_t>()->work_mode = WorkMode::Server;
return 0;
}

int bind2(uvcxx::string_view name, unsigned int flags) {
return bind2(name.data, name.size, flags);
return this->bind2(name.data, name.size, flags);
}

#endif

UVCXX_NODISCARD
uvcxx::promise<> connect(const connect_t &req, uvcxx::string name) {
auto p = pipe_connect(req, *this, name);
return p ? (_detach_(), p) : nullptr;
if (p) {
data<data_t>()->work_mode = WorkMode::Client;
_detach_();
}
return p;
}

UVCXX_NODISCARD
uvcxx::promise<> connect(uvcxx::string name) {
auto p = pipe_connect(*this, name);
return p ? (_detach_(), p) : nullptr;
return this->connect({}, name);
}

#if UVCXX_SATISFY_VERSION(1, 46, 0)

UVCXX_NODISCARD
uvcxx::promise<> connect2(const connect_t &req, const char *name, size_t namelen, unsigned int flags) {
auto p = pipe_connect2(req, *this, name, namelen, flags);
return p ? (_detach_(), p) : nullptr;
if (p) {
data<data_t>()->work_mode = WorkMode::Client;
_detach_();
}
return p;
}

UVCXX_NODISCARD
uvcxx::promise<> connect2(const char *name, size_t namelen, unsigned int flags) {
auto p = pipe_connect2(*this, name, namelen, flags);
return p ? (_detach_(), p) : nullptr;
return this->connect2({}, name, namelen, flags);
}

UVCXX_NODISCARD
uvcxx::promise<> connect2(const connect_t &req, uvcxx::string_view name, unsigned int flags) {
auto p = pipe_connect2(req, *this, name.data, name.size, flags);
return p ? (_detach_(), p) : nullptr;
return this->connect2(req, name.data, name.size, flags);
}

UVCXX_NODISCARD
uvcxx::promise<> connect2(uvcxx::string_view name, unsigned int flags) {
auto p = pipe_connect2(*this, name.data, name.size, flags);
return p ? (_detach_(), p) : nullptr;
return this->connect2({}, name.data, name.size, flags);
}

#endif
Expand Down
2 changes: 1 addition & 1 deletion include/uvcxx/poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace uv {
}
switch (status) {
case UV_EAGAIN:
data->start_cb.raise<uvcxx::E_EAGAIN>();
data->start_cb.raise<uvcxx::E_AGAIN>();
break;
default:
data->start_cb.raise<uvcxx::errcode>(status);
Expand Down
Loading

0 comments on commit 3531ec8

Please sign in to comment.