Update yasio to v4.2.0

This commit is contained in:
halx99 2024-02-07 19:49:51 +08:00
parent a1a444384e
commit 5eba65474f
9 changed files with 240 additions and 86 deletions

View File

@ -253,7 +253,7 @@
## yasio
- [![Upstream](https://img.shields.io/github/v/release/yasio/yasio?label=Upstream)](https://github.com/yasio/yasio)
- Version: 5.0.0
- Version: 4.2.0
- License: MIT WITH Anti-996
## zlib

View File

@ -77,21 +77,32 @@ YASIO_NI_API void yasio_init_globals(void(YASIO_INTEROP_DECL* pfn)(int level, co
}
YASIO_NI_API void yasio_cleanup_globals() { io_service::cleanup_globals(); }
struct yasio_event_data {
int kind;
int status;
int channel;
void* session; // transport
void* packet;
void* user; // event source
struct yasio_io_event {
int kind; //
int channel;
void* thandle;
union {
void* msg;
int status; //
};
void* user;
};
YASIO_NI_API void* yasio_create_service(int channel_count, void(YASIO_INTEROP_DECL* event_cb)(yasio_event_data* event), void* user)
YASIO_NI_API void* yasio_create_service(int channel_count, void(YASIO_INTEROP_DECL* event_cb)(yasio_io_event* event), void* user)
{
assert(!!event_cb);
io_service* service = new io_service(channel_count);
service->start([=](event_ptr e) {
auto& pkt = e->packet();
yasio_event_data event{e->kind(), e->status(), e->cindex(), e->transport(), !is_packet_empty(pkt) ? &pkt : nullptr, user};
yasio_io_event event;
event.kind = e->kind();
event.channel = e->cindex();
event.thandle = e->transport();
event.user = user;
if (event.kind == yasio::YEK_ON_PACKET)
event.msg = !is_packet_empty(pkt) ? &pkt : nullptr;
else
event.status = e->status();
event_cb(&event);
});
return service;
@ -239,15 +250,17 @@ YASIO_NI_API int yasio_write(void* service_ptr, void* thandle, const char* bytes
return service->write(reinterpret_cast<transport_handle_t>(thandle), yasio::sbyte_buffer(bytes, bytes + len));
return -1;
}
YASIO_NI_API int yasio_forward(void* service_ptr, void* thandle, void* bufferHandle, const char*(YASIO_INTEROP_DECL* pfnLockBuffer)(void* bufferHandle, int* bufferDataLen), void(YASIO_INTEROP_DECL* pfnUnlockBuffer)(void* bufferHandle))
YASIO_NI_API int yasio_forward(void* service_ptr, void* thandle, void* bufferHandle,
const char*(YASIO_INTEROP_DECL* pfnLockBuffer)(void* bufferHandle, int* bufferDataLen),
void(YASIO_INTEROP_DECL* pfnUnlockBuffer)(void* bufferHandle))
{
auto service = reinterpret_cast<io_service*>(service_ptr);
if (service) {
int len = 0;
if (service)
{
int len = 0;
auto bytes = pfnLockBuffer(bufferHandle, &len);
return service->forward(reinterpret_cast<transport_handle_t>(thandle), bytes, len, [bufferHandle,pfnUnlockBuffer](int,size_t){
pfnUnlockBuffer(bufferHandle);
});
return service->forward(reinterpret_cast<transport_handle_t>(thandle), bytes, len,
[bufferHandle, pfnUnlockBuffer](int, size_t) { pfnUnlockBuffer(bufferHandle); });
}
return -1;
}

View File

@ -205,7 +205,7 @@ SOFTWARE.
/*
** The yasio version macros
*/
#define YASIO_VERSION_NUM 0x050000
#define YASIO_VERSION_NUM 0x040200
/*
** The macros used by io_service.

57
thirdparty/yasio/yasio/fsutils.hpp vendored Normal file
View File

@ -0,0 +1,57 @@
//////////////////////////////////////////////////////////////////////////////////////////
// A multi-platform support c++11 library with focus on asynchronous socket I/O for any
// client application.
//////////////////////////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
Copyright (c) 2012-2024 HALX99
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include "yasio/string.hpp"
#include "yasio/string_view.hpp"
#include <fstream>
namespace yasio
{
inline yasio::string read_text_file(cxx17::string_view file_path)
{
std::ifstream fin(file_path.data(), std::ios_base::binary);
if (fin.is_open())
{
fin.seekg(std::ios_base::end);
auto n = static_cast<size_t>(fin.tellg());
if (n > 0)
{
yasio::string ret;
ret.resize_and_overwrite(n, [&fin](char* out, size_t outlen) {
fin.seekg(std::ios_base::beg);
fin.read(out, outlen);
return outlen;
});
return ret;
}
}
return yasio::string{};
}
} // namespace yasio

View File

@ -31,6 +31,8 @@ SOFTWARE.
#if YASIO_SSL_BACKEND == 2
# include "yasio/split.hpp"
YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts)
{
auto ctx = new yssl_ctx_st();
@ -63,17 +65,16 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts)
if (yasio__valid_str(opts.crtfile_)) // the cafile_ must be full path
{
int fail_count = 0;
yssl_splitpath(opts.crtfile_, [&](char* first, char* last) {
yssl_split_term null_term(last);
yasio::split(
opts.crtfile_, ',', [&](char* first, char* last) {
yasio::split_term null_term(last);
if ((ret = ::mbedtls_x509_crt_parse_file(&ctx->cert, first)) != 0)
{
++fail_count;
YASIO_LOG("mbedtls_x509_crt_parse_file with ret=-0x%x", (unsigned int)-ret);
}
return !!ret;
});
if ((ret = ::mbedtls_x509_crt_parse_file(&ctx->cert, first)) != 0)
{
++fail_count;
YASIO_LOG("mbedtls_x509_crt_parse_file with ret=-0x%x", (unsigned int)-ret);
}
});
if (!fail_count)
authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
}

View File

@ -31,6 +31,8 @@ SOFTWARE.
#if YASIO_SSL_BACKEND == 1 // OpenSSL
# include "yasio/split.hpp"
// The ssl error mask (1 << 31), a little hack, but works
# define YSSL_ERR_MASK 0x80000000
@ -51,8 +53,8 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts)
if (yasio__valid_str(opts.crtfile_))
{ // CAfile for verify
fail_count = 0;
yssl_splitpath(opts.crtfile_, [&](char* first, char* last) {
yssl_split_term null_term(last);
yasio::split(opts.crtfile_, ',', [&](char* first, char* last) {
yasio::split_term null_term(last);
# if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
/* OpenSSL 3.0.0 has deprecated SSL_CTX_load_verify_locations */
@ -65,15 +67,13 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts)
++fail_count;
YASIO_LOG("[global] load ca certifaction file failed!");
}
return !ok;
});
}
/*
* client cert & key not implement yet, since it not common usecase
* SSL_CTX_use_certificate_chain_file
* SSL_CTX_use_PrivateKey_file
*/
* client cert & key not implement yet, since it not common usecase
* SSL_CTX_use_certificate_chain_file
* SSL_CTX_use_PrivateKey_file
*/
if (!fail_count)
{
::SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, ::SSL_CTX_get_verify_callback(ctx));
@ -99,12 +99,12 @@ YASIO__DECL yssl_ctx_st* yssl_ctx_new(const yssl_options& opts)
if (yasio__valid_str(opts.keyfile_) && ::SSL_CTX_use_PrivateKey_file(ctx, opts.keyfile_, SSL_FILETYPE_PEM) <= 0)
YASIO_LOG("[gobal] load server private key file failed!");
/*
* If client provide cert, then verify, otherwise skip verify
* Note: if SSL_VERIFY_FAIL_IF_NO_PEER_CERT specified, client must provide
* cert & key which is not common use, means 100% online servers doesn't require
* client to provide cert
*/
/*
* If client provide cert, then verify, otherwise skip verify
* Note: if SSL_VERIFY_FAIL_IF_NO_PEER_CERT specified, client must provide
* cert & key which is not common use, means 100% online servers doesn't require
* client to provide cert
*/
::SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, ::SSL_CTX_get_verify_callback(ctx));
}

127
thirdparty/yasio/yasio/split.hpp vendored Normal file
View File

@ -0,0 +1,127 @@
//////////////////////////////////////////////////////////////////////////////////////////
// A multi-platform support c++11 library with focus on asynchronous socket I/O for any
// client application.
//////////////////////////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
Copyright (c) 2012-2024 HALX99
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* The standard split stub of yasio:
* The pred callback prototype: [](CStr first, CStr last) ->bool{ return true; }
* returns:
* true: want continue split
* false: abort split
*
*/
#pragma once
#include "yasio/string_view.hpp"
#include <string.h>
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4706)
#endif
namespace yasio
{
template <typename _CStr, typename _Pred>
inline void split_if(_CStr s, typename std::remove_pointer<_CStr>::type delim, _Pred&& pred)
{
auto _Start = s; // the start of every string
auto _Ptr = s; // source string iterator
while ((_Ptr = strchr(_Ptr, delim)))
{
if (_Start <= _Ptr && !pred(_Start, _Ptr))
return;
_Start = _Ptr + 1;
++_Ptr;
}
pred(_Start, nullptr); // last one, end is nullptr
}
template <typename _CStr, typename _Pred>
inline void split_if_n(_CStr s, size_t slen, typename std::remove_pointer<_CStr>::type delim, _Pred&& pred)
{
auto _Start = s; // the start of every string
auto _Ptr = s; // source string iterator
auto _End = s + slen;
while ((_Ptr = strchr(_Ptr, delim)))
{
if (_Ptr >= _End)
break;
if (_Start <= _Ptr && !pred(_Start, _Ptr))
return;
_Start = _Ptr + 1;
++_Ptr;
}
if (_Start <= _End)
pred(_Start, _End);
}
template <typename _CStr, typename _Func>
inline void split(_CStr s, typename std::remove_pointer<_CStr>::type delim, _Func&& func)
{
split_if(s, delim, [func](_CStr first, _CStr last) {
func(first, last);
return true;
});
}
template <typename _CStr, typename _Func>
inline void split_n(_CStr s, size_t slen, typename std::remove_pointer<_CStr>::type delim, _Func&& func)
{
split_if_n(s, slen, delim, [func](_CStr first, _CStr last) {
func(first, last);
return true;
});
}
struct split_term {
split_term(char* end)
{
if (end)
{
this->val_ = *end;
*end = '\0';
this->end_ = end;
}
}
~split_term()
{
if (this->end_)
*this->end_ = this->val_;
}
private:
char* end_ = nullptr;
char val_ = '\0';
};
} // namespace yasio
#if defined(_MSC_VER)
# pragma warning(pop)
#endif

View File

@ -98,50 +98,6 @@ YASIO__DECL int yssl_read(yssl_st* ssl, void* data, size_t len, int& err);
#define yasio__valid_str(cstr) (cstr && *cstr)
#define yasio__c_str(str) (!str.empty() ? &str.front() : nullptr)
/* private use for split cert files */
template <typename _Fty>
inline bool yssl_splitpath(char* str, _Fty&& func)
{
auto _Start = str; // the start of every string
auto _Ptr = str; // source string iterator
bool aborted = false;
while ((_Ptr = strchr(_Ptr, ',')))
{
if (_Start <= _Ptr)
{
if ((aborted = func(_Start, _Ptr)))
break;
}
_Start = _Ptr + 1;
++_Ptr;
}
if (!aborted)
aborted = func(_Start, nullptr); // last one
return aborted;
}
struct yssl_split_term {
yssl_split_term(char* end)
{
if (end)
{
this->val_ = *end;
*end = '\0';
this->end_ = end;
}
}
~yssl_split_term()
{
if (this->end_)
*this->end_ = this->val_;
}
private:
char* end_ = nullptr;
char val_ = '\0';
};
#if YASIO_SSL_BACKEND == 1 // openssl
# include "yasio/impl/openssl.hpp"
#elif YASIO_SSL_BACKEND == 2 // mbedtls

View File

@ -40,7 +40,7 @@ class io_event;
class io_channel;
typedef class io_transport* transport_handle_t;
} // namespace inet
#if !YASIO__HAS_NS_INLINE
#if !YASIO__HAS_CXX11
using namespace yasio::inet;
#endif
} // namespace yasio