Skip to content

Commit 7eeb538

Browse files
authored
Merge pull request #51 from index1207/develop
Add test suites & Fix buffer io
2 parents 06cd5d6 + bde1580 commit 7eeb538

File tree

14 files changed

+178
-96
lines changed

14 files changed

+178
-96
lines changed

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ endif(MSVC)
1515
add_compile_definitions(NETCPP_BUILD)
1616

1717
# Initialize
18-
option(NETCPP_TEST "Enable build with unitTest source" OFF)
18+
option(NETCPP_TEST "Enable build with unitTest source" ON)
1919

2020
set(CMAKE_CXX_STANDARD 20)
21+
file(GLOB_RECURSE SOURCE_FILES "${PROJECT_SOURCE_DIR}/src/*.cpp")
22+
set(INCLUDES "${PROJECT_SOURCE_DIR}/include")
23+
24+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
25+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
26+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
27+
2128
set(EXPORT_NAMESPACE "${PROJECT_NAME}::")
2229
set(CMAKE_CONFIG_FILE_BASENAME "${PROJECT_NAME}Config.cmake")
2330
set(CMAKE_EXPORT_FILE_BASENAME "${PROJECT_NAME}Export.cmake")
24-
file(GLOB_RECURSE SOURCE_FILES "${PROJECT_SOURCE_DIR}/src/*.cpp")
25-
set(INCLUDES "${PROJECT_SOURCE_DIR}/include")
2631

2732
string(REPLACE "/${CMAKE_LIBRARY_ARCHITECTURE}" "" CMAKE_INSTALL_LIBDIR_ARCHIND "${CMAKE_INSTALL_LIBDIR}")
2833

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ vcpkg add port netcpp # In manifest mode
1212

1313
Or clone this repository, and execute the command line below.
1414
```shell
15-
git clone https://github.com/index1207/netcpp.git && cd netcpp # clone and move directory
16-
cmake -B build # CMake Configuration
17-
cmake --build build --config Debug # Build Debug mode
18-
cmake --build build --config Release # Build Release mode
19-
cmake --install build --prefix {PATH_TO_INSTALL} # Install to other project
15+
git clone https://github.com/index1207/netcpp.git && cd netcpp # clone and move directory
16+
cmake -B build # CMake Configuration
17+
cmake --build build --config <BUILD_MODE> # Build library
18+
cmake --install build --config <BUILD_MODE> --prefix <PATH_TO_INSTALL> # Install to other project
2019
```
2120

2221
netcpp provides CMake targets:
@@ -31,12 +30,6 @@ target_link_libraries(main PRIVATE netcpp::netcpp)
3130
| `NETCPP_BUILD_SHARED` | Build by shared library |
3231
| `NETCPP_TEST` | Include unit test in build |
3332

34-
## Macros
35-
| Macro | Description |
36-
|-----------------|----------------------------------------------------------|
37-
| `NETCPP_STATIC` | When use this library by static, You should define this. |
38-
39-
4033
## Example
4134
- Create a socket
4235
```cpp

README_ko_KR.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ vcpkg add port netcpp # 매니페스트 모드에서
1111

1212
직접 클론하고 빌드하여 사용할 수 있습니다.
1313
```shell
14-
git clone https://github.com/index1207/netcpp.git && cd netcpp # 클론 및 디렉토리 이동
15-
cmake -B build # CMake 설정
16-
cmake --build build --config Debug # 디버그 모드로 빌드
17-
cmake --build build --config Release # 릴리즈 모드로 빌드
18-
cmake --install build --prefix {PATH_TO_INSTALL} # 다른 프로젝트에 설치
14+
git clone https://github.com/index1207/netcpp.git && cd netcpp # 클론 및 디렉토리 이동
15+
cmake -B build # CMake 설정
16+
cmake --build build --config <BUILD_MODE> # 지정한 모드로 빌드
17+
cmake --install build --config <BUILD_MODE> --prefix <PATH_TO_INSTALL> # 지정된 경로에 설치
1918
```
2019

2120
netcpp는 다음의 CMake 타겟을 제공합니다.
@@ -30,12 +29,6 @@ target_link_libraries(main PRIVATE netcpp::netcpp)
3029
| `NETCPP_BUILD_SHARED` | 공유 라이브러리로 빌드합니다. |
3130
| `NETCPP_TEST` | 유닛테스트를 빌드에 포함합니다. |
3231

33-
## 매크로
34-
| 매크로 | 설명 |
35-
|-----------------|---------------------------------------|
36-
| `NETCPP_STATIC` | 정적 라이브러리로 사용할 시, 반드시 이 매크로를 정의해야 합니다. |
37-
38-
3932
## 예제
4033
- 소켓 생성
4134
```cpp

include/net/context.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,23 @@ namespace net
3636
public:
3737
void set_buffer(char* buffer, int offset, int count);
3838
void set_buffer(std::span<char> buffer);
39+
40+
void add_data(char* buffer, int offset, int count);
41+
void add_data(std::span<char> buffer);
3942
public:
4043
std::shared_ptr<net::socket> accept_socket;
4144
std::optional<net::endpoint> endpoint;
42-
std::optional<std::vector<std::span<char>>> buffer_list;
4345
u_long length;
4446
private:
4547
void init();
4648
private:
4749
std::span<char> _buffer;
50+
#ifdef _WIN32
51+
std::vector<WSABUF> _buffer_list;
52+
#else
53+
std::vector<iovec> _buffer_list;
54+
#endif
55+
4856
void* _token;
4957
io_type _io_type;
5058
};

include/net/export.hpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
#pragma once
22

3-
#ifndef NETCPP_STATIC
4-
#ifdef _WIN32
5-
#define NETCPP_API_EXPORT __declspec(dllexport)
6-
#define NETCPP_API_IMPORT __declspec(dllimport)
3+
#ifdef _WIN32
4+
#ifdef NETCPP_BUILD_SHARED
5+
#define NETCPP_API __declspec(dllexport)
76
#pragma warning(disable: 4251)
8-
#else
9-
#define NETCPP_API_EXPORT __attribute__((__visibility__("default")))
10-
#define NETCPP_API_IMPORT __attribute__((__visibility__("default")))
7+
#elif NETCPP_SHARED
8+
#define NETCPP_API __declspec(dllimport)
119
#endif
1210
#else
13-
#define NETCPP_API_EXPORT
14-
#define NETCPP_API_IMPORT
15-
#endif
16-
17-
#ifdef NETCPP_BUILD_SHARED
18-
#define NETCPP_API NETCPP_API_EXPORT
19-
#else
20-
#define NETCPP_API NETCPP_API_IMPORT
11+
#ifdef NETCPP_BUILD_SHARED
12+
#define NETCPP_API __attribute__((__visibility__("default")))
13+
#elif NETCPP_SHARED
14+
#define NETCPP_API __attribute__((__visibility__("default")))
15+
#endif
2116
#endif
17+
#ifndef NETCPP_API
18+
#define NETCPP_API
19+
#endif

src/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
22

3-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
4-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
5-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
6-
73
# Build
84
if (NETCPP_BUILD_SHARED)
95
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
10-
target_compile_definitions(${PROJECT_NAME} PRIVATE NETCPP_BUILD_SHARED)
6+
target_compile_definitions(${PROJECT_NAME} PRIVATE NETCPP_BUILD_SHARED INTERFACE NETCPP_SHARED)
117
else ()
128
add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES})
139
target_compile_definitions(${PROJECT_NAME} PRIVATE NETCPP_STATIC)

src/context.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,34 @@ context::~context() = default;
2525

2626
void context::set_buffer(char* buffer, int offset, int count)
2727
{
28-
if (buffer_list.has_value())
28+
if (!_buffer_list.empty())
2929
throw std::runtime_error("Can't be use with `buffer_list`");
3030

3131
_buffer = std::span(buffer + offset, buffer + offset + count);
3232
}
3333

3434
void context::set_buffer(std::span<char> buffer)
3535
{
36-
if (buffer_list.has_value())
36+
if (!_buffer_list.empty())
3737
throw std::runtime_error("Can't be use with `buffer_list`");
3838

3939
_buffer = buffer;
4040
}
41+
42+
void context::add_data(char* buffer, int offset, int count)
43+
{
44+
#ifdef _WIN32
45+
_buffer_list.emplace_back(static_cast<ULONG>(count), buffer + offset);
46+
#else
47+
_buffer_list.push_back({buffer + offset, static_cast<size_t>(count)});
48+
#endif
49+
}
50+
51+
void context::add_data(std::span<char> buffer)
52+
{
53+
#ifdef _WIN32
54+
_buffer_list.emplace_back(static_cast<ULONG>(buffer.size()), buffer.data());
55+
#else
56+
_buffer_list.push_back({buffer.data(), buffer.size()});
57+
#endif
58+
}

src/native.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ bool native::demux(context* context, u_long transferred, bool success)
153153
case io_type::accept:
154154
if (success)
155155
{
156-
auto listen_socket = reinterpret_cast<const socket*>(context->_token);
156+
auto listen_socket = static_cast<const socket*>(context->_token);
157157
#ifdef _WIN32
158158
if (!observe(context->accept_socket.get()))
159159
return false;
@@ -175,6 +175,7 @@ bool native::demux(context* context, u_long transferred, bool success)
175175
endpoint.set_port(listen_socket->get_local_endpoint()->get_port());
176176
context->accept_socket->_local_endpoint = endpoint;
177177
}
178+
context->_token = nullptr;
178179
context->completed(context, success);
179180
break;
180181
case io_type::connect:

src/socket.cpp

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ bool socket::send(context* context) const
185185
context->init();
186186
context->_io_type = io_type::send;
187187
#ifdef _WIN32
188-
if (!context->buffer_list.has_value())
188+
if (context->_buffer_list.empty())
189189
{
190190
WSABUF wsaBuf {
191191
.len = static_cast<ULONG>(context->_buffer.size()),
@@ -198,16 +198,8 @@ bool socket::send(context* context) const
198198
}
199199
else
200200
{
201-
std::vector<WSABUF> wsabufs(context->buffer_list.value().size());
202-
std::ranges::transform(context->buffer_list.value(), wsabufs.begin(), [](const auto& span) {
203-
return WSABUF {
204-
.len = static_cast<ULONG>(span.size()),
205-
.buf = span.data()
206-
};
207-
});
208-
209201
DWORD dwSent = 0;
210-
if (SOCKET_ERROR == WSASend(_sock, wsabufs.data(), static_cast<DWORD>(wsabufs.size()), &dwSent, 0, context, nullptr))
202+
if (SOCKET_ERROR == WSASend(_sock, context->_buffer_list.data(), static_cast<DWORD>(context->_buffer_list.size()), &dwSent, 0, context, nullptr))
211203
{
212204
return WSA_IO_PENDING == WSAGetLastError();
213205
}
@@ -216,23 +208,16 @@ bool socket::send(context* context) const
216208
auto uring = native::get_handle();
217209
auto sqe = io_uring_get_sqe(uring);
218210

219-
if (!context->buffer_list.has_value())
211+
if (context->_buffer_list.empty())
220212
{
221-
io_uring_prep_send(sqe, get_handle(), context->_buffer.data(), context->_buffer.size(), 0);
213+
io_uring_prep_send(sqe, _sock, context->_buffer.data(), context->_buffer.size(), 0);
222214
}
223215
else
224216
{
225-
std::vector<iovec> iovecs(context->buffer_list->size());
226-
std::ranges::transform(context->buffer_list.value(), iovecs.begin(), [](const auto& span) {
227-
return iovec {
228-
.iov_base = span.data(),
229-
.iov_len = span.size()
230-
};
231-
});;
232217
msghdr msg {};
233-
msg.msg_iov = iovecs.data();
234-
msg.msg_iovlen = iovecs.size();
235-
io_uring_prep_sendmsg(sqe, get_handle(), &msg, 0);
218+
msg.msg_iov = context->_buffer_list.data();
219+
msg.msg_iovlen = context->_buffer_list.size();
220+
io_uring_prep_sendmsg(sqe, _sock, &msg, 0);
236221
}
237222

238223
io_uring_sqe_set_data(sqe, context);
@@ -250,7 +235,7 @@ bool socket::receive(context* context) const
250235
context->_io_type = io_type::receive;
251236
#ifdef _WIN32
252237
DWORD recvBytes = 0, flags = 0;
253-
if (!context->buffer_list.has_value())
238+
if (context->_buffer_list.empty())
254239
{
255240
WSABUF wsaBuf {
256241
.len = static_cast<ULONG>(context->_buffer.size()),
@@ -264,15 +249,7 @@ bool socket::receive(context* context) const
264249
}
265250
else
266251
{
267-
std::vector<WSABUF> wsabufs(context->buffer_list->size());
268-
std::ranges::transform(context->buffer_list.value(), wsabufs.begin(), [](const auto& span) {
269-
return WSABUF {
270-
.len = static_cast<ULONG>(span.size()),
271-
.buf = span.data()
272-
};
273-
});
274-
275-
if (SOCKET_ERROR == WSARecv(_sock, wsabufs.data(), static_cast<DWORD>(wsabufs.size()), &recvBytes, &flags, context, nullptr))
252+
if (SOCKET_ERROR == WSARecv(_sock, context->_buffer_list.data(), static_cast<DWORD>(context->_buffer_list.size()), &recvBytes, &flags, context, nullptr))
276253
{
277254
return WSA_IO_PENDING == WSAGetLastError();
278255
}
@@ -281,22 +258,15 @@ bool socket::receive(context* context) const
281258
auto uring = native::get_handle();
282259
auto sqe = io_uring_get_sqe(uring);
283260

284-
if (!context->buffer_list.has_value())
261+
if (context->_buffer_list.empty())
285262
{
286263
io_uring_prep_recv(sqe, get_handle(), context->_buffer.data(), context->_buffer.size(), 0);
287264
}
288265
else
289266
{
290-
std::vector<iovec> iovecs(context->buffer_list->size());
291-
std::ranges::transform(context->buffer_list.value(), iovecs.begin(), [](const auto& span) {
292-
return iovec {
293-
.iov_base = span.data(),
294-
.iov_len = span.size()
295-
};
296-
});;
297267
msghdr msg {};
298-
msg.msg_iov = iovecs.data();
299-
msg.msg_iovlen = iovecs.size();
268+
msg.msg_iov = context->_buffer_list.data();
269+
msg.msg_iovlen = context->_buffer_list.size();
300270
io_uring_prep_recvmsg(sqe, get_handle(), &msg, 0);
301271
}
302272

test/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
file(GLOB_RECURSE TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
22

3-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
4-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
5-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
6-
73
set(BINARY_NAME "unitTest")
84

95
option(gtest_disable_pthreads ON)

0 commit comments

Comments
 (0)