├── .DS_Store ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── Utils ├── CMakeLists.txt ├── NNGReq.h ├── Structs.h ├── Utils.cpp └── Utils.h ├── conf ├── conf.toml ├── dbCommon.h ├── dbCommon.sql ├── dbTest.db ├── dbTest.h ├── dbTest.sql ├── ddl2cpp └── main.py ├── configs ├── CMakeLists.txt ├── Configs.cpp ├── Configs.h ├── Routers.cpp ├── Routers.h ├── conf.cpp └── conf.h ├── database ├── CMakeLists.txt ├── CommonDBProcess.cpp ├── CommonDBProcess.h ├── DBConnectManager.cpp ├── DBConnectManager.h ├── DBConnectNNG.cpp ├── DBConnectNNG.h ├── DBConnectTCP.cpp ├── DBConnectTCP.h ├── DBManager.cpp ├── DBManager.h ├── DBNNg.cpp ├── DBNNg.h ├── DBTcp.cpp ├── DBTcp.h ├── DBs.cpp ├── DBs.h ├── DealAuth.cpp ├── DealAuth.h ├── DealEcho.cpp ├── DealEcho.h ├── NNGDatabaseReq.h ├── UserManager.cpp ├── UserManager.h ├── database.cpp └── database.h ├── db.db ├── demo ├── CMakeLists.txt └── Source.cpp ├── endian-master.zip ├── hashlibrary ├── CMakeLists.txt ├── LICENSE ├── crc32.cpp ├── crc32.h ├── digest.cpp ├── hash.h ├── hmac.h ├── keccak.cpp ├── keccak.h ├── md5.cpp ├── md5.h ├── readme.md ├── sha1.cpp ├── sha1.h ├── sha256.cpp ├── sha256.h ├── sha3.cpp ├── sha3.h └── tests │ ├── github-issue2.cpp │ ├── github-issue6.cpp │ └── tests.cpp ├── hotupdate ├── CMakeLists.txt ├── CoEvent.cpp ├── CoEvent.h ├── FileOp.cpp ├── FileOp.h ├── FilePathMonitor.cpp ├── FilePathMonitor.h ├── IOContextManager.cpp ├── IOContextManager.h ├── PathMonitorImpl.cpp ├── PathMonitorImpl.h ├── _fileOpAsync.cpp ├── _fileOpSync.cpp ├── hotupdate.cpp └── hotupdate.h ├── inc ├── .DS_Store ├── Apps.h ├── HttpCmdTag.h ├── MainCmdTag.h ├── TransCmdTag.h ├── boost │ └── te.hpp ├── commUsing.h ├── consts.h ├── coros.h ├── ctml.hpp ├── enums.h ├── iguana │ ├── detail │ │ ├── itoa.hpp │ │ ├── string_stream.hpp │ │ └── traits.hpp │ ├── json.hpp │ ├── msgpack.hpp │ ├── reflection.hpp │ └── xml.hpp ├── myconcept.h ├── oneshot.hpp ├── orm │ ├── comm_db.h │ ├── comm_db.sql │ ├── ddl2cpp │ ├── demo.h │ ├── init_db.sql │ ├── main.py │ ├── rest.h │ ├── rest.sql │ └── weixin.h ├── os │ ├── _darwinSpec.h │ ├── _linuxSpec.h │ ├── _winSpec.h │ └── os.h ├── reqreps.h ├── subprocess.h ├── templatefuncs.h └── tsl │ ├── array-hash │ ├── array_growth_policy.h │ ├── array_hash.h │ ├── array_map.h │ └── array_set.h │ ├── htrie_hash.h │ ├── htrie_map.h │ └── htrie_set.h ├── logo.png ├── networkinterface ├── CMakeLists.txt ├── ChannelCombine.h ├── ChannelManager.cpp ├── ChannelManager.h ├── ChannelsMgr.cpp ├── ChannelsMgr.h ├── FullDuplexChannel.cpp ├── FullDuplexChannel.h ├── FullDuplexChannelManager.h ├── Https.cpp ├── Https.h ├── LocalNetChannel.cpp ├── LocalNetChannel.h ├── LocalNetManager.cpp ├── LocalNetManager.h ├── MainPlatformDealer.cpp ├── MainPlatformDealer.h ├── MesageHandlerBase.cpp ├── MesageHandlerBase.h ├── MessageRouter.cpp ├── MessageRouter.h ├── MessageSplitFuncs.cpp ├── MessageSplitFuncs.h ├── NMessage.cpp ├── NMessage.h ├── PackDealerBase.h ├── PackDealerCommon.cpp ├── PackDealerCommon.h ├── PackDealerMainSub.cpp ├── PackDealerMainSub.h ├── PackDealerNoHead.cpp ├── PackDealerNoHead.h ├── PackDealerNoMain.cpp ├── PackDealerNoMain.h ├── PackUnpack.cpp ├── PackUnpack.h ├── PackUnpackManager.cpp ├── PackUnpackManager.h ├── PlatformPackInterface.h ├── ReadMe.txt ├── ServerBase.h ├── ServerCommon.cpp ├── ServerCommon.h ├── StreamHandler.h ├── StreamNetDealer.cpp ├── StreamNetDealer.h ├── dealers │ ├── MainAssPlatPack.cpp │ ├── MainAssPlatPack.h │ ├── NoMainAssPlatPack.cpp │ └── NoMainAssPlatPack.h ├── networkinterface.cpp ├── networkinterface.h ├── nngs │ ├── AsynBase.h │ ├── AsynRep.cpp │ ├── AsynRep.h │ ├── AsynRepImpl.cpp │ ├── AsynRepImpl.h │ ├── AsynReq.cpp │ ├── AsynReq.h │ ├── AsynReqImpl.cpp │ ├── AsynReqImpl.h │ ├── NNGCommBase.h │ ├── NngRep.h │ ├── NngReq.h │ ├── NngServerBase.h │ ├── RepManager.h │ ├── ReqImpl.cpp │ ├── ReqImpl.h │ ├── ReqManager.h │ ├── ReqRepRaw.cpp │ ├── ReqRepRaw.h │ ├── SyncRep2.h │ ├── SyncReq.cpp │ └── SyncReq.h ├── socket │ ├── TcpChannel.h │ ├── TcpChannelImp.h │ ├── TcpChannelImpl.cpp │ ├── TcpClient.h │ ├── TcpClientCombine.h │ ├── TcpServer.h │ ├── TcpServerCombine.h │ ├── TcpStreamHandler.cpp │ └── TcpStreamHandler.h └── wss.h ├── rayX ├── CMakeLists.txt ├── ray │ ├── api.h │ └── api │ │ ├── actor_creator.h │ │ ├── actor_handle.h │ │ ├── actor_task_caller.h │ │ ├── arguments.h │ │ ├── common_types.h │ │ ├── function_manager.h │ │ ├── internal_api.h │ │ ├── logging.h │ │ ├── object_ref.h │ │ ├── overload.h │ │ ├── ray_config.h │ │ ├── ray_exception.h │ │ ├── ray_remote.h │ │ ├── ray_runtime.h │ │ ├── ray_runtime_holder.h │ │ ├── runtime_env.h │ │ ├── serializer.h │ │ ├── static_check.h │ │ ├── task_caller.h │ │ ├── task_options.h │ │ ├── type_traits.h │ │ ├── wait_result.h │ │ └── xlang_function.h ├── rayx.cpp └── rayx.h ├── tests ├── CMakeLists.txt ├── asyncEvent │ ├── CMakeLists.txt │ └── Common.cpp ├── asyncStreamtest │ ├── CMakeLists.txt │ ├── Common.cpp │ ├── linux │ │ └── typecheck.cpp │ └── win │ │ └── winspec.cpp ├── envcheck │ ├── CMakeLists.txt │ ├── Common.cpp │ ├── linux │ │ └── typecheck.cpp │ └── win │ │ └── typecheck.cpp ├── networktest │ ├── CMakeLists.txt │ ├── Common.cpp │ ├── linux │ │ └── typecheck.cpp │ └── win │ │ └── winspec.cpp ├── rayXtest │ ├── CMakeLists.txt │ ├── Common.cpp │ ├── linux │ │ └── typecheck.cpp │ └── win │ │ └── typecheck.cpp └── sqlppTest │ ├── CMakeLists.txt │ └── Common.cpp ├── tzdata2022a.tar.gz ├── uSockets2 ├── CMakeLists.txt ├── src │ ├── .DS_Store │ ├── bsd.c │ ├── context.c │ ├── crypto │ │ ├── openssl.c │ │ ├── sni_tree.cpp │ │ └── wolfssl.c │ ├── eventing │ │ ├── asio.cpp │ │ ├── epoll_kqueue.c │ │ ├── gcd.c │ │ └── libuv.c │ ├── internal │ │ ├── eventing │ │ │ ├── asio.h │ │ │ ├── epoll_kqueue.h │ │ │ ├── gcd.h │ │ │ └── libuv.h │ │ ├── internal.h │ │ ├── loop_data.h │ │ └── networking │ │ │ └── bsd.h │ ├── libusockets.h │ ├── loop.c │ ├── quic.c │ ├── quic.h │ ├── socket.c │ └── udp.c ├── uSockets-0.8.2.zip └── uSockets-0.8.3.zip └── whereami ├── .gitattributes ├── .gitignore ├── CMakeLists.txt ├── LICENSE.MIT ├── LICENSE.WTFPLv2 ├── README.md ├── _gnu-make └── Makefile ├── _ios-xcode ├── .gitignore ├── WhereAmI-Info.plist └── WhereAmI.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ └── contents.xcworkspacedata ├── _mac-xcode ├── .gitignore └── WhereAmI.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ └── contents.xcworkspacedata ├── example ├── executable.c └── library.c └── src ├── whereami.c └── whereami.h /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build*/ 2 | external/* 3 | !external/CMakeLists.txt 4 | *.sublime-workspace 5 | .tags 6 | tags 7 | *.pyc 8 | *.swp 9 | *.swo 10 | 11 | system.txt 12 | 13 | *.log 14 | /.vscode/ 15 | /source/.vscode/ 16 | 17 | /source/inc/export_flags/ 18 | 19 | /inc/export_flags/ 20 | /inc/Windows/ 21 | /inc/Linux/ 22 | /inc/os/Windows/ 23 | /inc/os/Linux/ 24 | /inc/os/Darwin/ 25 | /inc/Darwin/ 26 | /win_build/ 27 | /.vs/ 28 | 29 | endian/ 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 alqaz 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 | # SM 2 | 3 |
4 | 5 | 格式化输出 fmt 6 | 7 | 日志 spdlog 8 | 9 | 关系数据库连接 sqlpp11 10 | 11 | tcp/udp(uSockets,libuv) 12 | 13 | http(uWebSockets) 14 | 15 | 协程,磁盘文件异步访问(asio) 16 | 17 | 强扩展的消息协议(nanomsg) 18 | 19 | 任务调度库(taskflow)支持cpu和GPU 20 | 21 | 无锁队列(concurrentqueue) 22 | 23 | 枚举类型反射(magic_enum) 24 | 25 | 序列化json(nlohmann-json) 26 | 27 | 磁盘文件更新检测库(efsw) 28 | 29 | python交互(pybind11) 30 | 31 | 以及其他一些周边的库(jwt-cpp,cppcodec等) 32 | 33 | 编译工具(cmake(3.20之后版本),方便和vcpkg配合使用,后续加入windows,deb,rpm安装包打包脚本(cpack)) 34 | 35 | 包管理工具(vcpkg) 36 | 37 | 未开始模块: 38 | 39 | 分布式执行运行时(Ray,分布式调用直接简化成C++的函数和类,使用非常简单) 40 | 41 | rpc(rpclib) 42 | 43 | 通用框架SM(Suture monster 名字本身是缝合),中文名:百家衣 44 | 45 |
46 | 中途遇到比较困难的问题会在 https://github.com/yangxingpping/crashDay.git 记录 47 |

48 | -------------------------------------------------------------------------------- /Utils/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(Utils) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | FILE(GLOB_RECURSE MY_HEADERS "./*.h") 7 | source_group("Headers" FILES ${MY_HEADERS}) 8 | 9 | FILE(GLOB_RECURSE MY_INTERFACEHEADERS "../inc/networks/*.h") 10 | source_group("Headers" FILES ${MY_INTERFACEHEADERS}) 11 | 12 | add_definitions(-DBUILD_SHARED_LIBS -DUtils_EXPORTS) 13 | 14 | include_directories(../inc) 15 | 16 | AUX_SOURCE_DIRECTORY(. A) 17 | 18 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 19 | link_libraries(Ws2_32) 20 | add_compile_options(/Zc:__cplusplus /Wv:19.27) 21 | ADD_LIBRARY(Utils SHARED ${A}) 22 | set_target_properties(Utils PROPERTIES VS_GLOBAL_VcpkgEnabled true) 23 | ELSE() 24 | ADD_LIBRARY(Utils ${A}) 25 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 26 | 27 | include(GenerateExportHeader) 28 | generate_export_header(Utils EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 29 | 30 | target_include_directories( 31 | Utils SYSTEM 32 | PUBLIC 33 | "$" 34 | ) 35 | 36 | target_link_libraries(Utils PRIVATE spdlog::spdlog PRIVATE date::date date::date-tz PRIVATE msgpack-c) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Utils/NNGReq.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "reqs.h" 5 | #include "Utils.h" 6 | #include "spdlog/spdlog.h" 7 | #include "magic_enum.hpp" 8 | #include 9 | #include 10 | #include 11 | #include "nng/nng.h" 12 | #include "nng/protocol/reqrep0/req.h" 13 | 14 | using std::string; 15 | using std::string_view; 16 | 17 | namespace SMUtils 18 | { 19 | class NNGReq 20 | { 21 | public: 22 | NNGReq(string ip, uint16_t port, MainCmd mainx, ChannelType ct) 23 | : _ip(ip) 24 | , _port(port) 25 | , _mainc(mainx) 26 | , _channelType(ct) 27 | { 28 | string addr = "tcp://"; 29 | addr += _ip; 30 | addr += ":"; 31 | addr += std::to_string(_port); 32 | int opnng = nng_req0_open(&_sock); 33 | opnng = nng_dialer_create(&_dailer, _sock, addr.c_str()); 34 | nng_dialer_start(_dailer, NNG_FLAG_NONBLOCK); 35 | SPDLOG_INFO("start nng request channel type {} main type {} on addr {}", 36 | magic_enum::enum_name(_channelType), magic_enum::enum_name(mainx), addr); 37 | } 38 | ~NNGReq() 39 | { 40 | nng_close(_sock); 41 | } 42 | template 43 | Rep sendReq(Req& req, AssType op) 44 | { 45 | int nnop = 0; 46 | Rep rep; 47 | string strreq = req.to_string(); 48 | auto reqhead = SMUtils::packcmdrep(_mainc, (short)(op) , strreq.length()); 49 | string packreq(reqhead.begin(), reqhead.end()); 50 | packreq += strreq; 51 | nnop = nng_send(_sock, &packreq[0], packreq.length(), 0); 52 | size_t recvlen = 0; 53 | char* recvbuf = NULL; 54 | nnop = nng_recv(_sock, &recvbuf, &recvlen, NNG_FLAG_ALLOC); 55 | auto header = parsecmd(string_view(recvbuf, 12)); 56 | string strbody{recvbuf+12, recvlen-12}; 57 | rep.parse(strbody); 58 | return rep; 59 | } 60 | nng_socket _sock; 61 | nng_dialer _dailer; 62 | string _ip; 63 | uint16_t _port; 64 | 65 | MainCmd _mainc; 66 | ChannelType _channelType; 67 | }; 68 | } 69 | 70 | 71 | //NNGDBReq 72 | -------------------------------------------------------------------------------- /conf/conf.toml: -------------------------------------------------------------------------------- 1 | #context const, current text should not be modified 2 | [log] 3 | [log.node] 4 | addr = "127.0.0.1" 5 | port = 8010 6 | [log.context] 7 | log_level_array = ["trace", "debug", "info", "warn", "error"] 8 | format = "[%H:%M:%S %z] [%n] [%^%L%$] [%!] [thread %t] %v" 9 | level = 1 # "debug" 10 | log_file = "server.log" 11 | log_file_path= "./" 12 | log_roll_size = 10000000 13 | 14 | [login] 15 | port = 10086 16 | tokenlife = 3600 17 | [guid] 18 | [guid.node] 19 | addr = "127.0.0.1" 20 | port = 8081 21 | 22 | [http] 23 | addr="127.0.0.1" 24 | port=10086 25 | rootPath="./dist" 26 | resRootPath="./dist" 27 | 28 | [jwt] 29 | issuer="alqaz" 30 | type="JWS" 31 | key="key" 32 | noAuthUrl=["/http/User/Regist"] 33 | 34 | 35 | 36 | [transport] 37 | addr="127.0.0.1" 38 | port=10087 39 | timeout=3600 40 | 41 | [nanomsg] 42 | [nanomsg.rep] 43 | addr="127.0.0.1" 44 | port=9001 45 | 46 | [dbmgr] 47 | databasetype=1 #0 postgresql; 1 sqlite3 48 | databasemodel=0 #embed; 1 alone 49 | databasedebug=true #true or false 50 | [dbmgr.sqlite] 51 | sqlitepath="conf" 52 | sqlitename = "db.db" 53 | [dbmgr.postgresql] 54 | dbname="demo" 55 | addr="127.0.0.1" 56 | port=5432 57 | user="postgres" 58 | pass="123456" 59 | useSSL=0 60 | [dbmgr.redis] 61 | addr="127.0.0.1" 62 | port=6379 63 | user="" 64 | pass="" 65 | useSSL=0 66 | [dbmgr.remote] 67 | addr="127.0.0.1" 68 | port=5400 69 | useSSL=0 70 | [dbmgr.remotenng] 71 | addr="127.0.0.1" 72 | port=5401 73 | useSSL=0 74 | [dbmgr.rremote] 75 | addr="127.0.0.1" 76 | port=5402 77 | useSSL=0 78 | [dbmgr.rremotenng] 79 | addr="127.0.0.1" 80 | port=5403 81 | useSSL=0 -------------------------------------------------------------------------------- /conf/dbCommon.sql: -------------------------------------------------------------------------------- 1 | 2 | DROP table if EXISTS users; 3 | DROP table if EXISTS token; 4 | DROP table if EXISTS sysadmin; 5 | 6 | create table users(name varchar(255), passwd varchar(255), phone varchar(64), expiredate datetime); 7 | create table token(name varchar(255), token varchar(255)); 8 | create table sysadmin(user varchar(255), phone varchar(255), admin varchar(255), role int); -------------------------------------------------------------------------------- /conf/dbTest.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/conf/dbTest.db -------------------------------------------------------------------------------- /conf/dbTest.h: -------------------------------------------------------------------------------- 1 | // generated by ./ddl2cpp ./dbTest.sql dbTest dbTest 2 | #ifndef DBTEST_DBTEST_H 3 | #define DBTEST_DBTEST_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace dbTest 10 | { 11 | namespace Forsumgroup_ 12 | { 13 | struct Name 14 | { 15 | struct _alias_t 16 | { 17 | static constexpr const char _literal[] = "name"; 18 | using _name_t = sqlpp::make_char_sequence; 19 | template 20 | struct _member_t 21 | { 22 | T name; 23 | T& operator()() { return name; } 24 | const T& operator()() const { return name; } 25 | }; 26 | }; 27 | using _traits = sqlpp::make_traits; 28 | }; 29 | struct Ed 30 | { 31 | struct _alias_t 32 | { 33 | static constexpr const char _literal[] = "ed"; 34 | using _name_t = sqlpp::make_char_sequence; 35 | template 36 | struct _member_t 37 | { 38 | T ed; 39 | T& operator()() { return ed; } 40 | const T& operator()() const { return ed; } 41 | }; 42 | }; 43 | using _traits = sqlpp::make_traits; 44 | }; 45 | struct Price 46 | { 47 | struct _alias_t 48 | { 49 | static constexpr const char _literal[] = "price"; 50 | using _name_t = sqlpp::make_char_sequence; 51 | template 52 | struct _member_t 53 | { 54 | T price; 55 | T& operator()() { return price; } 56 | const T& operator()() const { return price; } 57 | }; 58 | }; 59 | using _traits = sqlpp::make_traits; 60 | }; 61 | } // namespace Forsumgroup_ 62 | 63 | struct Forsumgroup: sqlpp::table_t 67 | { 68 | struct _alias_t 69 | { 70 | static constexpr const char _literal[] = "forsumgroup"; 71 | using _name_t = sqlpp::make_char_sequence; 72 | template 73 | struct _member_t 74 | { 75 | T forsumgroup; 76 | T& operator()() { return forsumgroup; } 77 | const T& operator()() const { return forsumgroup; } 78 | }; 79 | }; 80 | }; 81 | } // namespace dbTest 82 | #endif 83 | -------------------------------------------------------------------------------- /conf/dbTest.sql: -------------------------------------------------------------------------------- 1 | 2 | DROP table if EXISTS forsumgroup; 3 | 4 | create table forsumgroup(name varchar(255), ed datetime, price int); 5 | 6 | insert into forsumgroup values("alqaz", "2022/01/01", 1); 7 | insert into forsumgroup values("xyz", "2022/01/01", 2); 8 | insert into forsumgroup values("alqaz", "2022/01/02", 3); 9 | insert into forsumgroup values("xyz", "2022/01/02", 4); 10 | -------------------------------------------------------------------------------- /conf/main.py: -------------------------------------------------------------------------------- 1 | # 2 | #script for generate database and database tables 3 | 4 | import os; 5 | 6 | #not support recursive 7 | if __name__ == '__main__': 8 | for root, dirs, files in os.walk('./'): 9 | for file in files: 10 | curfile = os.path.join(root, file); 11 | nameArr = os.path.splitext(file); 12 | if curfile.endswith('.sql') == False: 13 | continue; 14 | os.system('python3 ./ddl2cpp {} {} {}'.format(curfile, nameArr[0], nameArr[0])); 15 | -------------------------------------------------------------------------------- /configs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(configs) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | FILE(GLOB_RECURSE MY_HEADERS "./*.h") 7 | source_group("Headers" FILES ${MY_HEADERS}) 8 | 9 | FILE(GLOB_RECURSE MY_INTERFACEHEADERS "../inc/networks/*.h") 10 | source_group("Headers" FILES ${MY_INTERFACEHEADERS}) 11 | 12 | add_definitions(-DBUILD_SHARED_LIBS -Dconfigs_EXPORTS) 13 | 14 | include_directories(../inc ../Utils) 15 | 16 | AUX_SOURCE_DIRECTORY(. A) 17 | 18 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 19 | link_libraries(Ws2_32) 20 | add_compile_options(/Zc:__cplusplus /Wv:19.27) 21 | ADD_LIBRARY(configs SHARED ${A}) 22 | set_target_properties(configs PROPERTIES VS_GLOBAL_VcpkgEnabled true) 23 | ELSE() 24 | ADD_LIBRARY(configs ${A}) 25 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 26 | 27 | 28 | 29 | 30 | 31 | include(GenerateExportHeader) 32 | generate_export_header(configs EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 33 | 34 | target_include_directories( 35 | configs SYSTEM 36 | PUBLIC 37 | "$" 38 | ) 39 | 40 | 41 | 42 | target_link_libraries(configs PRIVATE whereami PRIVATE toml11::toml11 PRIVATE Utils PRIVATE spdlog::spdlog ) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /configs/Configs.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "configsExport.h" 4 | 5 | #include "Structs.h" 6 | 7 | 8 | 9 | namespace SMCONF 10 | { 11 | 12 | CONFIGS_EXPORT bool SInitLog(string logfile, string loggername); 13 | CONFIGS_EXPORT bool SInitLog(); 14 | class CONFIGS_EXPORT Configs 15 | { 16 | public: 17 | static bool sInit(); 18 | static Configs& getInst2(); 19 | Configs(); 20 | ~Configs(); 21 | bool Parse(string path); 22 | DatabaseConfig& getDBConfig(); 23 | void setDBConfig(const DatabaseConfig& conf); 24 | HttpConfig& getHttpConfig(); 25 | LogConfig& getLogConfig(); 26 | TransportConfig& getTransportConfig(); 27 | NanoRepConf& getNngRepConfig(); 28 | JWTConf& getJWTConf(); 29 | TimeZoneConfig& getTimeZoneConfig(); 30 | bool isRouterNeedJwt(string_view url); 31 | 32 | //protected: 33 | 34 | //private: 35 | NanoRepConf _nanotcp; 36 | DatabaseConfig _dbconf; 37 | HttpConfig _http; 38 | LogConfig _log; 39 | TransportConfig _transport; 40 | JWTConf _jwt; 41 | string _path; 42 | TimeZoneConfig _tzconf; 43 | }; 44 | } 45 | 46 | #define CONFINST (SMCONF::Configs::getInst2()) 47 | -------------------------------------------------------------------------------- /configs/Routers.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "configsExport.h" 3 | #include "magic_enum.hpp" 4 | #include "templatefuncs.h" 5 | #include "asio/awaitable.hpp" 6 | #include "fmt/format.h" 7 | #include "Utils.h" 8 | #include "tsl/htrie_map.h" 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | using std::map; 18 | using std::string; 19 | using std::shared_ptr; 20 | using std::tuple; 21 | using std::make_tuple; 22 | using tsl::htrie_map; 23 | using std::list; 24 | 25 | typedef std::function(string&, string)> RouterFuncType; 26 | typedef shared_ptr ConRouterType; 27 | typedef map> RoutersType; 28 | typedef RoutersType* PtRoutersType; 29 | typedef RoutersType& RefRoutersType; 30 | typedef htrie_map CodeRoutersType; 31 | 32 | typedef map* PtHtmlRoutersType; 33 | 34 | typedef map TransRouterElement; 35 | typedef TransRouterElement& RefTransRouterElement; 36 | typedef TransRouterElement* PtTransRouterElement; 37 | 38 | typedef map TransRoutersType; 39 | 40 | namespace SMCONF 41 | { 42 | 43 | 44 | CONFIGS_EXPORT bool addRouter(HtmlBodyType bodyType, tuple path, ConRouterType func); 45 | 46 | CONFIGS_EXPORT bool addRouterJson(tuple path, ConRouterType func); 47 | 48 | CONFIGS_EXPORT bool addRouterHtml(tuple path, ConRouterType func); 49 | 50 | CONFIGS_EXPORT bool addRouterTrans(MainCmd mainc, int assc, ConRouterType func); 51 | 52 | 53 | CONFIGS_EXPORT ConRouterType getRouterTrans(MainCmd mainc, int assc); 54 | 55 | CONFIGS_EXPORT PtTransRouterElement getRouterTransByMainC(MainCmd mainc); 56 | 57 | CONFIGS_EXPORT PtTransRouterElement getRouterTransByMainC(int mainc); 58 | 59 | CONFIGS_EXPORT PtHtmlRoutersType getRouters(); 60 | 61 | CONFIGS_EXPORT PtRoutersType getRouters(HtmlBodyType body); 62 | 63 | CONFIGS_EXPORT CodeRoutersType getCodeRouters(); 64 | 65 | template 66 | tuple combinePath(MainC mainc, AssC assc) 67 | { 68 | auto v = SMUtils::packheads(magic_enum::enum_integer(mainc), magic_enum::enum_integer(assc)); 69 | 70 | return make_tuple(std::move(v), fmt::format("/{}/{}", magic_enum::enum_name(mainc), magic_enum::enum_name(assc))); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /configs/conf.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "conf.h" 3 | #include "Configs.h" 4 | #include "whereami.h" 5 | namespace SMCONF 6 | { 7 | bool sInit(const char* path) 8 | { 9 | bool bret{ false }; 10 | 11 | string exepath; 12 | exepath.resize(1024); 13 | int outlen{ 0 }; 14 | auto mypath = wai_getModulePath(exepath.data(), exepath.size(), &outlen); 15 | 16 | if (!path) 17 | { 18 | return bret; 19 | } 20 | bret = Configs::sInit(); 21 | if (!bret) 22 | { 23 | return bret; 24 | } 25 | string strpath = path; 26 | bret = SMCONF::Configs::getInst2().Parse(strpath); 27 | return bret; 28 | } 29 | 30 | DatabaseConfig* getDBConfig() 31 | { 32 | return &SMCONF::Configs::getInst2().getDBConfig(); 33 | } 34 | 35 | void setDBConfig(const DatabaseConfig* conf) 36 | { 37 | assert(conf != nullptr); 38 | SMCONF::Configs::getInst2().setDBConfig(*conf); 39 | } 40 | 41 | HttpConfig* getHttpConfig() 42 | { 43 | return &SMCONF::Configs::getInst2().getHttpConfig(); 44 | } 45 | 46 | LogConfig* getLogConfig() 47 | { 48 | return &SMCONF::Configs::getInst2().getLogConfig(); 49 | } 50 | 51 | TransportConfig* getTransportConfig() 52 | { 53 | return &SMCONF::Configs::getInst2().getTransportConfig(); 54 | } 55 | 56 | NanoRepConf* getNngRepConfig() 57 | { 58 | return &SMCONF::Configs::getInst2().getNngRepConfig(); 59 | } 60 | 61 | JWTConf* getJWTConf() 62 | { 63 | return &SMCONF::Configs::getInst2().getJWTConf(); 64 | } 65 | 66 | TimeZoneConfig* getTimeZoneConfig() 67 | { 68 | return &SMCONF::Configs::getInst2().getTimeZoneConfig(); 69 | } 70 | 71 | bool isRouterNeedJwt(string_view url) 72 | { 73 | return SMCONF::Configs::getInst2().isRouterNeedJwt(url); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /configs/conf.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "configsExport.h" 3 | #include "Structs.h" 4 | #include 5 | #include 6 | namespace SMCONF 7 | { 8 | CONFIGS_EXPORT bool sInit(const char* path); 9 | CONFIGS_EXPORT DatabaseConfig* getDBConfig(); 10 | CONFIGS_EXPORT void setDBConfig(const DatabaseConfig* conf); 11 | CONFIGS_EXPORT HttpConfig* getHttpConfig(); 12 | CONFIGS_EXPORT LogConfig* getLogConfig(); 13 | CONFIGS_EXPORT TransportConfig* getTransportConfig(); 14 | CONFIGS_EXPORT NanoRepConf* getNngRepConfig(); 15 | CONFIGS_EXPORT JWTConf* getJWTConf(); 16 | CONFIGS_EXPORT TimeZoneConfig* getTimeZoneConfig(); 17 | CONFIGS_EXPORT bool isRouterNeedJwt(string_view url); 18 | } 19 | -------------------------------------------------------------------------------- /database/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(database) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_definitions(-DBUILD_SHARED_LIBS -Ddatabase_EXPORTS) 7 | 8 | include_directories() 9 | 10 | FILE(GLOB_RECURSE CUR_HEADER "./*.h") 11 | source_group("IncCur" FILES ${CUR_HEADER}) 12 | 13 | FILE(GLOB_RECURSE COMMN_HEADER "../inc/*.h") 14 | source_group("IncComm" FILES ${COMMN_HEADER}) 15 | 16 | #FILE(GLOB_RECURSE MY_INTERFACEHEADERS "../../inc/networks/*.h") 17 | #source_group("Headers" FILES ${MY_INTERFACEHEADERS}) 18 | 19 | 20 | 21 | include_directories(../conf ../inc ../Utils ../hotupdate ../networkinterface ../configs) 22 | 23 | AUX_SOURCE_DIRECTORY(. A) 24 | 25 | 26 | 27 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 28 | ADD_LIBRARY(database SHARED ${A} ${CUR_HEADER} ${COMMN_HEADER}) 29 | set_target_properties(database PROPERTIES VS_GLOBAL_VcpkgEnabled true) 30 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 31 | add_definitions(-w) 32 | add_compile_options(-fcoroutines) 33 | link_libraries(dl) 34 | ADD_LIBRARY(database ${A} ) 35 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 36 | ADD_LIBRARY(database ${A} ) 37 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 38 | 39 | 40 | 41 | generate_export_header(database EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 42 | 43 | 44 | 45 | target_link_libraries(database PRIVATE nng::nngpp PRIVATE ZLIB::ZLIB PRIVATE OpenSSL::SSL OpenSSL::Crypto PRIVATE unofficial::sqlite3::sqlite3 PRIVATE sqlpp11::sqlpp11 PRIVATE hashlibrary PRIVATE Utils PRIVATE configs PRIVATE networkinterface PRIVATE spdlog::spdlog PRIVATE hotupdate PRIVATE date::date date::date-tz) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /database/CommonDBProcess.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "TransCmdTag.h" 5 | #include "templatefuncs.h" 6 | #include "database.h" 7 | #include "enums.h" 8 | #include "coros.h" 9 | #include "reqreps.h" 10 | #include 11 | 12 | using std::string; 13 | using std::string_view; 14 | 15 | namespace SMDB 16 | { 17 | class DATABASE_EXPORT CommonDBProcess : public TransCmdTag 18 | { 19 | public: 20 | BEGIN_ROUTER_MAP_DB(MainCmd::DBQuery); 21 | ROUTER_DB3(&CommonDBProcess::sregist, AssDB::Regist); 22 | ROUTER_DB3(&CommonDBProcess::slogin, AssDB::Login); 23 | ROUTER_DB3(&CommonDBProcess::sgetAllUsers, AssDB::getAllUsers); 24 | ROUTER_DB3(&CommonDBProcess::saddSysAdmin, AssDB::AddSysAdmin); 25 | ROUTER_DB3(&CommonDBProcess::sgetSysAdmin, AssDB::GetSysAdmin); 26 | ROUTER_DB3(&CommonDBProcess::sdelSysAdmin, AssDB::DelSysAdmin); 27 | ROUTER_DB3(&CommonDBProcess::sgetUserGenInfo, AssDB::GetUserGenInfo); 28 | END_ROUTER_MAP_DB; 29 | public: 30 | 31 | RegistRep sregist(RegistReq req); 32 | LoginRep slogin(LoginReq req); 33 | getAllUserRep sgetAllUsers(getAllUserReq req); 34 | addSysAdminRep saddSysAdmin(addSysAdminReq req); 35 | getSysAdminRep sgetSysAdmin(getSysAdminReq req); 36 | delSysAdminRep sdelSysAdmin(delSysAdminReq req); 37 | getUserGenInfoRep sgetUserGenInfo(getUserGenInfoReq req); 38 | virtual void init(ServeMode mode) override; 39 | 40 | private: 41 | 42 | }; 43 | } 44 | -------------------------------------------------------------------------------- /database/DBConnectManager.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "DBConnectManager.h" 3 | #include "fmt/format.h" 4 | #include "Configs.h" 5 | #include "conf.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using std::exception; 13 | using std::runtime_error; 14 | using std::to_string; 15 | using std::make_shared; 16 | using std::string; 17 | 18 | namespace SMDB 19 | { 20 | 21 | static shared_ptr _inst = nullptr; 22 | 23 | shared_ptr DBConnectManager::getInst() 24 | { 25 | if (_inst == nullptr) 26 | { 27 | auto dbconf = SMCONF::getDBConfig()->_tcpClient; 28 | _inst = shared_ptr(new DBConnectManager(dbconf._ip, dbconf._port)); 29 | auto nngconf = SMCONF::getDBConfig()->_nngClient; 30 | } 31 | return _inst; 32 | } 33 | 34 | SMDB::DBConnectManager& DBConnectManager::getInst2() 35 | { 36 | if (_inst == nullptr) 37 | { 38 | auto dbconf = SMCONF::getDBConfig()->_tcpClient; 39 | _inst = shared_ptr(new DBConnectManager(dbconf._ip, dbconf._port)); 40 | } 41 | return *_inst; 42 | } 43 | 44 | DBConnectManager::DBConnectManager(string ip, uint16_t port) 45 | : _ip(ip) 46 | , _port(port) 47 | { 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /database/DBConnectNNG.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "DBConnectNNG.h" 3 | #include "DBConnectManager.h" 4 | #include "fmt/format.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using std::exception; 11 | using std::runtime_error; 12 | using std::to_string; 13 | namespace SMDB 14 | { 15 | DBConnectNNG::DBConnectNNG( string ip, uint16_t port) 16 | :_ip(ip) 17 | , _port(port) 18 | { 19 | auto addr = fmt::format("tcp://{}:{}", ip, port); 20 | _channels = make_shared>(string_view(addr), MainCmd::DBQuery); 21 | } 22 | 23 | 24 | DBConnectNNG::~DBConnectNNG() { 25 | 26 | } 27 | 28 | asio::awaitable DBConnectNNG::_execQuery(shared_ptr req, string& rep) 29 | { 30 | bool bret{ false }; 31 | assert(_channels); 32 | auto w = co_await _channels->getChannel(); 33 | if (w == nullptr) 34 | { 35 | co_return false; 36 | } 37 | BEGIN_ASIO; 38 | auto ptrep = co_await w->reqrep(req); 39 | if (ptrep != nullptr) 40 | { 41 | rep = *ptrep; 42 | } 43 | END_ASIO; 44 | co_return bret; 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /database/DBConnectNNG.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "databaseExport.h" 3 | #include "enums.h" 4 | #include "Utils.h" 5 | #include "nng/nng.h" 6 | #include "nng/protocol/reqrep0/req.h" 7 | #include "spdlog/spdlog.h" 8 | #include "spdlog/fmt/bin_to_hex.h" 9 | #include "enums.h" 10 | #include "../networkinterface/nngs/AsynReq.h" 11 | #include "PackDealerMainSub.h" 12 | #include "myconcept.h" 13 | #include "../networkinterface/nngs/ReqManager.h" 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | using std::string; 21 | using std::string_view; 22 | using std::shared_ptr; 23 | using std::make_shared; 24 | 25 | namespace SMDB 26 | { 27 | class DATABASE_EXPORT DBConnectNNG 28 | { 29 | public: 30 | DBConnectNNG( string ip, uint16_t port); 31 | ~DBConnectNNG(); 32 | asio::awaitable _execQuery(shared_ptr req, string& rep); 33 | 34 | private: 35 | string _ip; 36 | uint16_t _port; 37 | shared_ptr < SMNetwork::ReqManager> _channels; 38 | MainCmd _mainc = MainCmd::DBQuery; 39 | }; 40 | } 41 | -------------------------------------------------------------------------------- /database/DBConnectTCP.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "DBConnectTCP.h" 3 | #include "fmt/format.h" 4 | 5 | #include "PackUnpackManager.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using std::exception; 13 | using std::runtime_error; 14 | using std::to_string; 15 | namespace SMDB 16 | { 17 | DBConnectTCP::DBConnectTCP( string ip, uint16_t port) 18 | : _ip(ip) 19 | , _port(port) 20 | { 21 | _client = make_shared>(_ip, _port, MainCmd::DBQuery); 22 | } 23 | 24 | DBConnectTCP::~DBConnectTCP() { 25 | 26 | } 27 | 28 | 29 | asio::awaitable DBConnectTCP::_execQuery(string& req, string& rep) 30 | { 31 | bool succ = false; 32 | uint8_t trycount = 0; 33 | BEGIN_ASIO; 34 | while (++trycount<3) 35 | { 36 | auto ret = co_await _client->getChannel(); 37 | if (ret == nullptr) 38 | { 39 | co_return succ; 40 | } 41 | auto strreq = make_shared(req); 42 | auto ret2 = co_await ret->reqrep(strreq); 43 | if (ret2) 44 | { 45 | rep = *ret2; 46 | succ = true; 47 | } 48 | if (succ) 49 | { 50 | _client->_returnChannel(ret); 51 | break; 52 | } 53 | ++trycount; 54 | } 55 | END_ASIO; 56 | co_return succ; 57 | } 58 | } -------------------------------------------------------------------------------- /database/DBConnectTCP.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "databaseExport.h" 3 | #include "enums.h" 4 | #include "../networkinterface/socket/TcpClientCombine.h" 5 | #include "Utils.h" 6 | #include "spdlog/spdlog.h" 7 | #include "spdlog/fmt/bin_to_hex.h" 8 | #include "myconcept.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using std::string; 16 | using std::string_view; 17 | 18 | namespace SMDB 19 | { 20 | class dealDBMsgPlaceHolder 21 | { 22 | public: 23 | asio::awaitable dealMsg(string_view str, uint16_t assc) 24 | { 25 | co_return "placeholder"; 26 | } 27 | MainCmd getMainCmd() { 28 | return MainCmd::MainCmdBegin; 29 | } 30 | }; 31 | 32 | class DATABASE_EXPORT DBConnectTCP 33 | { 34 | public: 35 | DBConnectTCP( string ip, uint16_t port); 36 | ~DBConnectTCP(); 37 | asio::awaitable _execQuery(string& req, string& succ); 38 | private: 39 | string _ip; 40 | uint16_t _port; 41 | int16_t _timeoutMiseconds = 5000; 42 | shared_ptr> _client; 43 | MainCmd _mainc = MainCmd::DBQuery; 44 | }; 45 | } 46 | -------------------------------------------------------------------------------- /database/DBNNg.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "DBNNg.h" 3 | #include "enums.h" 4 | 5 | namespace SMDB 6 | { 7 | 8 | DBNNg::DBNNg(string ip, uint16_t port):_addr(ip), _port(port)//,_sock(nullptr) 9 | { 10 | } 11 | 12 | void DBNNg::init() 13 | { 14 | _sock = make_shared>(_addr, _port, MainCmd::DBQuery); 15 | bool bret{ false }; 16 | bret = _sock->init(); 17 | assert(bret); 18 | if (!bret) 19 | { 20 | SPDLOG_WARN("init database nng rep failed on ip {}, port {}", _addr, _port); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /database/DBNNg.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "databaseExport.h" 4 | #include "../networkinterface/nngs/RepManager.h" 5 | #include 6 | #include 7 | #include 8 | 9 | using std::string; 10 | using std::shared_ptr; 11 | 12 | namespace SMDB 13 | { 14 | class DBNNg 15 | { 16 | public: 17 | DBNNg(string ip, uint16_t port); 18 | void init(); 19 | private: 20 | string _addr; 21 | uint16_t _port; 22 | shared_ptr> _sock; 23 | }; 24 | } 25 | -------------------------------------------------------------------------------- /database/DBTcp.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "DBTcp.h" 3 | 4 | using std::make_shared; 5 | 6 | namespace SMDB 7 | { 8 | 9 | DBTcp::DBTcp(string ip, uint16_t port):_addr(ip), _port(port), _tcp(nullptr) 10 | { 11 | 12 | } 13 | 14 | void DBTcp::init() 15 | { 16 | _tcp = make_shared>(_addr, _port, MainCmd::DBQuery); 17 | _tcp->init(); 18 | } 19 | 20 | void DBTcp::stop() 21 | { 22 | SPDLOG_INFO("try stop db tcp server instance"); 23 | if (_tcp) 24 | { 25 | _tcp->stop(); 26 | } 27 | else 28 | { 29 | SPDLOG_WARN("db tcp server instance is null"); 30 | } 31 | } 32 | 33 | bool DBTcp::stoped() 34 | { 35 | if (_tcp) 36 | { 37 | return _tcp->stoped(); 38 | } 39 | return true; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /database/DBTcp.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "databaseExport.h" 4 | #include "enums.h" 5 | #include "../networkinterface/socket/TcpServerCombine.h" 6 | #include 7 | #include 8 | 9 | 10 | using std::string; 11 | using std::shared_ptr; 12 | 13 | namespace SMDB 14 | { 15 | class DATABASE_EXPORT DBTcp 16 | { 17 | public: 18 | DBTcp(string ip, uint16_t port); 19 | void init(); 20 | void stop(); 21 | bool stoped(); 22 | private: 23 | string _addr; 24 | uint16_t _port; 25 | shared_ptr> _tcp; 26 | }; 27 | } 28 | -------------------------------------------------------------------------------- /database/DBs.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "Utils.h" 3 | #include "DBs.h" 4 | #include "enums.h" 5 | #include "DBManager.h" 6 | #include "spdlog/spdlog.h" 7 | 8 | #include "PackDealerMainSub.h" 9 | #include "../networkinterface/socket/TcpServer.h" 10 | #include "../networkinterface/nngs/AsynRep.h" 11 | #include "DBNNg.h" 12 | #include "DBTcp.h" 13 | #include "conf.h" 14 | 15 | #include 16 | 17 | using std::make_shared; 18 | using std::shared_ptr; 19 | 20 | using std::string; 21 | using std::string_view; 22 | 23 | 24 | 25 | namespace SMDB 26 | { 27 | static shared_ptr _inst = { nullptr }; 28 | static shared_ptr> _rtcp = { nullptr }; 29 | static shared_ptr _rnng = { nullptr }; 30 | 31 | static shared_ptr _dealer = { nullptr }; 32 | 33 | static shared_ptr _nng = { nullptr }; 34 | static shared_ptr _tcp = { nullptr }; 35 | DBs& DBs::getInst() 36 | { 37 | if (!_inst) 38 | { 39 | _inst = make_shared(); 40 | } 41 | return *_inst; 42 | } 43 | 44 | DBs::DBs() 45 | { 46 | // may be need split another function. 47 | _dealer = std::shared_ptr(new SMNetwork::PackDealerMainSub(MainCmd::DBQuery, ChannelType::DBServer)); 48 | } 49 | 50 | void DBs::init() 51 | { 52 | auto conftcp = SMCONF::getDBConfig()->_tcpServer; 53 | _tcp = make_shared(conftcp._ip, conftcp._port); 54 | _tcp->init(); 55 | 56 | auto confnng = SMCONF::getDBConfig()->_nngServer; 57 | _nng = make_shared(confnng._ip, confnng._port); 58 | _nng->init(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /database/DBs.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | /** 5 | * @file DBs.h classes for deal connection from all other app instance for request databases. 6 | * @author your name (you@domain.com) 7 | * @brief 8 | * @version 0.1 9 | * @date 2022-01-04 10 | * 11 | * @copyright Copyright (c) 2022 12 | * 13 | */ 14 | 15 | 16 | namespace SMDB 17 | { 18 | class DBs 19 | { 20 | public: 21 | static DBs& getInst(); 22 | public: 23 | DBs(); 24 | void init(); 25 | 26 | }; 27 | } 28 | -------------------------------------------------------------------------------- /database/DealAuth.cpp: -------------------------------------------------------------------------------- 1 | #include "DealAuth.h" 2 | #include "reqreps.h" 3 | #include "enums.h" 4 | #include "Utils.h" 5 | #include "DBManager.h" 6 | #include "spdlog/spdlog.h" 7 | #include "networkinterface.h" 8 | #include "DBConnectManager.h" 9 | #include "dbCommon.h" 10 | 11 | namespace SMDB 12 | { 13 | DealAuth::DealAuth() 14 | { 15 | 16 | } 17 | 18 | DealAuth::~DealAuth() 19 | { 20 | 21 | } 22 | 23 | RegistRep DealAuth::lregist(RegistReq req) 24 | { 25 | RegistRep rep; 26 | rep.code = magic_enum::enum_integer(statusCode::needNextRouter); 27 | return rep; 28 | } 29 | 30 | LoginRep DealAuth::llogin(LoginReq req) 31 | { 32 | LoginRep rep; 33 | rep.code = magic_enum::enum_integer(statusCode::needNextRouter); 34 | return rep; 35 | } 36 | 37 | addSysAdminRep DealAuth::laddSysAdmin(addSysAdminReq req) 38 | { 39 | addSysAdminRep rep; 40 | auto bret = SMNetwork::isjwttokenright(string_view{ req.token }); 41 | if (!bret) 42 | { 43 | rep.code = magic_enum::enum_integer(statusCode::jwtCheckFailed); 44 | } 45 | return rep; 46 | } 47 | 48 | getSysAdminRep DealAuth::lgetSysAdmin(getSysAdminReq req) 49 | { 50 | getSysAdminRep rep; 51 | auto bret = SMNetwork::isjwttokenright(string_view{ req.token }); 52 | if (!bret) 53 | { 54 | rep.code = magic_enum::enum_integer(statusCode::jwtCheckFailed); 55 | } 56 | return rep; 57 | } 58 | 59 | delSysAdminRep DealAuth::ldelSysAdmin(delSysAdminReq req) 60 | { 61 | delSysAdminRep rep; 62 | auto bret = SMNetwork::isjwttokenright(string_view{ req.token }); 63 | if (!bret) 64 | { 65 | rep.code = magic_enum::enum_integer(statusCode::jwtCheckFailed); 66 | } 67 | return rep; 68 | } 69 | 70 | getUserGenInfoRep DealAuth::lgetUserGenInfo(getUserGenInfoReq req) 71 | { 72 | getUserGenInfoRep rep; 73 | auto bret = SMNetwork::isjwttokenright(string_view{ req.token }); 74 | if (!bret) 75 | { 76 | rep.code = magic_enum::enum_integer(statusCode::jwtCheckFailed); 77 | } 78 | return rep; 79 | } 80 | 81 | 82 | 83 | void DealAuth::init(ServeMode mode) 84 | { 85 | 86 | } 87 | } 88 | 89 | 90 | -------------------------------------------------------------------------------- /database/DealAuth.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "coros.h" 4 | #include "database.h" 5 | #include "wss.h" 6 | #include "Utils.h" 7 | #include "reqreps.h" 8 | #include "HttpCmdTag.h" 9 | #include "taskflow/taskflow.hpp" 10 | #include 11 | #include 12 | #include 13 | 14 | namespace SMDB 15 | { 16 | 17 | class DBManager; 18 | 19 | class DealAuth : public HttpCmdTag 20 | { 21 | public: 22 | DealAuth(); 23 | virtual ~DealAuth(); 24 | public: 25 | BEGIN_ROUTER_MAP(MainCmd::User); 26 | ROUTER_NEED_CACHE_DB_JSON3(&DealAuth::lregist, AssAuth::Regist, AssDB::Regist); 27 | ROUTER_NEED_CACHE_DB_JSON3(&DealAuth::llogin, AssAuth::Login, AssDB::Login); 28 | ROUTER_NEED_CACHE_DB_JSON3(&DealAuth::laddSysAdmin, AssAuth::AddSysAdmin, AssDB::AddSysAdmin); 29 | ROUTER_NEED_CACHE_DB_JSON3(&DealAuth::lgetSysAdmin, AssAuth::GetSysAdmin, AssDB::GetSysAdmin); 30 | ROUTER_NEED_CACHE_DB_JSON3(&DealAuth::ldelSysAdmin, AssAuth::DelSysAdmin, AssDB::DelSysAdmin); 31 | ROUTER_NEED_CACHE_DB_JSON3(&DealAuth::lgetUserGenInfo, AssAuth::GetUserGenInfo, AssDB::GetUserGenInfo); 32 | END_ROUTER_MAP; 33 | 34 | 35 | RegistRep lregist(RegistReq req); 36 | LoginRep llogin(LoginReq req); 37 | addSysAdminRep laddSysAdmin(addSysAdminReq req); 38 | getSysAdminRep lgetSysAdmin(getSysAdminReq req); 39 | delSysAdminRep ldelSysAdmin(delSysAdminReq req); 40 | getUserGenInfoRep lgetUserGenInfo(getUserGenInfoReq req); 41 | 42 | 43 | 44 | void init(ServeMode mode) override; 45 | 46 | }; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /database/DealEcho.cpp: -------------------------------------------------------------------------------- 1 | #include "DealEcho.h" 2 | #include "reqreps.h" 3 | #include "enums.h" 4 | #include "Utils.h" 5 | #include "DBManager.h" 6 | #include "spdlog/spdlog.h" 7 | #include "networkinterface.h" 8 | 9 | namespace SMDB 10 | { 11 | DealEcho::~DealEcho() 12 | { 13 | 14 | } 15 | 16 | EchoRep DealEcho::lEcho(EchoReq req) 17 | { 18 | EchoRep rep; 19 | 20 | return rep; 21 | } 22 | 23 | 24 | 25 | void DealEcho::init(ServeMode mode) { } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /database/DealEcho.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "database.h" 4 | #include "Utils.h" 5 | #include "reqreps.h" 6 | #include "HttpCmdTag.h" 7 | #include 8 | 9 | namespace SMDB 10 | { 11 | 12 | class DBManager; 13 | 14 | class DealEcho : public HttpCmdTag 15 | { 16 | public: 17 | BEGIN_ROUTER_MAP(MainCmd::Echo); 18 | ROUTER_NEED_CACHE_JSON3(&DealEcho::lEcho, AssEcho::Echo, AssDB::DBEcho); 19 | END_ROUTER_MAP; 20 | virtual ~DealEcho(); 21 | 22 | EchoRep lEcho(EchoReq req); 23 | 24 | 25 | void init(ServeMode mode) override; 26 | }; 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /database/UserManager.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "UserManager.h" 3 | 4 | #include 5 | #include 6 | 7 | using std::shared_ptr; 8 | using std::make_shared; 9 | using std::unique_ptr; 10 | using std::make_unique; 11 | using std::map; 12 | using std::string; 13 | 14 | namespace SMDB 15 | { 16 | static UserManager _inst; 17 | static map> _phone2Info; 18 | UserManager& UserManager::getInst() 19 | { 20 | return _inst; 21 | } 22 | 23 | UserGenInfoItem* UserManager::getUserInfo(string& phone) 24 | { 25 | UserGenInfoItem* pret{ nullptr }; 26 | auto it = _phone2Info.find(phone); 27 | if (it != _phone2Info.end()) 28 | { 29 | pret = it->second.get(); 30 | } 31 | return pret; 32 | } 33 | 34 | bool UserManager::addUserInfo(string& phone, UserGenInfoItem& info) 35 | { 36 | bool bret{ true }; 37 | auto it = _phone2Info.find(phone); 38 | if (it != _phone2Info.end()) 39 | { 40 | bret = false; 41 | it->second = make_unique(info); 42 | } 43 | else 44 | { 45 | _phone2Info[phone] = make_unique(info); 46 | } 47 | return bret; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /database/UserManager.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "reqreps.h" 5 | 6 | #include 7 | 8 | #define UMR (SMDB::UserManager::getInst()) 9 | 10 | using std::string; 11 | 12 | namespace SMDB 13 | { 14 | 15 | class UserManager 16 | { 17 | public: 18 | static UserManager& getInst(); 19 | 20 | public: 21 | UserGenInfoItem* getUserInfo(string& phone); 22 | bool addUserInfo(string& phone, UserGenInfoItem& info); 23 | }; 24 | } 25 | -------------------------------------------------------------------------------- /database/database.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "databaseExport.h" 3 | #include "date/tz.h" 4 | #include 5 | #include 6 | #include 7 | using std::shared_ptr; 8 | using std::string_view; 9 | using std::string; 10 | 11 | typedef std::function BeforeQueryFuncType; 12 | typedef std::function AfterQueryFuncType; 13 | namespace SMDB 14 | { 15 | DATABASE_EXPORT bool beforeQuery(string_view token); 16 | DATABASE_EXPORT void afterQuery(); 17 | 18 | DATABASE_EXPORT bool setDefaultTimeZone(string tz); 19 | DATABASE_EXPORT const string& getDefaultStrTimeZone(); 20 | DATABASE_EXPORT const date::time_zone* getDefaultTimeZone(); 21 | 22 | DATABASE_EXPORT void addBeforeQuery(shared_ptr func); 23 | DATABASE_EXPORT void addAfterQuery(shared_ptr < AfterQueryFuncType> func); 24 | DATABASE_EXPORT void init(bool dbnode=false); 25 | } 26 | -------------------------------------------------------------------------------- /db.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/db.db -------------------------------------------------------------------------------- /demo/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(demo) 3 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 4 | add_definitions(-D NN_HAVE_WINDOWS) 5 | add_definitions(-D _CRT_SECURE_NO_WARNINGS) 6 | link_libraries(Ws2_32 Synchronization )#sqlpp11-connector-postgresql) 7 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 8 | link_directories(/home/ubuntu/vcpkg/installed/x64-linux/lib) 9 | link_libraries(backtrace ssl crypto z uring sqlite3)# sqlpp11-connector-postgresql sqlpp11-connector-postgresql pq) 10 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 11 | 12 | include_directories(../configs ../Utils ../hotupdate ../database ../networkinterface ../inc) 13 | 14 | AUX_SOURCE_DIRECTORY(. A) 15 | 16 | 17 | 18 | add_executable(demo ${A} ) 19 | 20 | target_link_libraries(demo PRIVATE whereami PRIVATE date::date date::date-tz PRIVATE spdlog::spdlog PRIVATE hashlibrary PRIVATE ZLIB::ZLIB PRIVATE OpenSSL::SSL PRIVATE OpenSSL::Crypto PRIVATE $,uv_a,uv> PRIVATE Utils PRIVATE configs PRIVATE database PRIVATE hotupdate PRIVATE networkinterface PRIVATE Taskflow::Taskflow PRIVATE nng::nng PRIVATE fmt::fmt PRIVATE efsw::efsw) 21 | 22 | 23 | set_target_properties(demo PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/config") 24 | 25 | set_target_properties(demo PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") 26 | set_target_properties(demo PROPERTIES VS_GLOBAL_VcpkgEnabled true) 27 | -------------------------------------------------------------------------------- /endian-master.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/endian-master.zip -------------------------------------------------------------------------------- /hashlibrary/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.22) 2 | PROJECT(hashlibrary) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_definitions(-DBUILD_SHARED_LIBS -Dhashlibrary_EXPORTS) 7 | 8 | #include_directories(../inc/${CMAKE_SYSTEM_NAME}/export_flags) 9 | 10 | include(GenerateExportHeader) 11 | 12 | FILE(GLOB_RECURSE CUR_HEADER "./*.h") 13 | source_group("IncCur" FILES ${CUR_HEADER}) 14 | 15 | FILE(GLOB_RECURSE COMMN_HEADER "../inc/*.h") 16 | source_group("IncComm" FILES ${COMMN_HEADER}) 17 | 18 | #FILE(GLOB_RECURSE MY_INTERFACEHEADERS "../../inc/networks/*.h") 19 | #source_group("Headers" FILES ${MY_INTERFACEHEADERS}) 20 | 21 | AUX_SOURCE_DIRECTORY(. A) 22 | 23 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 24 | ADD_LIBRARY(hashlibrary SHARED ${A} ${CUR_HEADER} ${COMMN_HEADER}) 25 | set_target_properties(hashlibrary PROPERTIES VS_GLOBAL_VcpkgEnabled true) 26 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 27 | add_definitions(-w) 28 | ADD_LIBRARY(hashlibrary ${A} ) 29 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 30 | ADD_LIBRARY(hashlibrary ${A} ) 31 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 32 | 33 | 34 | 35 | generate_export_header(hashlibrary EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 36 | 37 | 38 | 39 | #target_link_libraries(HASHLIB PRIVATE Utils PRIVATE configs PRIVATE networkinterface PRIVATE spdlog::spdlog PRIVATE hotupdate) 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /hashlibrary/LICENSE: -------------------------------------------------------------------------------- 1 | zlib License 2 | 3 | Copyright (c) 2014,2015 Stephan Brumme 4 | 5 | This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. 6 | Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 7 | 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 8 | If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 9 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 10 | 3. This notice may not be removed or altered from any source distribution. 11 | -------------------------------------------------------------------------------- /hashlibrary/crc32.h: -------------------------------------------------------------------------------- 1 | // ////////////////////////////////////////////////////////// 2 | // crc32.h 3 | // Copyright (c) 2014,2015 Stephan Brumme. All rights reserved. 4 | // see http://create.stephan-brumme.com/disclaimer.html 5 | // 6 | 7 | #pragma once 8 | 9 | //#include "hash.h" 10 | #include 11 | #include "hashlibraryExport.h" 12 | // define fixed size integer types 13 | #ifdef _MSC_VER 14 | // Windows 15 | typedef unsigned __int8 uint8_t; 16 | typedef unsigned __int32 uint32_t; 17 | #else 18 | // GCC 19 | #include 20 | #endif 21 | 22 | 23 | /// compute CRC32 hash, based on Intel's Slicing-by-8 algorithm 24 | /** Usage: 25 | CRC32 crc32; 26 | std::string myHash = crc32("Hello World"); // std::string 27 | std::string myHash2 = crc32("How are you", 11); // arbitrary data, 11 bytes 28 | 29 | // or in a streaming fashion: 30 | 31 | CRC32 crc32; 32 | while (more data available) 33 | crc32.add(pointer to fresh data, number of new bytes); 34 | std::string myHash3 = crc32.getHash(); 35 | 36 | Note: 37 | You can find code for the faster Slicing-by-16 algorithm on my website, too: 38 | http://create.stephan-brumme.com/crc32/ 39 | Its unrolled version is about twice as fast but its look-up table doubled in size as well. 40 | */ 41 | class HASHLIBRARY_EXPORT CRC32 //: public Hash 42 | { 43 | public: 44 | /// hash is 4 bytes long 45 | enum { HashBytes = 4 }; 46 | 47 | /// same as reset() 48 | CRC32(); 49 | 50 | /// compute CRC32 of a memory block 51 | std::string operator()(const void* data, size_t numBytes); 52 | /// compute CRC32 of a string, excluding final zero 53 | std::string operator()(const std::string& text); 54 | 55 | /// add arbitrary number of bytes 56 | void add(const void* data, size_t numBytes); 57 | 58 | /// return latest hash as 8 hex characters 59 | std::string getHash(); 60 | /// return latest hash as bytes 61 | void getHash(unsigned char buffer[HashBytes]); 62 | 63 | /// restart 64 | void reset(); 65 | 66 | private: 67 | /// hash 68 | uint32_t m_hash; 69 | }; 70 | 71 | -------------------------------------------------------------------------------- /hashlibrary/hash.h: -------------------------------------------------------------------------------- 1 | // ////////////////////////////////////////////////////////// 2 | // hash.h 3 | // Copyright (c) 2014,2015 Stephan Brumme. All rights reserved. 4 | // see http://create.stephan-brumme.com/disclaimer.html 5 | // 6 | 7 | #pragma once 8 | 9 | #include "hashlibraryExport.h" 10 | #include 11 | 12 | /// abstract base class 13 | class HASHLIBRARY_EXPORT Hash 14 | { 15 | public: 16 | /// compute hash of a memory block 17 | virtual std::string operator()(const void* data, size_t numBytes) = 0; 18 | /// compute hash of a string, excluding final zero 19 | virtual std::string operator()(const std::string& text) = 0; 20 | 21 | /// add arbitrary number of bytes 22 | virtual void add(const void* data, size_t numBytes) = 0; 23 | 24 | /// return latest hash as hex characters 25 | virtual std::string getHash() = 0; 26 | 27 | /// restart 28 | virtual void reset() = 0; 29 | }; 30 | 31 | -------------------------------------------------------------------------------- /hashlibrary/md5.h: -------------------------------------------------------------------------------- 1 | // ////////////////////////////////////////////////////////// 2 | // md5.h 3 | // Copyright (c) 2014 Stephan Brumme. All rights reserved. 4 | // see http://create.stephan-brumme.com/disclaimer.html 5 | // 6 | 7 | #pragma once 8 | 9 | 10 | #include "hashlibraryExport.h" 11 | //#include "hash.h" 12 | #include 13 | 14 | // define fixed size integer types 15 | #ifdef _MSC_VER 16 | // Windows 17 | typedef unsigned __int8 uint8_t; 18 | typedef unsigned __int32 uint32_t; 19 | typedef unsigned __int64 uint64_t; 20 | #else 21 | // GCC 22 | #include 23 | #endif 24 | 25 | 26 | /// compute MD5 hash 27 | /** Usage: 28 | MD5 md5; 29 | std::string myHash = md5("Hello World"); // std::string 30 | std::string myHash2 = md5("How are you", 11); // arbitrary data, 11 bytes 31 | 32 | // or in a streaming fashion: 33 | 34 | MD5 md5; 35 | while (more data available) 36 | md5.add(pointer to fresh data, number of new bytes); 37 | std::string myHash3 = md5.getHash(); 38 | */ 39 | class HASHLIBRARY_EXPORT MD5 //: public Hash 40 | { 41 | public: 42 | /// split into 64 byte blocks (=> 512 bits), hash is 16 bytes long 43 | enum { BlockSize = 512 / 8, HashBytes = 16 }; 44 | 45 | /// same as reset() 46 | MD5(); 47 | 48 | /// compute MD5 of a memory block 49 | std::string operator()(const void* data, size_t numBytes); 50 | /// compute MD5 of a string, excluding final zero 51 | std::string operator()(const std::string& text); 52 | 53 | /// add arbitrary number of bytes 54 | void add(const void* data, size_t numBytes); 55 | 56 | /// return latest hash as 32 hex characters 57 | std::string getHash(); 58 | /// return latest hash as bytes 59 | void getHash(unsigned char buffer[HashBytes]); 60 | 61 | /// restart 62 | void reset(); 63 | 64 | private: 65 | /// process 64 bytes 66 | void processBlock(const void* data); 67 | /// process everything left in the internal buffer 68 | void processBuffer(); 69 | 70 | /// size of processed data in bytes 71 | uint64_t m_numBytes; 72 | /// valid bytes in m_buffer 73 | size_t m_bufferSize; 74 | /// bytes not processed yet 75 | uint8_t m_buffer[BlockSize]; 76 | 77 | enum { HashValues = HashBytes / 4 }; 78 | /// hash, stored as integers 79 | uint32_t m_hash[HashValues]; 80 | }; 81 | 82 | -------------------------------------------------------------------------------- /hashlibrary/readme.md: -------------------------------------------------------------------------------- 1 | # Portable C++ Hashing Library 2 | 3 | This is a mirror of my library hosted at https://create.stephan-brumme.com/hash-library/ 4 | 5 | In a nutshell: 6 | 7 | - computes CRC32, MD5, SHA1 and SHA256 (most common member of the SHA2 functions), Keccak and its SHA3 sibling 8 | - optional HMAC (keyed-hash message authentication code) 9 | - no external dependencies, small code size 10 | - can work chunk-wise (for example when reading streams block-by-block) 11 | - portable: supports Windows and Linux, tested on Little Endian and Big Endian CPUs 12 | - roughly as fast as Linux core hashing functions 13 | - open source, zlib license 14 | 15 | You can find code examples, benchmarks and much more on my website https://create.stephan-brumme.com/hash-library/ 16 | 17 | # How to use 18 | 19 | This example computes SHA256 hashes but the API is more or less identical for all hash algorithms: 20 | 21 | ``` cpp 22 | // SHA2 test program 23 | #include "sha256.h" 24 | #include // for std::cout only, not needed for hashing library 25 | 26 | int main(int, char**) 27 | { 28 | // create a new hashing object 29 | SHA256 sha256; 30 | 31 | // hashing an std::string 32 | std::cout << sha256("Hello World") << std::endl; 33 | // => a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e 34 | 35 | // hashing a buffer of bytes 36 | const char* buffer = "How are you"; 37 | std::cout << sha256(buffer, 11) << std::endl; 38 | // => 9c7d5b046878838da72e40ceb3179580958df544b240869b80d0275cc07209cc 39 | 40 | // or in a streaming fashion (re-use "How are you") 41 | SHA256 sha256stream; 42 | const char* url = "create.stephan-brumme.com"; // 25 bytes 43 | int step = 5; 44 | for (int i = 0; i < 25; i += step) 45 | sha256stream.add(url + i, step); // add five bytes at a time 46 | std::cout << sha256stream.getHash() << std::endl; 47 | // => 82aa771f1183c52f973c798c9243a1c73833ea40961c73e55e12430ec77b69f6 48 | 49 | return 0; 50 | } 51 | ``` 52 | -------------------------------------------------------------------------------- /hashlibrary/sha1.h: -------------------------------------------------------------------------------- 1 | // ////////////////////////////////////////////////////////// 2 | // sha1.h 3 | // Copyright (c) 2014,2015 Stephan Brumme. All rights reserved. 4 | // see http://create.stephan-brumme.com/disclaimer.html 5 | // 6 | 7 | #pragma once 8 | //#include "hash.h" 9 | #include 10 | #include "hashlibraryExport.h" 11 | // define fixed size integer types 12 | #ifdef _MSC_VER 13 | // Windows 14 | typedef unsigned __int8 uint8_t; 15 | typedef unsigned __int32 uint32_t; 16 | typedef unsigned __int64 uint64_t; 17 | #else 18 | // GCC 19 | #include 20 | #endif 21 | 22 | 23 | /// compute SHA1 hash 24 | /** Usage: 25 | SHA1 sha1; 26 | std::string myHash = sha1("Hello World"); // std::string 27 | std::string myHash2 = sha1("How are you", 11); // arbitrary data, 11 bytes 28 | 29 | // or in a streaming fashion: 30 | 31 | SHA1 sha1; 32 | while (more data available) 33 | sha1.add(pointer to fresh data, number of new bytes); 34 | std::string myHash3 = sha1.getHash(); 35 | */ 36 | class HASHLIBRARY_EXPORT SHA1 //: public Hash 37 | { 38 | public: 39 | /// split into 64 byte blocks (=> 512 bits), hash is 20 bytes long 40 | enum { BlockSize = 512 / 8, HashBytes = 20 }; 41 | 42 | /// same as reset() 43 | SHA1(); 44 | 45 | /// compute SHA1 of a memory block 46 | std::string operator()(const void* data, size_t numBytes); 47 | /// compute SHA1 of a string, excluding final zero 48 | std::string operator()(const std::string& text); 49 | 50 | /// add arbitrary number of bytes 51 | void add(const void* data, size_t numBytes); 52 | 53 | /// return latest hash as 40 hex characters 54 | std::string getHash(); 55 | /// return latest hash as bytes 56 | void getHash(unsigned char buffer[HashBytes]); 57 | 58 | /// restart 59 | void reset(); 60 | 61 | private: 62 | /// process 64 bytes 63 | void processBlock(const void* data); 64 | /// process everything left in the internal buffer 65 | void processBuffer(); 66 | 67 | /// size of processed data in bytes 68 | uint64_t m_numBytes; 69 | /// valid bytes in m_buffer 70 | size_t m_bufferSize; 71 | /// bytes not processed yet 72 | uint8_t m_buffer[BlockSize]; 73 | 74 | enum { HashValues = HashBytes / 4 }; 75 | /// hash, stored as integers 76 | uint32_t m_hash[HashValues]; 77 | }; 78 | 79 | -------------------------------------------------------------------------------- /hashlibrary/sha256.h: -------------------------------------------------------------------------------- 1 | // ////////////////////////////////////////////////////////// 2 | // sha256.h 3 | // Copyright (c) 2014,2015 Stephan Brumme. All rights reserved. 4 | // see http://create.stephan-brumme.com/disclaimer.html 5 | // 6 | 7 | #pragma once 8 | 9 | #include "hashlibraryExport.h" 10 | //#include "hash.h" 11 | #include 12 | 13 | // define fixed size integer types 14 | #ifdef _MSC_VER 15 | // Windows 16 | typedef unsigned __int8 uint8_t; 17 | typedef unsigned __int32 uint32_t; 18 | typedef unsigned __int64 uint64_t; 19 | #else 20 | // GCC 21 | #include 22 | #endif 23 | 24 | 25 | /// compute SHA256 hash 26 | /** Usage: 27 | SHA256 sha256; 28 | std::string myHash = sha256("Hello World"); // std::string 29 | std::string myHash2 = sha256("How are you", 11); // arbitrary data, 11 bytes 30 | 31 | // or in a streaming fashion: 32 | 33 | SHA256 sha256; 34 | while (more data available) 35 | sha256.add(pointer to fresh data, number of new bytes); 36 | std::string myHash3 = sha256.getHash(); 37 | */ 38 | class HASHLIBRARY_EXPORT SHA256 //: public Hash 39 | { 40 | public: 41 | /// split into 64 byte blocks (=> 512 bits), hash is 32 bytes long 42 | enum { BlockSize = 512 / 8, HashBytes = 32 }; 43 | 44 | /// same as reset() 45 | SHA256(); 46 | 47 | /// compute SHA256 of a memory block 48 | std::string operator()(const void* data, size_t numBytes); 49 | /// compute SHA256 of a string, excluding final zero 50 | std::string operator()(const std::string& text); 51 | 52 | /// add arbitrary number of bytes 53 | void add(const void* data, size_t numBytes); 54 | 55 | /// return latest hash as 64 hex characters 56 | std::string getHash(); 57 | /// return latest hash as bytes 58 | void getHash(unsigned char buffer[HashBytes]); 59 | 60 | /// restart 61 | void reset(); 62 | 63 | private: 64 | /// process 64 bytes 65 | void processBlock(const void* data); 66 | /// process everything left in the internal buffer 67 | void processBuffer(); 68 | 69 | /// size of processed data in bytes 70 | uint64_t m_numBytes; 71 | /// valid bytes in m_buffer 72 | size_t m_bufferSize; 73 | /// bytes not processed yet 74 | uint8_t m_buffer[BlockSize]; 75 | 76 | enum { HashValues = HashBytes / 4 }; 77 | /// hash, stored as integers 78 | uint32_t m_hash[HashValues]; 79 | }; 80 | 81 | -------------------------------------------------------------------------------- /hashlibrary/sha3.h: -------------------------------------------------------------------------------- 1 | // ////////////////////////////////////////////////////////// 2 | // sha3.h 3 | // Copyright (c) 2014,2015 Stephan Brumme. All rights reserved. 4 | // see http://create.stephan-brumme.com/disclaimer.html 5 | // 6 | 7 | #pragma once 8 | 9 | #include "hashlibraryExport.h" 10 | 11 | //#include "hash.h" 12 | #include 13 | 14 | // define fixed size integer types 15 | #ifdef _MSC_VER 16 | // Windows 17 | typedef unsigned __int8 uint8_t; 18 | typedef unsigned __int64 uint64_t; 19 | #else 20 | // GCC 21 | #include 22 | #endif 23 | 24 | 25 | /// compute SHA3 hash 26 | /** Usage: 27 | SHA3 sha3; 28 | std::string myHash = sha3("Hello World"); // std::string 29 | std::string myHash2 = sha3("How are you", 11); // arbitrary data, 11 bytes 30 | 31 | // or in a streaming fashion: 32 | 33 | SHA3 sha3; 34 | while (more data available) 35 | sha3.add(pointer to fresh data, number of new bytes); 36 | std::string myHash3 = sha3.getHash(); 37 | */ 38 | class HASHLIBRARY_EXPORT SHA3 //: public Hash 39 | { 40 | public: 41 | /// algorithm variants 42 | enum Bits { Bits224 = 224, Bits256 = 256, Bits384 = 384, Bits512 = 512 }; 43 | 44 | /// same as reset() 45 | explicit SHA3(Bits bits = Bits256); 46 | 47 | /// compute hash of a memory block 48 | std::string operator()(const void* data, size_t numBytes); 49 | /// compute hash of a string, excluding final zero 50 | std::string operator()(const std::string& text); 51 | 52 | /// add arbitrary number of bytes 53 | void add(const void* data, size_t numBytes); 54 | 55 | /// return latest hash as hex characters 56 | std::string getHash(); 57 | 58 | /// restart 59 | void reset(); 60 | 61 | private: 62 | /// process a full block 63 | void processBlock(const void* data); 64 | /// process everything left in the internal buffer 65 | void processBuffer(); 66 | 67 | /// 1600 bits, stored as 25x64 bit, BlockSize is no more than 1152 bits (Keccak224) 68 | enum { 69 | StateSize = 1600 / (8 * 8), 70 | MaxBlockSize = 200 - 2 * (224 / 8) 71 | }; 72 | 73 | /// hash 74 | uint64_t m_hash[StateSize]; 75 | /// size of processed data in bytes 76 | uint64_t m_numBytes; 77 | /// block size (less or equal to MaxBlockSize) 78 | size_t m_blockSize; 79 | /// valid bytes in m_buffer 80 | size_t m_bufferSize; 81 | /// bytes not processed yet 82 | uint8_t m_buffer[MaxBlockSize]; 83 | /// variant 84 | Bits m_bits; 85 | }; 86 | -------------------------------------------------------------------------------- /hashlibrary/tests/github-issue2.cpp: -------------------------------------------------------------------------------- 1 | // minimal test case for https://github.com/stbrumme/hash-library/issues/2 2 | // g++ github-issue2.cpp ../sha*.cpp ../keccak.cpp ../md5.cpp -o github-issue2 && ./github-issue2 3 | 4 | #include "../sha1.h" 5 | #include "../sha256.h" 6 | #include "../sha3.h" 7 | #include "../keccak.h" 8 | #include "../md5.h" 9 | #include 10 | 11 | int main() 12 | { 13 | std::string text = "hello world"; 14 | 15 | std::cout << "SHA1:" << std::endl; 16 | SHA1 sha1; 17 | sha1.add(text.c_str(), text.size()); 18 | 19 | std::cout << sha1.getHash() << std::endl; 20 | std::cout << sha1.getHash() << std::endl; 21 | std::cout << sha1.getHash() << std::endl; 22 | 23 | std::cout << std::endl; 24 | 25 | std::cout << "SHA256:" << std::endl; 26 | SHA256 sha256; 27 | sha256.add(text.c_str(), text.size()); 28 | 29 | std::cout << sha256.getHash() << std::endl; 30 | std::cout << sha256.getHash() << std::endl; 31 | std::cout << sha256.getHash() << std::endl; 32 | 33 | std::cout << std::endl; 34 | 35 | std::cout << "SHA3:" << std::endl; 36 | SHA3 sha3; 37 | sha3.add(text.c_str(), text.size()); 38 | 39 | std::cout << sha3.getHash() << std::endl; 40 | std::cout << sha3.getHash() << std::endl; 41 | std::cout << sha3.getHash() << std::endl; 42 | 43 | std::cout << std::endl; 44 | 45 | std::cout << "Keccak:" << std::endl; 46 | Keccak keccak; 47 | keccak.add(text.c_str(), text.size()); 48 | 49 | std::cout << keccak.getHash() << std::endl; 50 | std::cout << keccak.getHash() << std::endl; 51 | std::cout << keccak.getHash() << std::endl; 52 | 53 | std::cout << std::endl; 54 | 55 | std::cout << "MD5:" << std::endl; 56 | MD5 md5; 57 | md5.add(text.c_str(), text.size()); 58 | 59 | std::cout << md5.getHash() << std::endl; 60 | std::cout << md5.getHash() << std::endl; 61 | std::cout << md5.getHash() << std::endl; 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /hashlibrary/tests/github-issue6.cpp: -------------------------------------------------------------------------------- 1 | // minimal test case for https://github.com/stbrumme/hash-library/issues/6 2 | // g++ github-issue6.cpp ../sha3.cpp -o github-issue6 && ./github-issue6 3 | 4 | #include "../sha3.h" 5 | #include 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | std::string text = "72a5f501151ca974002f34"; 10 | 11 | SHA3 hasher(SHA3::Bits512); 12 | hasher.add(text.data(), text.size()); 13 | std::cout << hasher.getHash() << std::endl; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /hotupdate/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(hotupdate) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | FILE(GLOB_RECURSE MY_HEADERS "./*.h") 7 | source_group("Headers" FILES ${MY_HEADERS}) 8 | 9 | FILE(GLOB_RECURSE MY_INTERFACEHEADERS "../inc/networks/*.h") 10 | source_group("Headers" FILES ${MY_INTERFACEHEADERS}) 11 | 12 | include_directories(../inc ../Utils ../database ../configs) 13 | 14 | AUX_SOURCE_DIRECTORY(. A) 15 | 16 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 17 | ADD_LIBRARY(hotupdate SHARED ${A}) 18 | set_target_properties(hotupdate PROPERTIES VS_GLOBAL_VcpkgEnabled true) 19 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 20 | add_compile_options(-fcoroutines) 21 | ADD_LIBRARY(hotupdate ${A} ) 22 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 23 | ADD_LIBRARY(hotupdate ${A} ) 24 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 25 | 26 | target_link_libraries(hotupdate PRIVATE Utils PRIVATE configs PRIVATE spdlog::spdlog PRIVATE efsw::efsw) 27 | 28 | generate_export_header(hotupdate EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 29 | 30 | -------------------------------------------------------------------------------- /hotupdate/CoEvent.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "CoEvent.h" 3 | #include "asio/redirect_error.hpp" 4 | #include "asio/use_awaitable.hpp" 5 | using asio::awaitable; 6 | using asio::use_awaitable; 7 | using asio::redirect_error; 8 | 9 | namespace SMHotupdate 10 | { 11 | 12 | CoEvent::CoEvent( 13 | asio::io_context& io_service, uint64_t milliseconds /* = 3600000*/) 14 | : timer_(io_service) 15 | , milliseconds_(milliseconds) 16 | { 17 | } 18 | 19 | CoEvent::~CoEvent() 20 | { 21 | 22 | } 23 | 24 | awaitable CoEvent::async_wait() 25 | { 26 | timer_.expires_after(std::chrono::milliseconds(milliseconds_)); 27 | asio::error_code ec; 28 | co_await timer_.async_wait(redirect_error(use_awaitable, ec)); 29 | } 30 | 31 | asio::awaitable CoEvent::async_time_wait() 32 | { 33 | ETrigeType eRet{ ETrigeType::TimeOut }; 34 | timer_.expires_after(std::chrono::milliseconds(milliseconds_)); 35 | asio::error_code ec; 36 | co_await timer_.async_wait(redirect_error(use_awaitable, ec)); 37 | eRet = getEventType(ec); 38 | co_return eRet; 39 | } 40 | 41 | void CoEvent::reset() 42 | { 43 | timer_.cancel(); 44 | } 45 | 46 | void CoEvent::trigger() 47 | { 48 | timer_.cancel_one(); 49 | 50 | } 51 | void CoEvent::trigger_all() 52 | { 53 | timer_.cancel(); 54 | } 55 | 56 | ETrigeType getEventType(asio::error_code& ec) 57 | { 58 | ETrigeType ret{ ETrigeType::TimeOut }; 59 | if (ec.value() == asio::error::operation_aborted) 60 | { 61 | ret = SMHotupdate::ETrigeType::Cancel; 62 | } 63 | return ret; 64 | 65 | } 66 | 67 | } 68 | 69 | -------------------------------------------------------------------------------- /hotupdate/CoEvent.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include "hotupdateExport.h" 4 | #include "spdlog/spdlog.h" 5 | #include "asio/steady_timer.hpp" 6 | #include "myconcept.h" 7 | #include "IOContextManager.h" 8 | 9 | namespace SMHotupdate 10 | { 11 | enum class ETrigeType 12 | { 13 | TimeOut, 14 | Cancel, 15 | }; 16 | ETrigeType HOTUPDATE_EXPORT getEventType(asio::error_code& ec); 17 | 18 | class HOTUPDATE_EXPORT CoEvent 19 | { 20 | public: 21 | explicit CoEvent(asio::io_context& io_service, uint64_t milliseconds=3600000); 22 | CoEvent(const CoEvent& ) = delete; 23 | CoEvent(const CoEvent&&) = delete; 24 | ~CoEvent(); 25 | asio::awaitable async_wait(); 26 | asio::awaitable async_time_wait(); 27 | void reset(); 28 | void trigger(); 29 | void trigger_all(); 30 | private: 31 | asio::steady_timer timer_; 32 | uint64_t milliseconds_; 33 | }; 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /hotupdate/FileOp.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "FileOp.h" 3 | #include "_fileOpSync.cpp" 4 | #include "_fileOpAsync.cpp" 5 | 6 | #include 7 | #include 8 | 9 | using std::filesystem::exists; 10 | namespace SMHotupdate 11 | { 12 | fileOp::fileOp(std::string path) 13 | : _path(path), _datalen(0), _exist(false), _ev(*IOCTXHOTUPDATE, 24*3600*1000) 14 | { 15 | } 16 | 17 | string_view fileOp::getData() 18 | { 19 | return string_view{ &_datas[0], _datalen }; 20 | } 21 | 22 | const string fileOp::getPath() 23 | { 24 | return _path; 25 | } 26 | 27 | asio::awaitable fileOp::asyncGetData() 28 | { 29 | string_view ret; 30 | bool loadflag = false; 31 | if (_datas.empty() && _datalen==0) 32 | { 33 | auto filesize = getFileSize(); 34 | if (filesize == 0) 35 | { 36 | co_return ret; 37 | } 38 | BEGIN_ASIO; 39 | #if defined(ASIO_HAS_IO_URING) || defined(_WIN32) 40 | loadflag = co_await fileOpASync::getData(_datas, _path, 0, filesize); 41 | #endif 42 | if (!loadflag) 43 | { 44 | loadflag = co_await fileOpSync::getData(_datas, _path, 0, filesize); 45 | } 46 | END_ASIO; 47 | if (!loadflag) 48 | { 49 | SPDLOG_WARN("load file {} failed ", _path); 50 | } 51 | _ev.trigger_all(); 52 | } 53 | else if (_datalen > 0 && _datas.empty()) //async load already start, wait it finish 54 | { 55 | SPDLOG_INFO("resource {} async load data had started by other co, this co wait it finish", _path); 56 | co_await _ev.async_wait(); 57 | } 58 | else 59 | { 60 | SPDLOG_INFO("resource {} already cached, direct load it", _path); 61 | } 62 | ret = _datas; 63 | co_return ret; 64 | } 65 | 66 | 67 | size_t fileOp::getFileSize() 68 | { 69 | if (_datalen != 0) 70 | { 71 | SPDLOG_INFO("file {} size have already get size {}", _path, _datalen); 72 | return _datalen; 73 | } 74 | BEGIN_ASIO; 75 | if (std::filesystem::exists(std::filesystem::path(_path))) 76 | { 77 | _datalen = std::filesystem::file_size(std::filesystem::path(_path)); 78 | } 79 | else 80 | { 81 | SPDLOG_WARN("get file {} failed as file not exist", _path); 82 | } 83 | END_ASIO; 84 | if (_datalen == 0) 85 | { 86 | SPDLOG_WARN("get file [{}] dat len=0", _path); 87 | } 88 | SPDLOG_INFO("file {} data length {}", _path, _datalen); 89 | return _datalen; 90 | } 91 | 92 | } 93 | 94 | -------------------------------------------------------------------------------- /hotupdate/FileOp.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "hotupdateExport.h" 3 | #include "CoEvent.h" 4 | #include "asio/system_timer.hpp" 5 | #include "asio/awaitable.hpp" 6 | #include 7 | #include 8 | 9 | #if defined(ASIO_HAS_IO_URING) || defined(_WIN32) 10 | #include "asio/stream_file.hpp" 11 | using asio::stream_file; 12 | #endif 13 | using asio::io_context; 14 | using std::shared_ptr; 15 | using std::string; 16 | using std::string_view; 17 | 18 | namespace SMHotupdate 19 | { 20 | class HOTUPDATE_EXPORT fileOp 21 | { 22 | public: 23 | fileOp(std::string path); 24 | fileOp(const fileOp&) = delete; 25 | fileOp(const fileOp&& cv) = delete; 26 | string_view getData(); 27 | const string getPath(); 28 | asio::awaitable asyncGetData(); 29 | size_t getFileSize(); 30 | private: 31 | std::string _path; 32 | size_t _datalen; 33 | bool _exist; 34 | string _datas; 35 | string _filepath; 36 | SMHotupdate::CoEvent _ev; 37 | }; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /hotupdate/FilePathMonitor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "hotupdateExport.h" 3 | #include "concurrentqueue/concurrentqueue.h" 4 | #include "PathMonitorImpl.h" 5 | #include "FileOp.h" 6 | #include "tsl/htrie_map.h" 7 | #include "tsl/htrie_set.h" 8 | #include "CoEvent.h" 9 | #include 10 | #include 11 | #include 12 | #include 13 | #if defined(ASIO_HAS_IO_URING) || defined(_WIN32) 14 | using asio::stream_file; 15 | #endif 16 | using asio::io_context; 17 | using std::shared_ptr; 18 | using std::mutex; 19 | using std::lock_guard; 20 | 21 | #define RESLOAD (SMHotupdate::FilePathMonitor::getInst()) 22 | namespace SMHotupdate 23 | { 24 | class HOTUPDATE_EXPORT FilePathMonitor 25 | { 26 | public: 27 | static shared_ptr getInst(); 28 | public: 29 | FilePathMonitor(); 30 | /** 31 | * @brief add file monitor path 32 | * not thread safe 33 | * 34 | * @param path 35 | * @param recursion 36 | * @return true 37 | * @return false 38 | */ 39 | bool addMonitor(std::string path, bool recursion = false); 40 | void notifyLoadComplete(std::string path); 41 | void notifyFileModified(std::string path); 42 | void notifyConfigModified(std::string path); 43 | asio::awaitable asyncGetResource(std::string path); 44 | asio::awaitable waitHotUpdate(); 45 | void setReady(); 46 | bool isReady(); 47 | private: 48 | tsl::htrie_map> _files; 49 | tsl::htrie_set _paths; 50 | moodycamel::ConcurrentQueue _pathsUpdate; 51 | moodycamel::ConcurrentQueue _pathsErase; 52 | std::unique_ptr _fileWatcher; 53 | std::unique_ptr _listener; 54 | shared_ptr _eventHotUpdate = nullptr; 55 | bool _isReady = false; 56 | }; 57 | } 58 | -------------------------------------------------------------------------------- /hotupdate/IOContextManager.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "IOContextManager.h" 3 | #include "hotupdate.h" 4 | #include "asio/signal_set.hpp" 5 | #include "fmt/format.h" 6 | #include "spdlog/spdlog.h" 7 | #include 8 | #include 9 | 10 | using std::thread; 11 | using std::make_shared; 12 | using std::mutex; 13 | using std::lock_guard; 14 | 15 | static mutex _mForInitIoContext; 16 | 17 | 18 | namespace SMHotupdate 19 | { 20 | static shared_ptr _inst = { nullptr }; 21 | 22 | void IOContextManager::sInit(asio::io_context* ioc) 23 | { 24 | lock_guard lock(_mForInitIoContext); 25 | assert(_inst == nullptr); 26 | if (_inst != nullptr) 27 | { 28 | return; 29 | } 30 | _inst = make_shared(ioc); 31 | assert(ioc != nullptr); 32 | _inst->run(1); 33 | } 34 | 35 | IOContextManager* IOContextManager::getInst() 36 | { 37 | assert(_inst != nullptr); 38 | return _inst.get(); 39 | } 40 | 41 | void IOContextManager::sUninit() 42 | { 43 | } 44 | 45 | 46 | IOContextManager::IOContextManager(asio::io_context* ioc) 47 | :_ioc(ioc) 48 | { 49 | assert(_ioc != nullptr); 50 | assert(_ex == nullptr); 51 | assert(_exNet == nullptr); 52 | _ex = make_shared(); 53 | _exNet = make_shared(); 54 | } 55 | 56 | tf::Executor* IOContextManager::getExec() 57 | { 58 | assert(_ex != nullptr); 59 | return _ex.get(); 60 | } 61 | 62 | 63 | tf::Executor* IOContextManager::getNetExec() 64 | { 65 | assert(_exNet != nullptr); 66 | return _exNet.get(); 67 | } 68 | 69 | asio::io_context* IOContextManager::getIoc() 70 | { 71 | return _ioc; 72 | } 73 | 74 | 75 | bool IOContextManager::run(int threads) 76 | { 77 | bool bret{ false }; 78 | 79 | 80 | thread thio([&]() 81 | { 82 | bool bstop = false; 83 | asio::signal_set signals(*_ioc, SIGINT, SIGTERM); 84 | signals.async_wait([&](auto, auto) { 85 | bstop = true; 86 | }); 87 | while (!bstop) 88 | { 89 | SPDLOG_INFO("asio run start "); 90 | _ioc->run(); 91 | SPDLOG_INFO("asio run finish"); 92 | } 93 | }); 94 | thio.detach(); 95 | bret = true; 96 | return bret; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /hotupdate/IOContextManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "hotupdateExport.h" 3 | #include "consts.h" 4 | #include "hotupdate.h" 5 | #include "taskflow/taskflow.hpp" 6 | #include "asio/io_context.hpp" 7 | #include 8 | #include 9 | #include 10 | 11 | using asio::io_context; 12 | using std::shared_ptr; 13 | using std::unique_ptr; 14 | using std::map; 15 | 16 | 17 | namespace SMHotupdate 18 | { 19 | 20 | class IOContextManager 21 | { 22 | public: 23 | static void sInit(asio::io_context* ioc); 24 | static IOContextManager* getInst(); 25 | static void sUninit(); 26 | 27 | public: 28 | IOContextManager(asio::io_context* ioc); 29 | tf::Executor* getExec(); 30 | tf::Executor* getNetExec(); 31 | asio::io_context* getIoc(); 32 | bool run(int threads); 33 | private: 34 | 35 | asio::io_context* _ioc{nullptr}; 36 | shared_ptr _ex{nullptr}; 37 | shared_ptr _exNet{nullptr}; 38 | }; 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /hotupdate/PathMonitorImpl.cpp: -------------------------------------------------------------------------------- 1 | #include "PathMonitorImpl.h" 2 | 3 | #include "FilePathMonitor.h" 4 | 5 | #include "Configs.h" 6 | 7 | #include 8 | 9 | namespace SMHotupdate 10 | { 11 | 12 | PathMonitorImpl::PathMonitorImpl(FilePathMonitor* monitor) 13 | :_pathMonitor(monitor) 14 | { 15 | 16 | } 17 | 18 | void PathMonitorImpl::handleFileAction(efsw::WatchID watchid, const std::string& dir, const std::string& filename, efsw::Action action, std::string oldFilename) 19 | { 20 | switch (action) 21 | { 22 | case efsw::Actions::Add: 23 | { 24 | }break; 25 | case efsw::Actions::Delete: 26 | { 27 | 28 | }break; 29 | case efsw::Actions::Modified: 30 | { 31 | std::filesystem::path p = dir; 32 | p.append(filename); 33 | auto localpath = p.string(); 34 | if(localpath.ends_with(".toml")) 35 | { 36 | _pathMonitor->notifyConfigModified(localpath); 37 | } 38 | else 39 | { 40 | _pathMonitor->notifyFileModified(localpath); 41 | } 42 | }break; 43 | case efsw::Actions::Moved: 44 | { 45 | }break; 46 | default: 47 | { 48 | 49 | }break; 50 | } 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /hotupdate/PathMonitorImpl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "hotupdateExport.h" 3 | #include "efsw/efsw.hpp" 4 | 5 | namespace SMHotupdate 6 | { 7 | 8 | 9 | class FilePathMonitor; 10 | 11 | class HOTUPDATE_EXPORT PathMonitorImpl : public efsw::FileWatchListener 12 | { 13 | public: 14 | PathMonitorImpl(FilePathMonitor* monitor); 15 | void handleFileAction(efsw::WatchID watchid, const std::string& dir, 16 | const std::string& filename, efsw::Action action, 17 | std::string oldFilename) override; 18 | 19 | private: 20 | FilePathMonitor* _pathMonitor = nullptr; 21 | }; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /hotupdate/_fileOpAsync.cpp: -------------------------------------------------------------------------------- 1 | 2 | #if defined(ASIO_HAS_IO_URING) || defined(_WIN32) 3 | 4 | #include 5 | #include 6 | #include "hotupdate.h" 7 | #include "asio/buffer.hpp" 8 | #include "asio/use_awaitable.hpp" 9 | #include "asio/stream_file.hpp" 10 | #include "asio/random_access_file.hpp" 11 | #include "asio/awaitable.hpp" 12 | #include "spdlog/spdlog.h" 13 | 14 | using std::string; 15 | using std::string_view; 16 | 17 | namespace SMHotupdate 18 | { 19 | class fileOpASync 20 | { 21 | public: 22 | static asio::awaitable getData(string& dst, string_view path, off_t offset, size_t len) 23 | { 24 | bool bret = true; 25 | if (len == 0) 26 | { 27 | co_return false; 28 | } 29 | dst.resize(len); 30 | 31 | BEGIN_ASIO; 32 | asio::random_access_file _stream_file(*IOCTX); 33 | asio::error_code ec; 34 | asio::error_code openerr = _stream_file.open(path.data(), asio::stream_file::read_only, ec); 35 | if (openerr.value() != 0) 36 | { 37 | SPDLOG_WARN("async open path {} failed with error code {}", path, openerr.value()); 38 | co_return false; 39 | } 40 | co_await _stream_file.async_read_some_at(offset, asio::buffer(dst), asio::use_awaitable); 41 | bret = true; 42 | END_ASIO; 43 | if (!bret) 44 | { 45 | SPDLOG_WARN("current not support asio read from file, use sync read from file"); 46 | } 47 | co_return bret; 48 | } 49 | }; 50 | 51 | } 52 | #else 53 | 54 | #endif 55 | 56 | -------------------------------------------------------------------------------- /hotupdate/_fileOpSync.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "asio/use_awaitable.hpp" 3 | #include "asio/co_spawn.hpp" 4 | #include "asio/detached.hpp" 5 | #include "spdlog/spdlog.h" 6 | #include "hotupdate.h" 7 | #include "CoEvent.h" 8 | 9 | 10 | namespace SMHotupdate 11 | { 12 | 13 | class fileOpSync 14 | { 15 | public: 16 | static asio::awaitable getData(string& dst, string_view path, off_t offset, size_t len) 17 | { 18 | bool bret = true; 19 | 20 | if (len == 0) 21 | { 22 | co_return false; 23 | } 24 | CoEvent event(*IOCTX); 25 | asio::co_spawn(*IOCTX, [&]()->asio::awaitable { 26 | std::ifstream ifs; 27 | ifs.open(path.data()); 28 | if (ifs.good()) 29 | { 30 | ifs.seekg(offset); 31 | ifs.read(&dst[0], len); 32 | SPDLOG_INFO("sync get data length {} for path {}", len, path); 33 | } 34 | else 35 | { 36 | SPDLOG_WARN("get data from file [{}] failed", path); 37 | } 38 | event.trigger_all(); 39 | co_return; 40 | }, asio::detached); 41 | BEGIN_ASIO; 42 | co_await event.async_wait(); 43 | END_ASIO; 44 | co_return bret; 45 | } 46 | }; 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /hotupdate/hotupdate.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "hotupdate.h" 3 | #include "IOContextManager.h" 4 | #include "spdlog/spdlog.h" 5 | #include 6 | 7 | using std::make_shared; 8 | using std::map; 9 | 10 | static bool _inited{ false }; 11 | 12 | static shared_ptr>> _dendioctxinitfuncs = nullptr; 13 | 14 | namespace SMHotupdate 15 | { 16 | bool sInit(asio::io_context* ioc) 17 | { 18 | bool bret{ false }; 19 | if (!_inited) 20 | { 21 | IOContextManager::sInit(ioc); 22 | if (_dendioctxinitfuncs) 23 | { 24 | for (auto& func : *_dendioctxinitfuncs) 25 | { 26 | (*func.first)(); 27 | } 28 | } 29 | bret = true; 30 | } 31 | return bret; 32 | } 33 | 34 | HOTUPDATE_EXPORT void sUninit() 35 | { 36 | 37 | } 38 | 39 | asio::io_context* getIoContext() 40 | { 41 | return IOContextManager::getInst()->getIoc(); 42 | } 43 | 44 | bool AddDependIOCTXInitFunc(shared_ptr func) 45 | { 46 | bool bret{ true }; 47 | if (_dendioctxinitfuncs == nullptr) 48 | { 49 | _dendioctxinitfuncs = make_shared>>(); 50 | } 51 | if (_dendioctxinitfuncs->find(func.get()) != _dendioctxinitfuncs->end()) 52 | { 53 | SPDLOG_WARN("dpend ioctx function {} already at set", fmt::ptr(func.get())); 54 | return bret; 55 | } 56 | _dendioctxinitfuncs->insert({ func.get(), func }); 57 | return bret; 58 | } 59 | 60 | asio::io_context* getContext() 61 | { 62 | return IOContextManager::getInst()->getIoc(); 63 | } 64 | 65 | tf::Executor* getExec() 66 | { 67 | return IOContextManager::getInst()->getExec(); 68 | } 69 | 70 | tf::Executor* getNetExec() 71 | { 72 | return IOContextManager::getInst()->getNetExec(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /hotupdate/hotupdate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "hotupdateExport.h" 3 | #include "asio/io_context.hpp" 4 | #include "taskflow/taskflow.hpp" 5 | #include 6 | 7 | 8 | #define BEGIN_ASIO try { 9 | #define END_ASIO }\ 10 | catch (const asio::system_error& e)\ 11 | {\ 12 | SPDLOG_WARN("asio return system error {}", e.what());\ 13 | }\ 14 | catch (const std::runtime_error& e)\ 15 | {\ 16 | SPDLOG_WARN("asio return runtime error {}", e.what());\ 17 | }\ 18 | catch (const std::exception& e)\ 19 | {\ 20 | SPDLOG_WARN("asio return std exception {}", e.what());\ 21 | }\ 22 | catch (...)\ 23 | {\ 24 | SPDLOG_WARN("asio return unexpected error ");\ 25 | } 26 | 27 | using std::shared_ptr; 28 | typedef std::function DependIOCTXInitFuncType; 29 | 30 | namespace SMHotupdate 31 | { 32 | HOTUPDATE_EXPORT bool sInit(asio::io_context* ioc); 33 | HOTUPDATE_EXPORT void sUninit(); 34 | HOTUPDATE_EXPORT asio::io_context* getIoContext(); 35 | HOTUPDATE_EXPORT bool AddDependIOCTXInitFunc(shared_ptr func); 36 | HOTUPDATE_EXPORT asio::io_context* getContext(); 37 | HOTUPDATE_EXPORT tf::Executor* getExec(); 38 | HOTUPDATE_EXPORT tf::Executor* getNetExec(); 39 | } 40 | 41 | #define IOCTX (SMHotupdate::getContext()) 42 | 43 | #define APPCTX (SMHotupdate::getContext()) 44 | 45 | #define IOCTXHOTUPDATE (SMHotupdate::getContext()) 46 | 47 | #define TFEXEC (SMHotupdate::getExec()) 48 | 49 | #define TFNET (SMHotupdate::getNetExec()) 50 | -------------------------------------------------------------------------------- /inc/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/inc/.DS_Store -------------------------------------------------------------------------------- /inc/MainCmdTag.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | 5 | 6 | #include "enums.h" 7 | 8 | #include "asio/awaitable.hpp" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using std::string; 16 | using std::string_view; 17 | using asio::awaitable; 18 | using std::map; 19 | 20 | enum class ServeMode 21 | { 22 | SBind, 23 | SConnect, 24 | }; 25 | 26 | 27 | class MainCmdTag 28 | { 29 | public: 30 | MainCmdTag() 31 | {} 32 | 33 | virtual void init(ServeMode mode) {}; 34 | 35 | virtual awaitable dealMsg(uint16_t ass, string_view msg) 36 | { 37 | assert(0); 38 | co_return ""; 39 | } 40 | virtual awaitable dealMsg(uint16_t ass, map& query) 41 | { 42 | assert(0); 43 | co_return ""; 44 | }; 45 | virtual awaitable dealMsg(map& query) 46 | { 47 | assert(0); 48 | co_return ""; 49 | }; 50 | //virtual ServeMode getServeMode() = 0; 51 | }; 52 | -------------------------------------------------------------------------------- /inc/commUsing.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include 5 | #include 6 | 7 | using RouterFuncReturnType = std::shared_ptr; 8 | 9 | 10 | -------------------------------------------------------------------------------- /inc/consts.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | const static int useSSL = 1; 4 | -------------------------------------------------------------------------------- /inc/coros.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "asio/co_spawn.hpp" 5 | #include "asio/detached.hpp" 6 | #include "asio/io_context.hpp" 7 | #include "asio/ip/tcp.hpp" 8 | #include "asio/signal_set.hpp" 9 | #include "asio/write.hpp" 10 | #include "asio/system_timer.hpp" 11 | #include "asio/awaitable.hpp" 12 | #include "asio/buffer.hpp" 13 | #include "asio/ip/tcp.hpp" 14 | #include "asio/system_error.hpp" 15 | #include "asio/deadline_timer.hpp" 16 | #include "asio/steady_timer.hpp" 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /inc/iguana/detail/traits.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by QY on 2017-01-05. 3 | // 4 | 5 | #ifndef SERIALIZE_TRAITS_HPP 6 | #define SERIALIZE_TRAITS_HPP 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | namespace iguana 16 | { 17 | template< class T > 18 | struct is_signed_intergral_like : std::integral_constant < bool, 19 | (std::is_integral::value) && 20 | std::is_signed::value 21 | > {}; 22 | 23 | template< class T > 24 | struct is_unsigned_intergral_like : std::integral_constant < bool, 25 | (std::is_integral::value) && 26 | std::is_unsigned::value 27 | > {}; 28 | 29 | template < template class U, typename T > 30 | struct is_template_instant_of : std::false_type {}; 31 | 32 | template < template class U, typename... args > 33 | struct is_template_instant_of< U, U > : std::true_type {}; 34 | 35 | template 36 | struct is_stdstring : is_template_instant_of < std::basic_string, T > 37 | {}; 38 | 39 | template 40 | struct is_tuple : is_template_instant_of < std::tuple, T > 41 | {}; 42 | 43 | template< class T > 44 | struct is_sequence_container : std::integral_constant < bool, 45 | is_template_instant_of::value || 46 | is_template_instant_of::value || 47 | is_template_instant_of::value || 48 | is_template_instant_of::value 49 | > {}; 50 | 51 | template< class T > 52 | struct is_associat_container : std::integral_constant < bool, 53 | is_template_instant_of::value || 54 | is_template_instant_of::value 55 | > {}; 56 | 57 | template< class T > 58 | struct is_emplace_back_able : std::integral_constant < bool, 59 | is_template_instant_of::value || 60 | is_template_instant_of::value || 61 | is_template_instant_of::value 62 | > {}; 63 | 64 | template 65 | struct has_type; 66 | 67 | template 68 | struct has_type> : std::disjunction...> {}; 69 | 70 | template 71 | inline constexpr bool is_int64_v = std::is_same_v||std::is_same_v; 72 | } 73 | #endif //SERIALIZE_TRAITS_HPP 74 | -------------------------------------------------------------------------------- /inc/orm/comm_db.sql: -------------------------------------------------------------------------------- 1 | 2 | drop table if EXISTS Users; 3 | 4 | create table Users( 5 | id SERIAL, 6 | username varchar(128), 7 | nickname varchar(256), 8 | avatar varchar(256), 9 | passwd varchar(256), 10 | salt varchar(256) 11 | ); 12 | 13 | -------------------------------------------------------------------------------- /inc/orm/demo.h: -------------------------------------------------------------------------------- 1 | // generated by ./ddl2cpp ./sql.sql demo orm 2 | #ifndef ORM_DEMO_H 3 | #define ORM_DEMO_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace orm 10 | { 11 | namespace Student_ 12 | { 13 | struct Name 14 | { 15 | struct _alias_t 16 | { 17 | static constexpr const char _literal[] = "name"; 18 | using _name_t = sqlpp::make_char_sequence; 19 | template 20 | struct _member_t 21 | { 22 | T name; 23 | T& operator()() { return name; } 24 | const T& operator()() const { return name; } 25 | }; 26 | }; 27 | using _traits = sqlpp::make_traits; 28 | }; 29 | struct Age 30 | { 31 | struct _alias_t 32 | { 33 | static constexpr const char _literal[] = "age"; 34 | using _name_t = sqlpp::make_char_sequence; 35 | template 36 | struct _member_t 37 | { 38 | T age; 39 | T& operator()() { return age; } 40 | const T& operator()() const { return age; } 41 | }; 42 | }; 43 | using _traits = sqlpp::make_traits; 44 | }; 45 | } // namespace Student_ 46 | 47 | struct Student: sqlpp::table_t 50 | { 51 | struct _alias_t 52 | { 53 | static constexpr const char _literal[] = "student"; 54 | using _name_t = sqlpp::make_char_sequence; 55 | template 56 | struct _member_t 57 | { 58 | T student; 59 | T& operator()() { return student; } 60 | const T& operator()() const { return student; } 61 | }; 62 | }; 63 | }; 64 | } // namespace orm 65 | #endif 66 | -------------------------------------------------------------------------------- /inc/orm/init_db.sql: -------------------------------------------------------------------------------- 1 | 2 | drop table if EXISTS Users; 3 | 4 | create table Users( 5 | id SERIAL, 6 | username varchar(128), 7 | nickname varchar(256), 8 | avatar varchar(256), 9 | passwd varchar(256), 10 | salt varchar(256) 11 | ); 12 | 13 | create table Transitions( 14 | order_no SERIAL, 15 | timestampx varchar(32), 16 | username varchar(64), 17 | price int, 18 | status varchar(12) 19 | ); 20 | -------------------------------------------------------------------------------- /inc/orm/main.py: -------------------------------------------------------------------------------- 1 | 2 | import os; 3 | 4 | 5 | #not support recursive 6 | if __name__ == '__main__': 7 | for root, dirs, files in os.walk('./'): 8 | for file in files: 9 | curfile = os.path.join(root, file); 10 | nameArr = os.path.splitext(file); 11 | if curfile.endswith('.sql') == False: 12 | continue; 13 | os.system('python3 ./ddl2cpp {} {} {}'.format(curfile, nameArr[0], nameArr[0])); 14 | -------------------------------------------------------------------------------- /inc/os/_darwinSpec.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | -------------------------------------------------------------------------------- /inc/os/_linuxSpec.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "tinyutf8/tinyutf8.h" 4 | #include 5 | 6 | -------------------------------------------------------------------------------- /inc/os/_winSpec.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | -------------------------------------------------------------------------------- /inc/os/os.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef _WIN32 4 | #include "_winSpec.h" 5 | #endif 6 | 7 | #ifdef OS_LINUX 8 | #include "_linuxSpec.h" 9 | #endif 10 | 11 | #ifdef OS_DARWIN 12 | #include "_darwinSpec.h" 13 | #endif 14 | 15 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/logo.png -------------------------------------------------------------------------------- /networkinterface/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(networkinterface) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | include_directories(../configs ../database ../Utils ../inc ../.. ../inc/networks ./nngimpl ./tcpimpl ./websocketimpl ./ ../../capn) 7 | 8 | 9 | FILE(GLOB_RECURSE CUR_HEADER "./*.h") 10 | source_group("IncCur" FILES ${CUR_HEADER}) 11 | 12 | FILE(GLOB_RECURSE COMMN_HEADER "../inc/*.h") 13 | source_group("IncComm" FILES ${COMMN_HEADER}) 14 | 15 | include_directories(../conf ../inc ../Utils ../hotupdate ./nng ./socket) 16 | 17 | AUX_SOURCE_DIRECTORY(. A) 18 | aux_source_directory(./nngs B) 19 | aux_source_directory(./socket C) 20 | AUX_SOURCE_DIRECTORY(./dealers D) 21 | 22 | 23 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 24 | ADD_LIBRARY(networkinterface SHARED ${A} ${B} ${C} ${D} ${CUR_HEADER} ${COMMN_HEADER}) 25 | set_target_properties(networkinterface PROPERTIES VS_GLOBAL_VcpkgEnabled true) 26 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 27 | add_compile_options(-fcoroutines) 28 | ADD_LIBRARY(networkinterface ${A} ${B} ${C} ${D}) 29 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 30 | ADD_LIBRARY(networkinterface ${A} ${B} ${C} ${D}) 31 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 32 | 33 | target_link_libraries(networkinterface PRIVATE ZLIB::ZLIB PRIVATE nng::nngpp PRIVATE Utils PRIVATE configs PRIVATE hotupdate PRIVATE nlohmann_json::nlohmann_json PRIVATE nng::nng PRIVATE spdlog::spdlog PRIVATE OpenSSL::SSL OpenSSL::Crypto) 34 | 35 | 36 | generate_export_header(networkinterface EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 37 | 38 | -------------------------------------------------------------------------------- /networkinterface/ChannelManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/networkinterface/ChannelManager.cpp -------------------------------------------------------------------------------- /networkinterface/ChannelManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include 4 | #include "myconcept.h" 5 | #include "enums.h" 6 | #include "magic_enum.hpp" 7 | #include "concurrentqueue/concurrentqueue.h" 8 | #include "spdlog/spdlog.h" 9 | #include 10 | #include 11 | 12 | using std::shared_ptr; 13 | using std::map; 14 | using std::any; 15 | 16 | namespace SMNetwork 17 | { 18 | template 19 | class ChannelManager 20 | { 21 | public: 22 | void sInit(); 23 | void sUninit(); 24 | ChannelManager* getInst(); 25 | public: 26 | ChannelManager(); 27 | ~ChannelManager(); 28 | 29 | }; 30 | } 31 | -------------------------------------------------------------------------------- /networkinterface/ChannelsMgr.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ChannelsMgr.h" 3 | #include "ChannelManager.h" 4 | #include "ChannelCombine.h" 5 | #include 6 | #include 7 | using std::shared_ptr; 8 | using std::make_shared; 9 | namespace SMNetwork 10 | { 11 | bool ChannelsMgr::sInit() 12 | { 13 | bool bret{ true }; 14 | 15 | return bret; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /networkinterface/ChannelsMgr.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include "networkinterfaceExport.h" 4 | 5 | #include "enums.h" 6 | 7 | namespace SMNetwork 8 | { 9 | class NETWORKINTERFACE_EXPORT ChannelsMgr 10 | { 11 | public: 12 | static bool sInit(); 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /networkinterface/FullDuplexChannel.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include "networkinterfaceExport.h" 4 | #include "asio/awaitable.hpp" 5 | #include "StreamNetDealer.h" 6 | #include "asio/experimental/concurrent_channel.hpp" 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "PackDealerMainSub.h" 13 | #include "CoEvent.h" 14 | using std::string; 15 | using std::map; 16 | using std::array; 17 | using std::list; 18 | using std::array; 19 | using std::unique_ptr; 20 | using std::shared_ptr; 21 | using std::make_shared; 22 | using asio::experimental::concurrent_channel; 23 | 24 | namespace SMNetwork 25 | { 26 | class NETWORKINTERFACE_EXPORT FullDuplexChannel 27 | { 28 | public: 29 | explicit FullDuplexChannel(int sock, shared_ptr)>> sender, shared_ptr)>> recv); 30 | FullDuplexChannel(FullDuplexChannel& copy); 31 | FullDuplexChannel(FullDuplexChannel&& right); 32 | ~FullDuplexChannel(); 33 | 34 | asio::awaitable sendPack(string_view src, uint32_t no); 35 | asio::awaitable>> recvPack(); 36 | 37 | uint32_t sockNo(); 38 | void setSockNo(uint32_t sockno); 39 | private: 40 | int _socket; 41 | SMNetwork::StreamNetDealer _netpack; 42 | 43 | shared_ptr)>> _sender; 44 | shared_ptr)>> _recv; 45 | }; 46 | } // namespace SMNetwork 47 | 48 | -------------------------------------------------------------------------------- /networkinterface/FullDuplexChannelManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace SMNetwork 4 | { 5 | class FullDuplexChannelManager 6 | { 7 | public: 8 | FullDuplexChannelManager() 9 | { 10 | 11 | } 12 | 13 | private: 14 | 15 | }; 16 | } // namespace SMNetwork 17 | 18 | -------------------------------------------------------------------------------- /networkinterface/Https.cpp: -------------------------------------------------------------------------------- 1 | #include "Https.h" 2 | #include "Utils.h" 3 | #include "FilePathMonitor.h" 4 | #include "Configs.h" 5 | #include "wss.h" 6 | #include "spdlog/spdlog.h" 7 | #include "asio/signal_set.hpp" 8 | #include "networkinterface.h" 9 | #include "asio/co_spawn.hpp" 10 | #include "asio/detached.hpp" 11 | #include "asio/awaitable.hpp" 12 | #include "jwt-cpp/jwt.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | using std::make_shared; 19 | using std::string; 20 | 21 | namespace SMNetwork 22 | { 23 | static tsl::htrie_map _contentTypes = { 24 | {".js", HtmlBodyType::JS}, 25 | {".html", HtmlBodyType::Html}, 26 | {".htm", HtmlBodyType::Html}, 27 | {".json", HtmlBodyType::Json}, 28 | {".woff", HtmlBodyType::Woff}, 29 | {".jpg", HtmlBodyType::Jpeg}, 30 | {".jpeg", HtmlBodyType::Jpeg}, 31 | {".css", HtmlBodyType::Css}, 32 | {".ico", HtmlBodyType::Ico} }; 33 | static map> _respHeaders; 34 | static map _resHeaders; 35 | static string _noAuthRep{ "need auth" }; 36 | 37 | 38 | 39 | map& Https::getRespHeaders(HtmlBodyType type) 40 | { 41 | static map defaultheaders; 42 | auto it = _respHeaders.find(type); 43 | if (it != _respHeaders.end()) 44 | { 45 | return it->second; 46 | } 47 | return defaultheaders; 48 | } 49 | 50 | HtmlBodyType Https::getRespBodyType(const string& context) 51 | { 52 | return getRespBodyType(string_view(context.data(), context.length())); 53 | } 54 | 55 | HtmlBodyType Https::getRespBodyType(string_view context) 56 | { 57 | HtmlBodyType ret = HtmlBodyType::Html; 58 | auto it = _contentTypes.find(context); 59 | if (it != _contentTypes.end()) 60 | { 61 | ret = it.value(); 62 | } 63 | return ret; 64 | } 65 | 66 | void Https::sInit() 67 | { 68 | _respHeaders[HtmlBodyType::JS].insert({ "content-type", "text/javascript" }); 69 | 70 | _respHeaders[HtmlBodyType::Html].insert({ "content-type", "text/html;charset=UTF-8" }); 71 | _respHeaders[HtmlBodyType::Json].insert({ "content-type", "application/json" }); 72 | _respHeaders[HtmlBodyType::Woff].insert({ "content-type", "font/woff" }); 73 | _respHeaders[HtmlBodyType::Jpeg].insert({ "content-type", "image/jpeg" }); 74 | _respHeaders[HtmlBodyType::Css].insert({ "content-type", "text/css" }); 75 | _respHeaders[HtmlBodyType::Ico].insert({ "content-type", "image/vnd.microsoft.icon" }); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /networkinterface/Https.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /** 3 | * @file Https.h 4 | * @author alqaz (alqaz@outlook.com) 5 | * @brief class for manage http service node. function init(...) and startServer(...) shoud call at the same thread as 6 | uWesocket implement. 7 | * @version 0.1 8 | * @date 2022-01-27 9 | * 10 | * @copyright Copyright (c) 2022 11 | * 12 | */ 13 | 14 | #include "consts.h" 15 | #include "networkinterfaceExport.h" 16 | #include "coros.h" 17 | #include "uwebsockets/App.h" 18 | #include "Utils.h" 19 | #include "asio/io_context.hpp" 20 | #include "taskflow/taskflow.hpp" 21 | #include "tsl/htrie_map.h" 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | using std::shared_ptr; 29 | using std::vector; 30 | using std::string; 31 | 32 | 33 | class DBManager; 34 | namespace SMNetwork 35 | { 36 | class NETWORKINTERFACE_EXPORT Https 37 | { 38 | public: 39 | static map& getRespHeaders(HtmlBodyType type); 40 | static HtmlBodyType getRespBodyType(const string& context); 41 | static HtmlBodyType getRespBodyType(string_view context); 42 | static void sInit(); 43 | 44 | }; 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /networkinterface/LocalNetChannel.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "LocalNetChannel.h" 3 | #include "asio/co_spawn.hpp" 4 | #include "asio/detached.hpp" 5 | 6 | namespace SMNetwork 7 | { 8 | LocalNetChannel::LocalNetChannel(int index) 9 | { 10 | _notifyRecv = make_shared(*IOCTX); 11 | } 12 | void LocalNetChannel::start() 13 | { 14 | asio::co_spawn(*IOCTX, [this]()->asio::awaitable { 15 | while (_brun) 16 | { 17 | co_await _notifyRecv->async_time_wait(); 18 | } 19 | }, asio::detached); 20 | } 21 | void LocalNetChannel::stop() 22 | { 23 | asio::co_spawn(*IOCTX, [this]()->asio::awaitable { 24 | this->_brun = false; 25 | _notifyRecv->trigger(); 26 | co_return; 27 | }, asio::detached); 28 | } 29 | void LocalNetChannel::sendMessage(shared_ptr msg) 30 | { 31 | asio::co_spawn(*IOCTX, [this, msg]()->asio::awaitable { 32 | auto neednotify = _recvFrames.empty(); 33 | _recvFrames.push_back(msg); 34 | if (neednotify) 35 | { 36 | _notifyRecv->trigger(); 37 | } 38 | co_return; 39 | }, asio::detached); 40 | } 41 | asio::awaitable> LocalNetChannel::recvPack() 42 | { 43 | shared_ptr ret{nullptr}; 44 | if (_recvFrames.empty()) 45 | { 46 | co_await _notifyRecv->async_time_wait(); 47 | } 48 | if (!_recvFrames.empty()) 49 | { 50 | ret = *(_recvFrames.begin()); 51 | _recvFrames.pop_front(); 52 | } 53 | co_return ret; 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /networkinterface/LocalNetChannel.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "asio/awaitable.hpp" 4 | #include "CoEvent.h" 5 | #include 6 | #include 7 | #include 8 | using std::string; 9 | using std::shared_ptr; 10 | using std::list; 11 | 12 | namespace SMNetwork 13 | { 14 | class NETWORKINTERFACE_EXPORT LocalNetChannel 15 | { 16 | public: 17 | LocalNetChannel(int index); 18 | void start(); 19 | void stop(); 20 | void sendMessage(shared_ptr msg); 21 | asio::awaitable> recvPack(); 22 | private: 23 | shared_ptr _notifyRecv{ nullptr }; 24 | list> _recvFrames; 25 | int _index{ -1 }; 26 | bool _brun{ true }; 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /networkinterface/LocalNetManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "CoEvent.h" 4 | #include "asio/detached.hpp" 5 | #include "asio/co_spawn.hpp" 6 | #include "asio/experimental/concurrent_channel.hpp" 7 | #include "FullDuplexChannel.h" 8 | #include "ChannelCombine.h" 9 | #include "NMessage.h" 10 | #include "oneshot.hpp" 11 | #include "../networkinterface/socket/TcpChannelImp.h" 12 | #include "../networkinterface/nngs/AsynRepImpl.h" 13 | #include "../networkinterface/nngs/ReqImpl.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | using std::map; 21 | using std::shared_ptr; 22 | using std::string; 23 | using std::list; 24 | using std::tuple; 25 | using std::make_tuple; 26 | using std::pair; 27 | using asio::experimental::concurrent_channel; 28 | 29 | namespace SMNetwork 30 | { 31 | /** 32 | * @brief class for manage full duplex channel 33 | * 34 | */ 35 | class NETWORKINTERFACE_EXPORT LocalNetManager 36 | { 37 | public: 38 | static bool sInit(); 39 | static void sUninit(); 40 | public: 41 | LocalNetManager(); 42 | static LocalNetManager* GetInst2(); 43 | asio::awaitable sendPack(int sock, shared_ptr msg); 44 | asio::awaitable sendPack(int sock, shared_ptr msg); 45 | asio::awaitable> recvPack(int sock); 46 | 47 | 48 | tuple>, shared_ptr>> createPair(int mainc); 49 | ChannelCombine* createTcpChannel(unique_ptr sock, ChannelModeC channal, int mainc); 50 | bool createRep(string_view addr, ChannelModeC channel, int mainc, int cur); 51 | ChannelCombine* createReq(string_view addr, ChannelModeC channel, int mainc); 52 | 53 | protected: 54 | asio::awaitable _sendTcp(int sock, shared_ptr msg); 55 | asio::awaitable _sendRep(int sock, shared_ptr msg); 56 | asio::awaitable _sendReq(int sock, shared_ptr msg); 57 | private: 58 | map _channelPairs; 59 | map>> _channels; 60 | map)>>> _senddata; 61 | 62 | map>> _channelsTcp; 63 | map>> _channelsRep; 64 | map>> _channelsReq; 65 | }; 66 | } 67 | 68 | #define LNM (SMNetwork::LocalNetManager::GetInst2()) 69 | -------------------------------------------------------------------------------- /networkinterface/MainPlatformDealer.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "MainPlatformDealer.h" 3 | #include "magic_enum.hpp" 4 | #include "spdlog/spdlog.h" 5 | #include "hotupdate.h" 6 | #include "Routers.h" 7 | #include 8 | #include 9 | 10 | using std::string; 11 | 12 | namespace SMNetwork 13 | { 14 | MainPlatformDealer::MainPlatformDealer() :_main(-1) 15 | { 16 | 17 | } 18 | 19 | MainPlatformDealer::MainPlatformDealer(uint32_t mainc) :_main(mainc) 20 | { 21 | 22 | } 23 | 24 | uint32_t MainPlatformDealer::getMain() 25 | { 26 | return _main; 27 | } 28 | 29 | void MainPlatformDealer::setMain(uint32_t mainc) 30 | { 31 | _main = mainc; 32 | } 33 | 34 | int MainPlatformDealer::getAss() 35 | { 36 | assert(0); 37 | SPDLOG_WARN("this platform dealer only need main cmd, not need get ass cmd"); 38 | return 0; 39 | } 40 | 41 | void MainPlatformDealer::setAss(int ass) 42 | { 43 | assert(0); 44 | SPDLOG_WARN("this platform dealer only need main cmd, not need set ass cmd"); 45 | } 46 | 47 | 48 | 49 | void MainPlatformDealer::pack(uint32_t& no, size_t len, span dst) 50 | { 51 | 52 | } 53 | 54 | uint32_t MainPlatformDealer::getNo() 55 | { 56 | return _no; 57 | } 58 | 59 | 60 | bool MainPlatformDealer::unpack(uint32_t& no, string_view src) 61 | { 62 | bool bret{ true }; 63 | 64 | return bret; 65 | } 66 | 67 | int MainPlatformDealer::HeadLen() 68 | { 69 | return sizeof(int); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /networkinterface/MainPlatformDealer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include 4 | #include 5 | #include 6 | 7 | using std::string_view; 8 | using std::span; 9 | 10 | namespace SMNetwork 11 | { 12 | class NETWORKINTERFACE_EXPORT MainPlatformDealer 13 | { 14 | public: 15 | MainPlatformDealer(); 16 | MainPlatformDealer(uint32_t mainc); 17 | int HeadLen(); 18 | void setMain(uint32_t mainc); 19 | uint32_t getMain(); 20 | void setAss(int ass); 21 | bool unpack(uint32_t& no, string_view src); 22 | void pack(uint32_t& no, size_t len, span dst); 23 | int getAss(); 24 | uint32_t getNo(); 25 | private: 26 | int _len; 27 | uint32_t _no; 28 | uint32_t _main; 29 | }; 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /networkinterface/MesageHandlerBase.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "networkinterfaceExport.h" 3 | #include "MesageHandlerBase.h" 4 | #include "Utils.h" 5 | #include "asio/ip/tcp.hpp" 6 | #include "coros.h" 7 | 8 | #include 9 | 10 | using asio::ip::tcp; 11 | using std::shared_ptr; 12 | using std::function; 13 | using std::string; 14 | namespace SMNetwork 15 | { 16 | MesageHandlerBase::MesageHandlerBase() 17 | { 18 | } 19 | 20 | asio::awaitable MesageHandlerBase::messageTask(string_view msg) 21 | { 22 | string ret = syncDealMsg(msg); 23 | 24 | co_return ret; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /networkinterface/MesageHandlerBase.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "Utils.h" 4 | #include "asio/ip/tcp.hpp" 5 | #include "coros.h" 6 | #include "taskflow/taskflow.hpp" 7 | #include 8 | #include 9 | 10 | using asio::ip::tcp; 11 | using std::shared_ptr; 12 | using std::function; 13 | using std::string; 14 | using std::string_view; 15 | 16 | namespace SMNetwork 17 | { 18 | class NETWORKINTERFACE_EXPORT MesageHandlerBase 19 | { 20 | public: 21 | MesageHandlerBase(); 22 | virtual string syncDealMsg(string_view msg)=0; 23 | asio::awaitable messageTask(string_view msg); 24 | void stop() { 25 | _setStopFlag(true); 26 | } 27 | bool isStop() { 28 | return _bstop; 29 | } 30 | 31 | protected: 32 | void _setStopFlag(bool bflag) { 33 | _bstop = bflag; 34 | } 35 | private: 36 | bool _bstop = false; 37 | }; 38 | } 39 | -------------------------------------------------------------------------------- /networkinterface/MessageRouter.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include "networkinterfaceExport.h" 4 | #include "networkinterface.h" 5 | #include "NMessage.h" 6 | #include "asio/awaitable.hpp" 7 | #include "asio/experimental/concurrent_channel.hpp" 8 | #include "oneshot.hpp" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | using std::shared_ptr; 18 | using asio::experimental::concurrent_channel; 19 | using std::map; 20 | using std::tuple; 21 | using std::optional; 22 | using std::move; 23 | 24 | namespace SMNetwork 25 | { 26 | 27 | 28 | class MessageRouter 29 | { 30 | public: 31 | static bool sInit(); 32 | static void sUninit(); 33 | static MessageRouter* sInst(); 34 | public: 35 | MessageRouter(); 36 | 37 | PrepareReqRepRecver prepareReqRep(uint32_t sock, uint32_t msgno); 38 | bool finishReqRep(uint32_t sock, uint32_t msgno); 39 | 40 | asio::awaitable addPackFromIo(uint32_t sock, int mainc, shared_ptr msg); 41 | asio::awaitable addPackFromApp(uint32_t sock, shared_ptr msg); 42 | 43 | oneshot::receiver prepareSignalSendComplete(uint32_t sock, uint32_t msgno); 44 | oneshot::receiver> prepareSignalRecvNetRep(uint32_t sock, uint32_t msgno); 45 | 46 | 47 | bool notifyRecvNetRep(uint32_t sock, shared_ptr msg); 48 | bool notifyRecvAppRep(uint32_t sock, shared_ptr msg); 49 | bool notifySendNetComplete(uint32_t sock, uint32_t msgno, bool flag); 50 | oneshot::receiver> prepareSignalRecvAppRep(uint32_t sock, uint32_t msgno); 51 | 52 | bool canReqRep(uint32_t sockno, uint32_t msgno); 53 | bool canWaitAppRep(uint32_t sockno, uint32_t msgno); 54 | bool start(); 55 | private: 56 | map>> _sendCompleteSignals; 57 | map>>> _netRepSignals; 58 | map>>> _appRepSignals; 59 | 60 | //notify channel for recv data from network io 61 | shared_ptr)>> _dataio; 62 | 63 | //notify channel for send data to network io success 64 | shared_ptr> _notifyio; 65 | 66 | //notify channel for recv data from app 67 | shared_ptr)>> _dataapp; 68 | }; 69 | } 70 | 71 | #define MR (MessageRouter::sInst()) 72 | -------------------------------------------------------------------------------- /networkinterface/NMessage.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "NMessage.h" 3 | 4 | #include 5 | namespace SMNetwork 6 | { 7 | NMessage::NMessage(uint32_t no, shared_ptr msg) : _len(msg->length()), _no(no), _platBody(msg) 8 | { 9 | 10 | } 11 | NMessage::NMessage(uint32_t no, string_view msg) : _len(msg.length()), _no(no), _platBody(std::make_shared(msg.begin(), msg.end())) 12 | { 13 | 14 | } 15 | NMessage::NMessage(NMessage& copy) 16 | { 17 | assert(0); 18 | } 19 | 20 | uint32_t NMessage::Len() 21 | { 22 | return _len; 23 | } 24 | uint32_t NMessage::No() 25 | { 26 | return _no; 27 | } 28 | shared_ptr NMessage::body() 29 | { 30 | return _platBody; 31 | } 32 | void NMessage::body(shared_ptr body) 33 | { 34 | _platBody = body; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /networkinterface/NMessage.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using std::shared_ptr; 9 | using std::string_view; 10 | using std::string; 11 | 12 | namespace SMNetwork 13 | { 14 | class NETWORKINTERFACE_EXPORT NMessage 15 | { 16 | public: 17 | NMessage(uint32_t no, shared_ptr msg); 18 | NMessage(uint32_t no, string_view msg); 19 | NMessage(NMessage& copy); 20 | 21 | uint32_t Len(); 22 | uint32_t No(); 23 | shared_ptr body(); 24 | void body(shared_ptr body); 25 | private: 26 | uint32_t _len{ 0 }; 27 | uint32_t _no{ 0 }; 28 | shared_ptr _platBody{nullptr}; 29 | }; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /networkinterface/PackDealerBase.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "networkinterfaceExport.h" 5 | #include "networkinterface.h" 6 | #include "enums.h" 7 | #include "asio/awaitable.hpp" 8 | #include "asio/detached.hpp" 9 | 10 | #include "Utils.h" 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace SMNetwork 18 | { 19 | /** 20 | * @brief base class(interface) for all packdealer, packdealer is used to pack logic classify headers 21 | * and transport headers to message send from app and unpack transport and logic headers 22 | * message recv from transport and send to app. 23 | * 24 | */ 25 | class PackDealerBase 26 | { 27 | public: 28 | virtual ~PackDealerBase() {} 29 | virtual MainCmd getMainc() { assert(0); return MainCmd::MainCmdBegin; } 30 | virtual int getAssc() { assert(0); return 0xffff; } 31 | virtual ChannelType getChannelType() { assert(0); return ChannelType::EchoServer; } 32 | virtual void setAssc(int ) { assert(0); } 33 | virtual std::string_view getMsg() { assert(0); return string_view(); }; 34 | virtual std::string* unpack(seqNumType* seqnum, string& pack) {assert(0); return nullptr;}; 35 | virtual std::string* unpack(seqNumType* seqnum, std::string_view pack) { assert(0); return nullptr; }; 36 | virtual std::string* unpack(std::string_view pack) { assert(0); return nullptr; }; 37 | virtual std::string* pack(seqNumType* seqnum, std::string_view msg, uint16_t assc) {assert(0); return nullptr;}; 38 | virtual std::string* pack(seqNumType* seqnum, std::string_view msg) { assert(0); return nullptr; }; 39 | virtual std::string* pack(std::string_view msg) { assert(0); return nullptr; }; 40 | virtual asio::awaitable dealmsg(string& msg) { assert(0); co_return nullptr; }; 41 | virtual asio::awaitable dealmsgc(std::string& msg) 42 | { 43 | auto pstrreq = unpack(reqSeq(), msg); 44 | co_return pstrreq; 45 | }; 46 | virtual seqNumType* reqSeq() { assert(0); return nullptr; } 47 | virtual seqNumType* repSeq() { assert(0); return nullptr; } 48 | virtual PackDealerBase* clone() { assert(0); return nullptr; }; 49 | 50 | 51 | }; 52 | 53 | } 54 | -------------------------------------------------------------------------------- /networkinterface/PackDealerCommon.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include "PackDealerCommon.h" 5 | 6 | namespace SMNetwork 7 | { 8 | 9 | PackDealerCommon::PackDealerCommon(ChannelType channel):_channel(channel) 10 | { 11 | 12 | } 13 | 14 | std::string_view PackDealerCommon::getMsg() 15 | { 16 | return string_view{ _msg }; 17 | } 18 | 19 | seqNumType* PackDealerCommon::reqSeq() 20 | { 21 | return &_seqnumreq; 22 | } 23 | 24 | seqNumType* PackDealerCommon::repSeq() 25 | { 26 | return &_seqnumrep; 27 | } 28 | 29 | ChannelType PackDealerCommon::getChannelType() 30 | { 31 | return _channel; 32 | } 33 | 34 | PackDealerCommon::~PackDealerCommon() 35 | { 36 | 37 | } 38 | 39 | } 40 | 41 | -------------------------------------------------------------------------------- /networkinterface/PackDealerCommon.h: -------------------------------------------------------------------------------- 1 | /*****************************************************************//** 2 | * \file PackDealerCommon.h 3 | * \brief common implement for all pack dealers 4 | * 5 | * \author alqaz 6 | * \date February 2023 7 | *********************************************************************/ 8 | 9 | #pragma once 10 | 11 | #include "PackDealerBase.h" 12 | 13 | namespace SMNetwork 14 | { 15 | class NETWORKINTERFACE_EXPORT PackDealerCommon : public PackDealerBase 16 | { 17 | public: 18 | PackDealerCommon(ChannelType channel); 19 | virtual std::string_view getMsg() override; 20 | virtual seqNumType* reqSeq() override; 21 | virtual seqNumType* repSeq() override; 22 | virtual ChannelType getChannelType() override; 23 | virtual ~PackDealerCommon(); 24 | protected: 25 | seqNumType _seqnumreq{ 0 }; 26 | seqNumType _seqnumrep{ 0 }; 27 | ChannelType _channel; 28 | std::string _msg; 29 | }; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /networkinterface/PackDealerMainSub.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "PackDealerCommon.h" 5 | #include "Routers.h" 6 | #include "hotupdate.h" 7 | #include 8 | 9 | 10 | namespace SMNetwork 11 | { 12 | template 13 | class AppPlatformDealer 14 | { 15 | public: 16 | AppPlatformDealer(App app, Plat plat):_app(app), _plat(plat) 17 | { 18 | 19 | } 20 | asio::awaitable> dealMessage(shared_ptr req) 21 | { 22 | auto funcs = SMCONF::getRouterTransByMainC(_plat.getMain()); 23 | assert(funcs); 24 | _plat.unpack(string_view(*req)); 25 | auto func = funcs->find(_plat.getAss()); 26 | if (func == funcs->end()) 27 | { 28 | co_return nullptr; 29 | } 30 | RouterFuncReturnType ret; 31 | string appmsg(req->data() + _plat.HeadLen(), req->length() - _plat.HeadLen()); 32 | string token; 33 | BEGIN_ASIO; 34 | ret = co_await(*(func->second))(appmsg, token); 35 | END_ASIO; 36 | SPDLOG_INFO("send to client data: [{}], size:[{}]", *ret, ret->length()); 37 | if (ret->empty()) 38 | { 39 | SPDLOG_ERROR(" mainc = {}, assc = {} response 0", magic_enum::enum_name(_plat.getMain()), _plat.getAss()); 40 | co_return nullptr; 41 | } 42 | ret->reserve(ret->length() + _plat.HeadLen()); 43 | _plat.pack(span(ret->data() + ret->length(), _plat.HeadLen())); 44 | co_return ret; 45 | } 46 | private: 47 | App _app; 48 | Plat _plat; 49 | }; 50 | 51 | class NETWORKINTERFACE_EXPORT PackDealerMainSub : public PackDealerCommon 52 | { 53 | public: 54 | PackDealerMainSub(MainCmd mainc, ChannelType channel); 55 | PackDealerMainSub(const PackDealerMainSub& c); 56 | virtual void setAssc(int) override; 57 | virtual int getAssc() override; 58 | virtual MainCmd getMainc() override; 59 | 60 | virtual std::string* unpack(seqNumType* seqnum, string& pack) override; 61 | virtual std::string* unpack(seqNumType* seqnum, std::string_view pack) override; 62 | virtual std::string* unpack(std::string_view pack); 63 | virtual std::string* pack(seqNumType* seqnum, std::string_view msg) override; 64 | virtual std::string* pack(seqNumType* seqnum, std::string_view msg, uint16_t assc) override; 65 | virtual std::string* pack(std::string_view msg) override; 66 | virtual asio::awaitable dealmsg(string& msg) override; 67 | virtual PackDealerBase* clone() override; 68 | 69 | private: 70 | int _assc; 71 | MainCmd _mainc; 72 | }; 73 | } 74 | -------------------------------------------------------------------------------- /networkinterface/PackDealerNoHead.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "PackDealerCommon.h" 5 | 6 | namespace SMNetwork 7 | { 8 | class NETWORKINTERFACE_EXPORT PackDealerNoHead : public PackDealerCommon 9 | { 10 | public: 11 | PackDealerNoHead(ChannelType channel); 12 | PackDealerNoHead(const PackDealerNoHead& c); 13 | 14 | virtual std::string* unpack(seqNumType* seqnum, std::string& pack) override; 15 | virtual std::string* unpack(seqNumType* seqnum, std::string_view pack) override; 16 | virtual std::string* pack(seqNumType* seqnum, std::string_view msg) override; 17 | virtual asio::awaitable dealmsg(string& msg) override; 18 | virtual PackDealerBase* clone() override; 19 | }; 20 | 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /networkinterface/PackDealerNoMain.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "PackDealerCommon.h" 5 | 6 | namespace SMNetwork 7 | { 8 | class NETWORKINTERFACE_EXPORT PackDealerNoMain : public PackDealerCommon 9 | { 10 | public: 11 | PackDealerNoMain(ChannelType channel); 12 | PackDealerNoMain(const PackDealerNoMain& c); 13 | 14 | virtual int getAssc() override; 15 | virtual void setAssc(int assc) override; 16 | virtual std::string* unpack(seqNumType* seqnum, std::string& pack) override; 17 | virtual std::string* pack(seqNumType* seqnum, std::string_view msg, uint16_t assc) override; 18 | 19 | virtual asio::awaitable dealmsg(std::string& msg) override; 20 | virtual PackDealerBase* clone() override; 21 | 22 | private: 23 | uint16_t _assc; 24 | }; 25 | 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /networkinterface/PackUnpack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/networkinterface/PackUnpack.cpp -------------------------------------------------------------------------------- /networkinterface/PackUnpack.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include "networkinterfaceExport.h" 4 | #include "myconcept.h" 5 | 6 | namespace SMNetwork 7 | { 8 | class NETWORKINTERFACE_EXPORT PackUnpack 9 | { 10 | public: 11 | public: 12 | private: 13 | 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /networkinterface/PackUnpackManager.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "PackUnpackManager.h" 3 | 4 | #include "Utils.h" 5 | 6 | #include "spdlog/spdlog.h" 7 | #include "magic_enum.hpp" 8 | 9 | using std::make_shared; 10 | 11 | namespace SMNetwork 12 | { 13 | shared_ptr _sInst{nullptr}; 14 | 15 | bool PackUnpackManager::sInit() 16 | { 17 | if (_sInst != nullptr) 18 | { 19 | return false; 20 | } 21 | _sInst = make_shared(); 22 | return true; 23 | } 24 | 25 | PackUnpackManager* PackUnpackManager::sInst() 26 | { 27 | assert(_sInst != nullptr); 28 | if (_sInst == nullptr) 29 | { 30 | SPDLOG_WARN("PackUnpackManager instance is null"); 31 | return nullptr; 32 | } 33 | return _sInst.get(); 34 | } 35 | 36 | bool PackUnpackManager::sUninit() 37 | { 38 | if (_sInst == nullptr) 39 | { 40 | return false; 41 | } 42 | _sInst = nullptr; 43 | return true; 44 | } 45 | 46 | bool PackUnpackManager::addPlatformPack(int mainc, shared_ptr pack) 47 | { 48 | bool bret{ false }; 49 | if (_platPacks.find(mainc) != _platPacks.end()) 50 | { 51 | SPDLOG_WARN("main cmd {} have other platform pack", mainc); 52 | return bret; 53 | } 54 | _platPacks[mainc] = pack; 55 | bret = true; 56 | return bret; 57 | } 58 | 59 | shared_ptr PackUnpackManager::clone(int mainc) 60 | { 61 | shared_ptr ret{nullptr}; 62 | auto it = _platPacks.find(mainc); 63 | assert(it != _platPacks.end()); 64 | if (it != _platPacks.end()) 65 | { 66 | ret = shared_ptr(it->second->clone()); 67 | } 68 | return ret; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /networkinterface/PackUnpackManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "myconcept.h" 4 | #include "PlatformPackInterface.h" 5 | #include "magic_enum.hpp" 6 | #include "spdlog/spdlog.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using std::span; 16 | using std::string_view; 17 | using std::map; 18 | using std::shared_ptr; 19 | 20 | namespace SMNetwork 21 | { 22 | 23 | class PackUnpackManager 24 | { 25 | public: 26 | static bool sInit(); 27 | static PackUnpackManager* sInst(); 28 | static bool sUninit(); 29 | public: 30 | bool addPlatformPack(int mainc, shared_ptr pack); 31 | shared_ptr clone(int mainc); 32 | private: 33 | map> _platPacks; 34 | }; 35 | 36 | } 37 | 38 | #define PUM (SMNetwork::PackUnpackManager::sInst()) 39 | 40 | -------------------------------------------------------------------------------- /networkinterface/PlatformPackInterface.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include 4 | #include 5 | #include 6 | 7 | using std::span; 8 | using std::string_view; 9 | 10 | namespace SMNetwork 11 | { 12 | class PlatformPackInterface 13 | { 14 | public: 15 | virtual PlatformPackInterface* clone() 16 | { 17 | assert(0); 18 | return nullptr; 19 | } 20 | virtual int getMain() 21 | { 22 | assert(0); 23 | return 0; 24 | }; 25 | virtual int getAss() 26 | { 27 | assert(0); 28 | return 0; 29 | }; 30 | virtual void setAss(int ass) 31 | { 32 | (void)(ass); 33 | assert(0); 34 | }; 35 | virtual bool pack(span dst) 36 | { 37 | (void)(dst); 38 | assert(0); 39 | return false; 40 | }; 41 | virtual size_t len() 42 | { 43 | assert(0); 44 | return 0; 45 | } 46 | virtual size_t HeadLen() 47 | { 48 | assert(0); 49 | return 0; 50 | } 51 | virtual bool unpack(string_view src) 52 | { 53 | (void)(src); 54 | assert(0); 55 | return false; 56 | }; 57 | virtual ~PlatformPackInterface() {} 58 | }; 59 | } 60 | -------------------------------------------------------------------------------- /networkinterface/ReadMe.txt: -------------------------------------------------------------------------------- 1 | 网络库说明: 2 | 网络库整合使用nanomsg, http, websocket, 基于各个协议的特点 分成一下两类 3 | 1, http(post,get) 使用 HttpGetPost 来实现(消息使用url来区分) 4 | 2, websocket, nanomsg 消息头包含在消息的开始固定长度字节来区分,使用base64编码 -------------------------------------------------------------------------------- /networkinterface/ServerBase.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "Utils.h" 4 | #include "coros.h" 5 | #include "uwebsockets/App.h" 6 | #include "enums.h" 7 | 8 | 9 | namespace SMNetwork 10 | { 11 | class NETWORKINTERFACE_EXPORT ServerBase 12 | { 13 | public: 14 | ServerBase() { 15 | 16 | } 17 | ServerBase(const ServerBase& cons) = delete; 18 | ServerBase(const ServerBase&& cons) = delete; 19 | virtual void init() = 0; 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /networkinterface/ServerCommon.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ServerCommon.h" 3 | 4 | namespace SMNetwork 5 | { 6 | 7 | ServerCommon::ServerCommon() 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /networkinterface/ServerCommon.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "ServerBase.h" 5 | 6 | namespace SMNetwork 7 | { 8 | class ServerCommon : public ServerBase 9 | { 10 | public: 11 | ServerCommon(); 12 | }; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /networkinterface/StreamHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "Utils.h" 4 | #include "asio/awaitable.hpp" 5 | #include "MessageSplitFuncs.h" 6 | #include "enums.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using asio::ip::tcp; 13 | using std::shared_ptr; 14 | using std::function; 15 | using std::string; 16 | using std::string_view; 17 | using std::deque; 18 | using std::tuple; 19 | using std::make_tuple; 20 | 21 | namespace SMNetwork 22 | { 23 | /** 24 | * @brief base class for stream protocol implement, need split message for recv and pack message header for send message 25 | * 26 | * @tparam ConnectType connect stream protocol implement(like tcp, tcp is a traditional stream protocol) 27 | */ 28 | template 29 | class StreamHandler 30 | { 31 | public: 32 | StreamHandler() { } 33 | virtual ~StreamHandler() { } 34 | virtual asio::awaitable> recvPack(uint16_t retrycount=50) = 0; 35 | virtual asio::awaitable sendPack(string_view msg) = 0; 36 | virtual shared_ptr getHandler() = 0; 37 | virtual string getFullAddr() = 0; 38 | virtual NetHeadType getNetHeadType() = 0; 39 | }; 40 | } 41 | -------------------------------------------------------------------------------- /networkinterface/StreamNetDealer.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "StreamNetDealer.h" 3 | #include "magic_enum.hpp" 4 | #include "spdlog/spdlog.h" 5 | #include "hotupdate.h" 6 | #include "Routers.h" 7 | #include 8 | #include 9 | 10 | using std::string; 11 | 12 | namespace SMNetwork 13 | { 14 | StreamNetDealer::StreamNetDealer() 15 | { 16 | 17 | } 18 | 19 | int StreamNetDealer::HeadLen() 20 | { 21 | return sizeof(_len) + sizeof(_no); 22 | } 23 | 24 | int StreamNetDealer::Len() 25 | { 26 | return _len; 27 | } 28 | 29 | bool StreamNetDealer::unpack(uint32_t& no, string_view src) 30 | { 31 | bool bret{ false }; 32 | uint32_t temp{ 0 }; 33 | auto [bbret, tail] = SMUtils::unpackuint32(src, temp); 34 | if (bbret) 35 | { 36 | _len = (int)(temp); 37 | auto [bbret, _] = SMUtils::unpackuint32(tail, temp); 38 | if (bbret) 39 | { 40 | _no = temp; 41 | bret = true; 42 | } 43 | } 44 | else 45 | { 46 | 47 | } 48 | return bret; 49 | } 50 | 51 | void StreamNetDealer::pack(uint32_t& no, size_t len, span dst) 52 | { 53 | _len = len; 54 | _no = no; 55 | SMUtils::packuint32(span(dst.data(), sizeof(_len)), _len); 56 | SMUtils::packuint32(span(dst.data() + sizeof(_len), sizeof(_no)), _no); 57 | } 58 | 59 | uint32_t StreamNetDealer::getNo() 60 | { 61 | return _no; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /networkinterface/StreamNetDealer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include 4 | #include 5 | #include 6 | 7 | using std::string_view; 8 | using std::span; 9 | 10 | namespace SMNetwork 11 | { 12 | class NETWORKINTERFACE_EXPORT StreamNetDealer 13 | { 14 | public: 15 | StreamNetDealer(); 16 | int HeadLen(); 17 | int Len(); 18 | bool unpack(uint32_t& no, string_view src); 19 | void pack(uint32_t& no, size_t len, span dst); 20 | uint32_t getNo(); 21 | private: 22 | int _len; 23 | uint32_t _no; 24 | }; 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /networkinterface/dealers/MainAssPlatPack.cpp: -------------------------------------------------------------------------------- 1 | #include "MainAssPlatPack.h" 2 | #include "Utils.h" 3 | #include "spdlog/spdlog.h" 4 | #include 5 | 6 | 7 | namespace SMNetwork 8 | { 9 | MainAssPlatPack::MainAssPlatPack(int mainc) 10 | : _main(mainc) 11 | , _ass(0) 12 | { 13 | assert(sizeof(int) == sizeof(uint32_t)); 14 | } 15 | 16 | MainAssPlatPack::MainAssPlatPack(MainAssPlatPack& ref) 17 | :_main(ref._main) 18 | , _ass(ref._ass) 19 | { 20 | 21 | } 22 | 23 | MainAssPlatPack::~MainAssPlatPack() 24 | { 25 | 26 | } 27 | 28 | int MainAssPlatPack::getMain() 29 | { 30 | return static_cast(_main); 31 | } 32 | 33 | void MainAssPlatPack::setAss(int ass) 34 | { 35 | _ass = static_cast(ass); 36 | } 37 | 38 | int MainAssPlatPack::getAss() 39 | { 40 | return _ass; 41 | } 42 | 43 | bool MainAssPlatPack::pack(span dst) 44 | { 45 | bool bret{ true }; 46 | assert(dst.size() == len()); 47 | if (dst.size() != len()) 48 | { 49 | bret = false; 50 | SPDLOG_WARN("at pack platform pack head size {} != holder container size {}", len(), dst.size()); 51 | } 52 | else 53 | { 54 | SMUtils::packuint32(span(dst.data(), sizeof(uint32_t)), _main); 55 | SMUtils::packuint32(span(dst.data() + sizeof(uint32_t), sizeof(uint32_t)), _ass); 56 | } 57 | return bret; 58 | } 59 | 60 | bool MainAssPlatPack::unpack(string_view src) 61 | { 62 | bool bret{ true }; 63 | uint32_t temp; 64 | if (src.size() != len()) 65 | { 66 | bret = false; 67 | SPDLOG_WARN("at unpack platform pack head size {} != holder container size {}", len(), src.size()); 68 | } 69 | else 70 | { 71 | auto [bret, tail] = SMUtils::unpackuint32(src, temp); 72 | { 73 | _main = temp; 74 | auto [bret, _] = SMUtils::unpackuint32(tail, temp); 75 | _ass = temp; 76 | } 77 | } 78 | return bret; 79 | } 80 | 81 | size_t MainAssPlatPack::len() 82 | { 83 | return sizeof(_main) + sizeof(_ass); 84 | } 85 | 86 | size_t MainAssPlatPack::HeadLen() 87 | { 88 | return sizeof(_main) + sizeof(_ass); 89 | } 90 | 91 | PlatformPackInterface* MainAssPlatPack::clone() 92 | { 93 | return new MainAssPlatPack(*this); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /networkinterface/dealers/MainAssPlatPack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "PlatformPackInterface.h" 4 | 5 | 6 | 7 | namespace SMNetwork 8 | { 9 | class NETWORKINTERFACE_EXPORT MainAssPlatPack : public PlatformPackInterface 10 | { 11 | public: 12 | MainAssPlatPack(int mainc); 13 | MainAssPlatPack(MainAssPlatPack& ref); 14 | virtual ~MainAssPlatPack(); 15 | virtual int getMain() override; 16 | void setAss(int ass) override; 17 | int getAss() override; 18 | bool pack(span dst) override; 19 | bool unpack(string_view src) override; 20 | size_t len() override; 21 | size_t HeadLen() override; 22 | virtual PlatformPackInterface* clone() override; 23 | private: 24 | uint32_t _main; 25 | uint32_t _ass; 26 | }; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /networkinterface/dealers/NoMainAssPlatPack.cpp: -------------------------------------------------------------------------------- 1 | #include "NoMainAssPlatPack.h" 2 | #include 3 | 4 | namespace SMNetwork 5 | { 6 | NoMainAssPlatPack::NoMainAssPlatPack(int mainc) 7 | : _main(mainc) 8 | { 9 | assert(sizeof(int) == sizeof(uint32_t)); 10 | } 11 | 12 | NoMainAssPlatPack::NoMainAssPlatPack(NoMainAssPlatPack& ref) 13 | :_main(ref._main) 14 | { 15 | 16 | } 17 | 18 | NoMainAssPlatPack::~NoMainAssPlatPack() 19 | { 20 | 21 | } 22 | 23 | int NoMainAssPlatPack::getMain() 24 | { 25 | return static_cast(_main); 26 | } 27 | 28 | 29 | 30 | bool NoMainAssPlatPack::pack(span dst) 31 | { 32 | return true; 33 | } 34 | 35 | bool NoMainAssPlatPack::unpack(string_view src) 36 | { 37 | return true; 38 | } 39 | 40 | size_t NoMainAssPlatPack::len() 41 | { 42 | return 0; 43 | } 44 | 45 | PlatformPackInterface* NoMainAssPlatPack::clone() 46 | { 47 | return new NoMainAssPlatPack(*this); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /networkinterface/dealers/NoMainAssPlatPack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "PlatformPackInterface.h" 4 | 5 | namespace SMNetwork 6 | { 7 | 8 | class NETWORKINTERFACE_EXPORT NoMainAssPlatPack : public PlatformPackInterface 9 | { 10 | public: 11 | NoMainAssPlatPack(int mainc); 12 | NoMainAssPlatPack(NoMainAssPlatPack& ref); 13 | virtual ~NoMainAssPlatPack(); 14 | virtual int getMain() override; 15 | bool pack(span dst) override; 16 | bool unpack(string_view src) override; 17 | size_t len() override; 18 | virtual PlatformPackInterface* clone() override; 19 | private: 20 | uint32_t _main; 21 | }; 22 | } 23 | -------------------------------------------------------------------------------- /networkinterface/nngs/AsynRep.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "NNGCommBase.h" 4 | #include "networkinterface.h" 5 | #include "asio/awaitable.hpp" 6 | #include "AsynBase.h" 7 | 8 | #include 9 | #include 10 | using std::shared_ptr; 11 | using std::vector; 12 | using std::string; 13 | 14 | namespace SMNetwork 15 | { 16 | 17 | class NETWORKINTERFACE_EXPORT AsynRep : public NNGCommBase, public AsyncBase 18 | { 19 | 20 | public: 21 | AsynRep(string ip, uint16_t port, ChannelType ctype, NNgTransType trans = NNgTransType::TCP, NngSockImplType socktype= NngSockImplType::NormalSock, int concurrent=1024); 22 | void init(ServeMode mode, shared_ptr dealer); 23 | virtual void init(ServeMode mode) override; 24 | asio::awaitable messageTask(work* sock, nng_msg* msg); 25 | virtual ServeMode getServeMode(); 26 | virtual int concurrentcount() override; 27 | protected: 28 | friend work; 29 | 30 | private: 31 | ServeMode _serveMode; 32 | int _concurrent {1024}; 33 | vector> _cons; 34 | }; 35 | 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /networkinterface/nngs/AsynRepImpl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "asio/awaitable.hpp" 4 | #include "asio/experimental/concurrent_channel.hpp" 5 | #include "nng/nng.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using std::string_view; 14 | using std::unique_ptr; 15 | using std::tuple; 16 | using std::shared_ptr; 17 | using std::string; 18 | 19 | enum class EStateType 20 | { 21 | WaitReq, 22 | WaitRep, 23 | }; 24 | 25 | namespace SMNetwork 26 | { 27 | enum class NngStatus 28 | { 29 | INIT, //context initialized 30 | RECV, //context recv finish 31 | WAIT, //context wait response 32 | SEND, //context send response finish 33 | AIOERROR, //aio operation error 34 | }; 35 | 36 | class NETWORKINTERFACE_EXPORT AsynRepImpl 37 | { 38 | public: 39 | static void rep_callback(void* pRep); 40 | static void rep_cancel(nng_aio* aio, void* arg, int rv); 41 | public: 42 | AsynRepImpl( uint32_t sockNo); 43 | AsynRepImpl(const AsynRepImpl& ref) = delete; 44 | AsynRepImpl(AsynRepImpl&& right); 45 | asio::awaitable sendPack(string_view src, uint32_t no); 46 | asio::awaitable>> recvPack(); 47 | uint32_t sockNo(); 48 | void setSockNo(uint32_t sockno); 49 | void cleanReqRepCache(); 50 | public: 51 | nng_aio* _aio; 52 | nng_ctx _ctx; 53 | uint32_t _sockno; 54 | EStateType _state{ EStateType::WaitReq }; 55 | NngStatus _nStatus{ NngStatus::INIT }; 56 | nng_msg* _req{ nullptr }; 57 | nng_msg* _rep{ nullptr }; 58 | shared_ptr> _msgs; 59 | }; 60 | } 61 | -------------------------------------------------------------------------------- /networkinterface/nngs/AsynReq.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "networkinterfaceExport.h" 5 | #include "NNGCommBase.h" 6 | #include "AsynBase.h" 7 | 8 | #include "concurrentqueue/concurrentqueue.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | namespace SMNetwork 14 | { 15 | class NETWORKINTERFACE_EXPORT AsyncReq : public NNGCommBase, public AsyncBase 16 | { 17 | public: 18 | AsyncReq(string ip, uint16_t port, ChannelType ctype, NNgTransType trans = NNgTransType::TCP, NngSockImplType socktype = NngSockImplType::NormalSock, int concurrent = 1024); 19 | void init(ServeMode mode, shared_ptr dealer); 20 | virtual void init(ServeMode mode) override; 21 | virtual int concurrentcount() override; 22 | virtual asio::awaitable messageTask(work* sock, nng_msg* msg) override; 23 | asio::awaitable reqrep(std::string req); 24 | virtual shared_ptr getWorker() override; 25 | private: 26 | moodycamel::ConcurrentQueue> _cons; 27 | int _concurrentcount; 28 | 29 | ServeMode _mode; 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /networkinterface/nngs/AsynReqImpl.cpp: -------------------------------------------------------------------------------- 1 | #include "AsynReqImpl.h" 2 | #include "hotupdate.h" 3 | #include "magic_enum.hpp" 4 | #include "asio/co_spawn.hpp" 5 | #include "asio/detached.hpp" 6 | #include "spdlog/spdlog.h" 7 | 8 | using std::make_tuple; 9 | using std::make_shared; 10 | 11 | namespace SMNetwork 12 | { 13 | AsynReqImpl::AsynReqImpl(nng_socket addr, uint32_t sockNo) 14 | :_sock(addr) 15 | ,_sockno(sockNo) 16 | { 17 | _msgs = make_shared>(*IOCTX, 5); 18 | } 19 | 20 | AsynReqImpl::AsynReqImpl(AsynReqImpl&& right) 21 | :_sockno(right._sockno) 22 | { 23 | 24 | } 25 | 26 | asio::awaitable AsynReqImpl::sendPack(string_view src, uint32_t no) 27 | { 28 | bool bret{ false }; 29 | int nnop{ 0 }; 30 | 31 | co_return bret; 32 | } 33 | 34 | asio::awaitable>> AsynReqImpl::recvPack() 35 | { 36 | uint32_t msgno{ 0 }; 37 | shared_ptr msg{nullptr}; 38 | 39 | co_return make_tuple(msgno, msg); 40 | } 41 | 42 | uint32_t AsynReqImpl::sockNo() 43 | { 44 | return _sockno; 45 | } 46 | 47 | void AsynReqImpl::setSockNo(uint32_t sockno) 48 | { 49 | _sockno = sockno; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /networkinterface/nngs/AsynReqImpl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "asio/awaitable.hpp" 4 | #include "asio/experimental/concurrent_channel.hpp" 5 | #include "nng/nng.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using std::string_view; 14 | using std::unique_ptr; 15 | using std::tuple; 16 | using std::shared_ptr; 17 | using std::string; 18 | 19 | namespace SMNetwork 20 | { 21 | class AsynReqImpl 22 | { 23 | public: 24 | AsynReqImpl(nng_socket addr, uint32_t sockNo); 25 | AsynReqImpl(const AsynReqImpl& ref) = delete; 26 | AsynReqImpl(AsynReqImpl&& right); 27 | asio::awaitable sendPack(string_view src, uint32_t no); 28 | asio::awaitable>> recvPack(); 29 | uint32_t sockNo(); 30 | void setSockNo(uint32_t sockno); 31 | public: 32 | nng_socket _sock; 33 | uint32_t _sockno; 34 | nng_msg* _req{ nullptr }; 35 | nng_msg* _rep{ nullptr }; 36 | shared_ptr> _msgs; 37 | }; 38 | } 39 | -------------------------------------------------------------------------------- /networkinterface/nngs/NNGCommBase.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "NngServerBase.h" 5 | #include 6 | #include 7 | #include "enums.h" 8 | 9 | using std::string; 10 | 11 | namespace SMNetwork 12 | { 13 | 14 | class NNGCommBase : public NNgServerBase 15 | { 16 | public: 17 | NNGCommBase(string ip, uint16_t port, ChannelType type, NNgTransType trans, NngSockImplType socktype=NngSockImplType::NormalSock) 18 | : _channelType(type) 19 | ,_ip(ip) 20 | ,_port(port) 21 | , _trans(trans) 22 | ,_socktype(socktype) 23 | { 24 | 25 | } 26 | virtual ChannelType getChannelType() override 27 | { 28 | return _channelType; 29 | } 30 | 31 | virtual string getAddr() override 32 | { 33 | string ret; 34 | switch(_trans) 35 | { 36 | case NNgTransType::TCP: 37 | { 38 | ret = "tcp://"; 39 | }break; 40 | case NNgTransType::WS: 41 | { 42 | ret = "ws://"; 43 | }break; 44 | default: 45 | break; 46 | } 47 | ret += _ip; 48 | ret += ":"; 49 | ret += std::to_string(_port); 50 | return ret; 51 | } 52 | 53 | 54 | 55 | 56 | 57 | virtual NNgTransType getNNgTransType() override{ 58 | return _trans; 59 | } 60 | 61 | 62 | NngSockImplType Socktype() const { return _socktype; } 63 | void Socktype(NngSockImplType val) { _socktype = val; } 64 | protected: 65 | nng_socket _socket = {}; 66 | ChannelType _channelType; 67 | NngSockImplType _socktype; 68 | string _ip; 69 | uint16_t _port; 70 | PackType _packType; 71 | NNgTransType _trans; 72 | }; 73 | 74 | } 75 | 76 | -------------------------------------------------------------------------------- /networkinterface/nngs/NngRep.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /networkinterface/nngs/NngReq.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "enums.h" 3 | #include "myconcept.h" 4 | #include "ChannelCombine.h" 5 | #include "ReqImpl.h" 6 | 7 | #include "concurrentqueue/concurrentqueue.h" 8 | 9 | #include 10 | #include 11 | 12 | using std::shared_ptr; 13 | using std::string; 14 | 15 | /** 16 | * class for manage nng request. 17 | */ 18 | 19 | namespace SMNetwork 20 | { 21 | template 22 | class NngReq 23 | { 24 | public: 25 | NngReq(string addr) 26 | :_addr(addr) 27 | { 28 | 29 | } 30 | 31 | asio::awaitable*> getChannel() 32 | { 33 | ChannelCombine* retchannel = nullptr; 34 | string addr = _ip + ":" + std::to_string(_port); 35 | if (_channelpool.empty()) 36 | { 37 | auto timeoutmillsecond = SMCONF::getTransportConfig()->_timeout; 38 | if (timeoutmillsecond == 0) 39 | { 40 | timeoutmillsecond = 3000; 41 | SPDLOG_WARN("tcp connect time out time not set, use default value {} channel type {}", timeoutmillsecond, _strmainc); 42 | } 43 | int trycount = 0; 44 | while (++trycount <= 3) 45 | { 46 | SPDLOG_INFO("start get tcp connect channel from addr {} channel type {}", addr, _strmainc); 47 | asio::ip::tcp::endpoint endpoint(asio::ip::address::from_string(_ip), _port); 48 | unique_ptr sock = make_unique(*IOCTX); 49 | BEGIN_ASIO; 50 | co_await sock->async_connect(endpoint, asio::use_awaitable); 51 | uint32_t sockno = SMNetwork::newSockNo(); 52 | retchannel = LNM->createTcpChannel(move(sock), ChannelModeC::Initiative, MainCmd::DBQuery); 53 | retchannel->start(); 54 | break; 55 | END_ASIO; 56 | } 57 | } 58 | else 59 | { 60 | retchannel = _channelpool.front(); 61 | _channelpool.pop_front(); 62 | } 63 | if (retchannel == nullptr) 64 | { 65 | SPDLOG_WARN("connect addr [{}] failed channel type {}", addr, _strmainc); 66 | } 67 | co_return retchannel; 68 | } 69 | 70 | ChannelModeC channelType() 71 | { 72 | return modec; 73 | } 74 | private: 75 | string _addr; 76 | moodycamel::ConcurrentQueue>> _channels; 77 | }; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /networkinterface/nngs/NngServerBase.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | #include "networkinterfaceExport.h" 4 | #include "nng/nng.h" 5 | #include "MainCmdTag.h" 6 | #include "enums.h" 7 | #include "asio/awaitable.hpp" 8 | #include 9 | #include 10 | 11 | using std::string; 12 | using std::string_view; 13 | 14 | enum class NngSockType 15 | { 16 | SyncReq, 17 | SyncPush, 18 | SyncPull, 19 | AsynRep, 20 | }; 21 | 22 | namespace SMNetwork 23 | { 24 | 25 | class NNgServerBase : public MainCmdTag 26 | { 27 | public: 28 | NNgServerBase(){} 29 | NNgServerBase(const NNgServerBase& cons) = delete; 30 | NNgServerBase(const NNgServerBase&& cons) = delete; 31 | virtual ~NNgServerBase() {}; 32 | virtual ChannelType getChannelType() = 0; 33 | virtual string getAddr() = 0; 34 | virtual NNgTransType getNNgTransType() = 0; 35 | }; 36 | } 37 | -------------------------------------------------------------------------------- /networkinterface/nngs/RepManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "nng/nng.h" 4 | #include "myconcept.h" 5 | #include "hotupdate.h" 6 | #include "nng/protocol/reqrep0/rep.h" 7 | #include "ChannelCombine.h" 8 | #include "LocalNetManager.h" 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using std::shared_ptr; 15 | using std::vector; 16 | using std::string; 17 | using std::set; 18 | 19 | namespace SMNetwork 20 | { 21 | 22 | template 23 | class RepManager 24 | { 25 | public: 26 | RepManager(string ip, uint16_t port, MainType mainc, uint16_t cur=1) 27 | :_ip(ip) 28 | ,_port(port) 29 | ,_cur(cur) 30 | ,_mainc(mainc) 31 | { 32 | assert(_port != 0); 33 | } 34 | bool init() 35 | { 36 | _addr = fmt::format("tcp://{}:{}", _ip, _port); 37 | auto bret = LNM->createRep(string_view(_addr), ChannelModeC::Passive, magic_enum::enum_integer(_mainc), _cur); 38 | return bret; 39 | } 40 | private: 41 | string _ip; 42 | uint16_t _port{ 0 }; 43 | string _addr{}; 44 | uint16_t _cur{ 1024 }; 45 | nng_socket _nng; 46 | MainType _mainc; 47 | }; 48 | } 49 | -------------------------------------------------------------------------------- /networkinterface/nngs/ReqImpl.cpp: -------------------------------------------------------------------------------- 1 | #include "ReqImpl.h" 2 | #include "hotupdate.h" 3 | #include "magic_enum.hpp" 4 | #include "asio/co_spawn.hpp" 5 | #include "asio/detached.hpp" 6 | #include "spdlog/spdlog.h" 7 | #include "hotupdate.h" 8 | #include "oneshot.hpp" 9 | #include 10 | 11 | #include "asio/co_spawn.hpp" 12 | #include "asio/detached.hpp" 13 | #include "asio/use_awaitable.hpp" 14 | #include "asio/deferred.hpp" 15 | 16 | using std::make_tuple; 17 | using std::make_shared; 18 | using std::move; 19 | 20 | namespace SMNetwork 21 | { 22 | ReqImpl::ReqImpl(nng_socket sock, uint32_t sockNo) 23 | :_sockno(sockNo) 24 | ,_sock(sock) 25 | { 26 | } 27 | 28 | ReqImpl::ReqImpl(ReqImpl&& right) 29 | :_sockno(right._sockno) 30 | ,_sock(right._sock) 31 | { 32 | right._sockno=0; 33 | } 34 | 35 | asio::awaitable ReqImpl::sendPack(string_view src, uint32_t no) 36 | { 37 | bool bret{ false }; 38 | auto [s, r] = oneshot::create(); 39 | TFNET->silent_async([this, &s, src]() { 40 | auto nnop = nng_send(this->_sock, (void*)(src.data()), src.length(), 0); 41 | auto bflag = nnop == src.length(); 42 | asio::co_spawn(*IOCTX, [&s, bflag]()->asio::awaitable { 43 | s.send(bflag); 44 | co_return; 45 | }(), asio::detached); 46 | }); 47 | BEGIN_ASIO; 48 | co_await r.async_wait(asio::deferred); 49 | bret = r.get(); 50 | END_ASIO; 51 | _msgno = no; 52 | co_return bret; 53 | } 54 | 55 | asio::awaitable>> ReqImpl::recvPack() 56 | { 57 | uint32_t msgno{ 0 }; 58 | shared_ptr msg{nullptr}; 59 | auto [s, r] = oneshot::create(); 60 | TFNET->silent_async([this, &s]() { 61 | char* buf{ nullptr }; 62 | size_t len{ 0 }; 63 | auto nnop = nng_recv(this->_sock, &buf, &len, 0); 64 | assert(nnop > 0); 65 | asio::co_spawn(*IOCTX, [&s, buf, len]()->asio::awaitable { 66 | s.send(string_view(buf, len)); 67 | co_return; 68 | }(), asio::detached); 69 | }); 70 | BEGIN_ASIO; 71 | co_await r.async_wait(asio::deferred); 72 | auto msgv = r.get(); 73 | msg = make_shared(msgv.data(), msgv.length()); 74 | END_ASIO; 75 | co_return make_tuple(msgno, msg); 76 | } 77 | 78 | uint32_t ReqImpl::sockNo() 79 | { 80 | return _sockno; 81 | } 82 | 83 | void ReqImpl::setSockNo(uint32_t sockno) 84 | { 85 | _sockno = sockno; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /networkinterface/nngs/ReqImpl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "asio/awaitable.hpp" 4 | #include "asio/experimental/concurrent_channel.hpp" 5 | #include "nng/nng.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using std::string_view; 14 | using std::unique_ptr; 15 | using std::tuple; 16 | using std::shared_ptr; 17 | using std::string; 18 | 19 | 20 | namespace SMNetwork 21 | { 22 | class NETWORKINTERFACE_EXPORT ReqImpl 23 | { 24 | public: 25 | ReqImpl(nng_socket sock, uint32_t sockNo); 26 | ReqImpl(const ReqImpl& ref) = delete; 27 | ReqImpl(ReqImpl&& right); 28 | /** 29 | * send a pack. 30 | * 31 | * \param src message body 32 | * \param no message No. 33 | * \return if send success, return true; else return false 34 | */ 35 | asio::awaitable sendPack(string_view src, uint32_t no); 36 | /** 37 | * recv a pack. 38 | * 39 | * \return message No. and message body tuple 40 | */ 41 | asio::awaitable>> recvPack(); 42 | 43 | uint32_t sockNo(); 44 | 45 | void setSockNo(uint32_t sockno); 46 | 47 | public: 48 | nng_msg* _req{ nullptr }; 49 | nng_msg* _rep{ nullptr }; 50 | nng_socket _sock; 51 | uint32_t _sockno; 52 | uint32_t _msgno; 53 | }; 54 | } 55 | -------------------------------------------------------------------------------- /networkinterface/nngs/ReqManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "asio/awaitable.hpp" 4 | #include "magic_enum.hpp" 5 | #include "myconcept.h" 6 | #include "hotupdate.h" 7 | #include "ChannelCombine.h" 8 | #include "ReqImpl.h" 9 | #include "LocalNetManager.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using std::shared_ptr; 16 | using std::vector; 17 | using std::string; 18 | using std::string_view; 19 | using std::list; 20 | 21 | namespace SMNetwork 22 | { 23 | 24 | template 25 | class ReqManager 26 | { 27 | public: 28 | ReqManager(string_view addr, MainType mainc) 29 | :_addr(addr) 30 | ,_mainc(mainc) 31 | { 32 | 33 | } 34 | asio::awaitable*> getChannel() 35 | { 36 | ChannelCombine* ret{nullptr}; 37 | if (_channelPools.empty()) 38 | { 39 | ret = LNM->createReq(string_view(_addr), modelc, magic_enum::enum_integer(_mainc)); 40 | } 41 | else 42 | { 43 | ret = _channelPools.front(); 44 | _channelPools.pop_front(); 45 | } 46 | co_return ret; 47 | } 48 | private: 49 | string _addr; 50 | list*> _channelPools; 51 | 52 | MainType _mainc; 53 | }; 54 | } 55 | -------------------------------------------------------------------------------- /networkinterface/nngs/ReqRepRaw.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ReqRepRaw.h" 3 | -------------------------------------------------------------------------------- /networkinterface/nngs/ReqRepRaw.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | class ReqRepRaw 5 | { 6 | ReqRepRaw(); 7 | }; 8 | -------------------------------------------------------------------------------- /networkinterface/nngs/SyncRep2.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "asio/awaitable.hpp" 4 | #include "nng/nng.h" 5 | #include "nng/protocol/reqrep0/rep.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | using std::shared_ptr; 12 | using std::vector; 13 | using std::string; 14 | 15 | namespace SMNetwork 16 | { 17 | 18 | template 19 | class SyncRep2 20 | { 21 | public: 22 | SyncRep2(std::string addr, uint16_t port) 23 | :_ip(addr) 24 | ,_port(port) 25 | , _concurrent(concurrent) 26 | { 27 | 28 | } 29 | void init() 30 | { 31 | std::thread th[&,this](){ 32 | while(_brun){ 33 | nng_socket sock; 34 | nng_listener listener; 35 | int rv; 36 | int count = 0; 37 | string addr = "tcp://" 38 | addr += _ip; 39 | addr += ":"; 40 | addr += std::to_string(_port); 41 | if ((rv = nng_rep0_open(&sock)) != 0) { 42 | //log 43 | } 44 | 45 | if ((rv = nng_listener_create(&listener, sock, addr.c_str())) != 0) { 46 | //log 47 | } 48 | nng_listener_start(listener, 0); 49 | for (;;) { 50 | char * buf = NULL; 51 | size_t sz; 52 | if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { 53 | } 54 | this->_dealer.dealMsg(string_view(buf, sz)); 55 | rv = nng_send(sock, buf, sz, NNG_FLAG_ALLOC); 56 | nng_free(buf, sz); 57 | } 58 | } 59 | }; 60 | th.detach(); 61 | } 62 | 63 | 64 | private: 65 | string _ip; 66 | uint16_t _port; 67 | 68 | T _dealer; 69 | bool _brun = true; 70 | }; 71 | 72 | } 73 | -------------------------------------------------------------------------------- /networkinterface/nngs/SyncReq.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "networkinterfaceExport.h" 5 | #include "NNGCommBase.h" 6 | #include "PackDealerBase.h" 7 | #include "enums.h" 8 | #include 9 | #include 10 | #include 11 | 12 | using std::string; 13 | using std::string_view; 14 | using std::shared_ptr; 15 | 16 | namespace SMNetwork 17 | { 18 | class NETWORKINTERFACE_EXPORT SyncReq : public NNGCommBase 19 | { 20 | public: 21 | SyncReq(string ip, uint16_t port, ChannelType ctype, NNgTransType trans = NNgTransType::TCP, NngSockImplType socktype = NngSockImplType::NormalSock); 22 | virtual void init(ServeMode mode) override; 23 | void init(ServeMode mode, shared_ptr dealer); 24 | std::string reqrep(string_view req, uint16_t assc); 25 | private: 26 | std::shared_ptr _dealer; 27 | ServeMode _mode; 28 | }; 29 | } 30 | -------------------------------------------------------------------------------- /networkinterface/socket/TcpChannelImp.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "networkinterfaceExport.h" 3 | #include "asio/awaitable.hpp" 4 | #include "asio/buffer.hpp" 5 | #include "asio/ip/tcp.hpp" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using std::string; 12 | using std::unique_ptr; 13 | using std::array; 14 | using std::tuple; 15 | using std::string_view; 16 | using std::shared_ptr; 17 | 18 | namespace SMNetwork 19 | { 20 | class NETWORKINTERFACE_EXPORT TcpChannelImpl 21 | { 22 | public: 23 | TcpChannelImpl(unique_ptr sock, uint32_t sockNo); 24 | TcpChannelImpl(TcpChannelImpl&& right); 25 | /** 26 | * send a pack. 27 | * 28 | * \param src message body 29 | * \param no message No. 30 | * \return if send success, return true; else return false 31 | */ 32 | asio::awaitable sendPack(string_view src, uint32_t no); 33 | /** 34 | * recv a pack. 35 | * 36 | * \return message No. and message body tuple 37 | */ 38 | asio::awaitable>> recvPack(); 39 | 40 | uint32_t sockNo(); 41 | void setSockNo(uint32_t sockno); 42 | private: 43 | unique_ptr _sock{ nullptr }; 44 | unique_ptr _bRecv; 45 | unique_ptr _bRecvMsg; 46 | array _recvasio; 47 | int _retryCount{ 2 }; 48 | int _sockNo{ -1 }; 49 | }; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /networkinterface/socket/TcpStreamHandler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../configs/Configs.h" 3 | #include "Utils.h" 4 | #include "IOContextManager.h" 5 | #include "MesageHandlerBase.h" 6 | #include "StreamHandler.h" 7 | #include "coros.h" 8 | #include "asio/local/stream_protocol.hpp" 9 | 10 | 11 | #include "MessageSplitFuncs.h" 12 | #include "fmt/core.h" 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using asio::ip::tcp; 20 | using asio::steady_timer; 21 | using std::shared_ptr; 22 | using std::function; 23 | using std::string; 24 | using std::string_view; 25 | using std::vector; 26 | using std::tuple; 27 | using std::make_tuple; 28 | 29 | namespace SMNetwork 30 | { 31 | class NETWORKINTERFACE_EXPORT TcpStreamHandler : public StreamHandler 32 | { 33 | public: 34 | TcpStreamHandler()=delete; 35 | TcpStreamHandler(shared_ptr sock, NetHeadType nettype); 36 | TcpStreamHandler(const TcpStreamHandler&) = delete; 37 | TcpStreamHandler(TcpStreamHandler&& c); 38 | TcpStreamHandler& operator = (TcpStreamHandler&& c); 39 | virtual ~TcpStreamHandler(); 40 | 41 | virtual shared_ptr getHandler() override; 42 | 43 | virtual string getFullAddr() override; 44 | 45 | virtual asio::awaitable> recvPack(uint16_t retrycount = 60) override; 46 | virtual asio::awaitable sendPack(string_view msg) override; 47 | 48 | virtual NetHeadType getNetHeadType() override; 49 | 50 | protected: 51 | 52 | void _packnethead(uint32_t packlen); 53 | 54 | shared_ptr _socket; 55 | NetHeadType _nethead; 56 | string _fullAddr; 57 | uint32_t _curpacklen = 0; 58 | std::string _recvbuf; 59 | std::array _recvasio; 60 | std::string _recvmsg; 61 | std::string _recvmsgbin; //for binary protocol 62 | std::string _sendbuf; 63 | }; 64 | } 65 | -------------------------------------------------------------------------------- /networkinterface/wss.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #include "uwebsockets/App.h" 5 | 6 | 7 | struct WebSocketData 8 | { 9 | bool valid = false; 10 | int per; 11 | }; 12 | -------------------------------------------------------------------------------- /rayX/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(rayX) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | include_directories(../configs ../database ../Utils ../inc ../.. ../inc/networks ./ ) 7 | 8 | 9 | FILE(GLOB_RECURSE CUR_HEADER "./*.h") 10 | source_group("IncCur" FILES ${CUR_HEADER}) 11 | 12 | FILE(GLOB_RECURSE COMMN_HEADER "../inc/*.h") 13 | source_group("IncComm" FILES ${COMMN_HEADER}) 14 | 15 | include_directories(../conf ../inc ../Utils ../hotupdate) 16 | 17 | AUX_SOURCE_DIRECTORY(. A) 18 | 19 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 20 | ADD_LIBRARY(rayX SHARED ${A} ${CUR_HEADER} ${COMMN_HEADER}) 21 | set_target_properties(rayX PROPERTIES VS_GLOBAL_VcpkgEnabled true) 22 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 23 | add_compile_options(-fcoroutines) 24 | ADD_LIBRARY(rayX ${A}) 25 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 26 | ADD_LIBRARY(rayX ${A}) 27 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 28 | 29 | target_link_libraries(rayX PRIVATE function2::function2 PRIVATE msgpack-c)# PRIVATE ZLIB::ZLIB PRIVATE nng::nngpp PRIVATE Utils PRIVATE configs PRIVATE hotupdate PRIVATE nlohmann_json::nlohmann_json PRIVATE nng::nng PRIVATE spdlog::spdlog PRIVATE OpenSSL::SSL OpenSSL::Crypto) 30 | 31 | 32 | generate_export_header(rayX EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 33 | 34 | -------------------------------------------------------------------------------- /rayX/ray/api/common_types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | #include 18 | #include 19 | 20 | #include "boost/optional.hpp" 21 | 22 | namespace ray { 23 | namespace internal { 24 | 25 | struct TaskArg { 26 | TaskArg() = default; 27 | TaskArg(TaskArg &&rhs) { 28 | buf = std::move(rhs.buf); 29 | id = rhs.id; 30 | meta_str = std::move(rhs.meta_str); 31 | } 32 | 33 | TaskArg(const TaskArg &) = delete; 34 | TaskArg &operator=(TaskArg const &) = delete; 35 | TaskArg &operator=(TaskArg &&) = delete; 36 | 37 | /// If the buf is initialized shows it is a value argument. 38 | boost::optional buf; 39 | /// If the id is initialized shows it is a reference argument. 40 | boost::optional id; 41 | 42 | std::string_view meta_str; 43 | }; 44 | 45 | using ArgsBuffer = msgpack::sbuffer; 46 | using ArgsBufferList = std::vector; 47 | 48 | using RemoteFunction = std::function; 49 | using RemoteFunctionMap_t = std::unordered_map; 50 | 51 | using RemoteMemberFunction = 52 | std::function; 53 | using RemoteMemberFunctionMap_t = std::unordered_map; 54 | 55 | } // namespace internal 56 | } // namespace ray -------------------------------------------------------------------------------- /rayX/ray/api/internal_api.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | namespace ray { 18 | 19 | void RunTaskExecutionLoop(); 20 | 21 | } // namespace ray 22 | -------------------------------------------------------------------------------- /rayX/ray/api/ray_config.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020-2021 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "boost/optional.hpp" 25 | 26 | namespace ray { 27 | 28 | enum class ActorLifetime { 29 | NON_DETACHED, 30 | DETACHED, 31 | }; 32 | 33 | class RayConfig { 34 | public: 35 | // The address of the Ray cluster to connect to. 36 | // If not provided, it will be initialized from environment variable "RAY_ADDRESS" by 37 | // default. 38 | std::string address = ""; 39 | 40 | // Whether or not to run this application in a local mode. This is used for debugging. 41 | bool local_mode = false; 42 | 43 | // An array of directories or dynamic library files that specify the search path for 44 | // user code. This parameter is not used when the application runs in local mode. 45 | // Only searching the top level under a directory. 46 | std::vector code_search_path; 47 | 48 | // The command line args to be appended as parameters of the `ray start` command. It 49 | // takes effect only if Ray head is started by a driver. Run `ray start --help` for 50 | // details. 51 | std::vector head_args = {}; 52 | 53 | // The default actor lifetime type, `DETACHED` or `NON_DETACHED`. 54 | ActorLifetime default_actor_lifetime = ActorLifetime::NON_DETACHED; 55 | 56 | // The job level runtime environments. 57 | boost::optional runtime_env; 58 | 59 | /* The following are unstable parameters and their use is discouraged. */ 60 | 61 | // Prevents external clients without the password from connecting to Redis if provided. 62 | boost::optional redis_password_; 63 | 64 | // A specific flag for internal `default_worker`. Please don't use it in user code. 65 | bool is_worker_ = false; 66 | 67 | // A namespace is a logical grouping of jobs and named actors. 68 | std::string ray_namespace = ""; 69 | }; 70 | 71 | } // namespace ray 72 | -------------------------------------------------------------------------------- /rayX/ray/api/ray_exception.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020-2021 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | #include 19 | 20 | namespace ray { 21 | namespace internal { 22 | 23 | class RayException : public std::exception { 24 | public: 25 | RayException(const std::string &msg) : msg_(msg){}; 26 | 27 | const char *what() const noexcept override { return msg_.c_str(); }; 28 | 29 | std::string msg_; 30 | }; 31 | 32 | class RayActorException : public RayException { 33 | public: 34 | RayActorException(const std::string &msg) : RayException(msg){}; 35 | }; 36 | 37 | class RayTaskException : public RayException { 38 | public: 39 | RayTaskException(const std::string &msg) : RayException(msg){}; 40 | }; 41 | 42 | class RayWorkerException : public RayException { 43 | public: 44 | RayWorkerException(const std::string &msg) : RayException(msg){}; 45 | }; 46 | 47 | class UnreconstructableException : public RayException { 48 | public: 49 | UnreconstructableException(const std::string &msg) : RayException(msg){}; 50 | }; 51 | 52 | class RayFunctionNotFound : public RayException { 53 | public: 54 | RayFunctionNotFound(const std::string &msg) : RayException(msg){}; 55 | }; 56 | 57 | class RayRuntimeEnvException : public RayException { 58 | public: 59 | RayRuntimeEnvException(const std::string &msg) : RayException(msg){}; 60 | }; 61 | } // namespace internal 62 | } // namespace ray -------------------------------------------------------------------------------- /rayX/ray/api/ray_runtime_holder.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | namespace ray { 20 | namespace internal { 21 | 22 | struct RayRuntimeHolder { 23 | static RayRuntimeHolder &Instance() { 24 | static RayRuntimeHolder instance; 25 | return instance; 26 | } 27 | 28 | void Init(std::shared_ptr runtime) { runtime_ = runtime; } 29 | 30 | std::shared_ptr Runtime() { return runtime_; } 31 | 32 | private: 33 | RayRuntimeHolder() = default; 34 | ~RayRuntimeHolder() = default; 35 | RayRuntimeHolder(RayRuntimeHolder const &) = delete; 36 | RayRuntimeHolder(RayRuntimeHolder &&) = delete; 37 | RayRuntimeHolder &operator=(RayRuntimeHolder const &) = delete; 38 | RayRuntimeHolder &operator=(RayRuntimeHolder &&) = delete; 39 | 40 | std::shared_ptr runtime_; 41 | }; 42 | 43 | inline static std::shared_ptr GetRayRuntime() { 44 | return RayRuntimeHolder::Instance().Runtime(); 45 | } 46 | 47 | } // namespace internal 48 | } // namespace ray -------------------------------------------------------------------------------- /rayX/ray/api/static_check.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | namespace ray { 23 | namespace internal { 24 | 25 | template 26 | struct FilterArgType { 27 | using type = T; 28 | }; 29 | 30 | template 31 | struct FilterArgType> { 32 | using type = T; 33 | }; 34 | 35 | template 36 | struct FilterArgType &> { 37 | using type = T; 38 | }; 39 | 40 | template 41 | struct FilterArgType &&> { 42 | using type = T; 43 | }; 44 | 45 | template 46 | struct FilterArgType &> { 47 | using type = T; 48 | }; 49 | 50 | template 51 | struct is_invocable 52 | : std::is_constructible< 53 | std::function, 54 | std::reference_wrapper::type>> {}; 55 | 56 | template 57 | inline std::enable_if_t::value> StaticCheck() { 58 | static_assert(is_invocable::type...>::value || 59 | is_invocable::value, 60 | "arguments not match"); 61 | } 62 | 63 | template 64 | inline std::enable_if_t::value> StaticCheck() { 65 | using ActorType = boost::callable_traits::class_of_t; 66 | static_assert( 67 | is_invocable::type...>::value || 68 | is_invocable::value, 69 | "arguments not match"); 70 | } 71 | 72 | } // namespace internal 73 | } // namespace ray 74 | -------------------------------------------------------------------------------- /rayX/ray/api/wait_result.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020-2021 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include 20 | 21 | namespace ray { 22 | 23 | /// \param T The type of object. 24 | template 25 | class WaitResult { 26 | public: 27 | /// The object id list of ready objects 28 | std::list> ready; 29 | /// The object id list of unready objects 30 | std::list> unready; 31 | WaitResult(){}; 32 | WaitResult(std::list> &&ready_objects, 33 | std::list> &&unready_objects) 34 | : ready(ready_objects), unready(unready_objects){}; 35 | }; 36 | 37 | } // namespace ray -------------------------------------------------------------------------------- /rayX/ray/api/xlang_function.h: -------------------------------------------------------------------------------- 1 | // Copyright 2020-2021 The Ray Authors. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | #include 19 | 20 | namespace ray { 21 | 22 | template 23 | struct PyFunction { 24 | bool IsPython() { return true; } 25 | R operator()() { return {}; } 26 | 27 | std::string module_name; 28 | std::string function_name; 29 | }; 30 | 31 | struct PyActorClass { 32 | bool IsPython() { return true; } 33 | void operator()() {} 34 | 35 | std::string module_name; 36 | std::string class_name; 37 | std::string function_name = "__init__"; 38 | }; 39 | 40 | template 41 | struct PyActorMethod { 42 | bool IsPython() { return true; } 43 | R operator()() { return {}; } 44 | 45 | std::string function_name; 46 | }; 47 | 48 | struct JavaActorClass { 49 | bool IsJava() { return true; } 50 | void operator()() {} 51 | std::string class_name; 52 | std::string module_name = ""; 53 | std::string function_name = ""; 54 | }; 55 | template 56 | struct JavaActorMethod { 57 | bool IsJava() { return true; } 58 | R operator()() { return {}; } 59 | std::string function_name; 60 | }; 61 | 62 | template 63 | struct JavaFunction { 64 | bool IsJava() { return true; } 65 | R operator()() { return {}; } 66 | std::string class_name; 67 | std::string function_name; 68 | }; 69 | 70 | namespace internal { 71 | 72 | enum class LangType { 73 | CPP, 74 | PYTHON, 75 | JAVA, 76 | }; 77 | 78 | inline constexpr size_t XLANG_HEADER_LEN = 9; 79 | inline constexpr std::string_view METADATA_STR_DUMMY = "__RAY_DUMMY__"; 80 | inline constexpr std::string_view METADATA_STR_RAW = "RAW"; 81 | inline constexpr std::string_view METADATA_STR_XLANG = "XLANG"; 82 | 83 | } // namespace internal 84 | 85 | } // namespace ray -------------------------------------------------------------------------------- /rayX/rayx.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ray/api.h" 3 | 4 | #include "rayx.h" 5 | 6 | namespace RAYX 7 | { 8 | void hello() 9 | { 10 | 11 | } 12 | 13 | RAYX_EXPORT void Init() 14 | { 15 | 16 | } 17 | 18 | RAYX_EXPORT void Shutdown() 19 | { 20 | 21 | } 22 | 23 | } 24 | 25 | 26 | 27 | namespace internal 28 | { 29 | 30 | } 31 | -------------------------------------------------------------------------------- /rayX/rayx.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "rayXExport.h" 4 | 5 | namespace RAYX 6 | { 7 | RAYX_EXPORT void hello(); 8 | RAYX_EXPORT void Init(); 9 | RAYX_EXPORT void Shutdown(); 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.20) 3 | 4 | project(tests) 5 | 6 | include_directories(../networkinterface ../Utils ../hotupdate ../rayX) 7 | 8 | add_subdirectory(envcheck) 9 | add_subdirectory(networktest) 10 | add_subdirectory(asyncStreamtest) 11 | add_subdirectory(asyncEvent) 12 | add_subdirectory(sqlppTest) 13 | add_subdirectory(rayXtest) 14 | 15 | add_test (test_run envcheck) 16 | 17 | #add_test (test_run envcheck) -------------------------------------------------------------------------------- /tests/asyncEvent/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(asyncEvent) 3 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 4 | add_definitions(-D _CRT_SECURE_NO_WARNINGS) 5 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 6 | 7 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 8 | 9 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 10 | 11 | 12 | 13 | AUX_SOURCE_DIRECTORY(. A) 14 | 15 | 16 | 17 | add_executable(asyncEvent ${A}) 18 | 19 | target_link_libraries(asyncEvent PRIVATE Catch2::Catch2 Catch2::Catch2WithMain PRIVATE hashlibrary PRIVATE database PRIVATE Utils PRIVATE configs PRIVATE networkinterface PRIVATE spdlog::spdlog PRIVATE hotupdate) 20 | 21 | 22 | set_target_properties(asyncEvent PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/config") 23 | 24 | set_target_properties(asyncEvent PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") 25 | set_target_properties(asyncEvent PROPERTIES VS_GLOBAL_VcpkgEnabled true) 26 | -------------------------------------------------------------------------------- /tests/asyncStreamtest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(asyncStreamtest) 3 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 4 | add_definitions(-D _CRT_SECURE_NO_WARNINGS) 5 | AUX_SOURCE_DIRECTORY(./win B) 6 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 7 | AUX_SOURCE_DIRECTORY(./linux B) 8 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 9 | AUX_SOURCE_DIRECTORY(./osx B) 10 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 11 | 12 | 13 | 14 | AUX_SOURCE_DIRECTORY(. A) 15 | 16 | 17 | 18 | add_executable(asyncStreamtest ${A} ${B}) 19 | 20 | target_link_libraries(asyncStreamtest PRIVATE Catch2::Catch2 Catch2::Catch2WithMain PRIVATE hashlibrary PRIVATE database PRIVATE Utils PRIVATE configs PRIVATE networkinterface PRIVATE spdlog::spdlog PRIVATE hotupdate) 21 | 22 | 23 | set_target_properties(asyncStreamtest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/config") 24 | 25 | set_target_properties(asyncStreamtest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") 26 | set_target_properties(asyncStreamtest PROPERTIES VS_GLOBAL_VcpkgEnabled true) 27 | -------------------------------------------------------------------------------- /tests/asyncStreamtest/linux/typecheck.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/tests/asyncStreamtest/linux/typecheck.cpp -------------------------------------------------------------------------------- /tests/asyncStreamtest/win/winspec.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "catch.hpp" 3 | 4 | #include 5 | #include 6 | 7 | -------------------------------------------------------------------------------- /tests/envcheck/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(envcheck) 3 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 4 | add_definitions(-D _CRT_SECURE_NO_WARNINGS) 5 | AUX_SOURCE_DIRECTORY(./win B) 6 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 7 | AUX_SOURCE_DIRECTORY(./linux B) 8 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 9 | AUX_SOURCE_DIRECTORY(./osx B) 10 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 11 | 12 | 13 | 14 | AUX_SOURCE_DIRECTORY(. A) 15 | 16 | 17 | 18 | add_executable(envcheck ${A} ${B}) 19 | 20 | target_link_libraries(envcheck PUBLIC networkinterface PRIVATE OpenSSL::SSL OpenSSL::Crypto PRIVATE database PRIVATE hotupdate PRIVATE nng::nng PUBLIC configs PUBLIC Utils PRIVATE Catch2::Catch2 Catch2::Catch2WithMain) 21 | 22 | 23 | set_target_properties(envcheck PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/config") 24 | 25 | set_target_properties(envcheck PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") 26 | set_target_properties(envcheck PROPERTIES VS_GLOBAL_VcpkgEnabled true) 27 | -------------------------------------------------------------------------------- /tests/envcheck/linux/typecheck.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/tests/envcheck/linux/typecheck.cpp -------------------------------------------------------------------------------- /tests/envcheck/win/typecheck.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "catch.hpp" 3 | 4 | #include 5 | #include 6 | 7 | TEST_CASE("type check", "32 bit") 8 | { 9 | REQUIRE(sizeof(u_long) == 4); 10 | } 11 | -------------------------------------------------------------------------------- /tests/networktest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(networktest) 3 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 4 | add_definitions(-D _CRT_SECURE_NO_WARNINGS) 5 | AUX_SOURCE_DIRECTORY(./win B) 6 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 7 | AUX_SOURCE_DIRECTORY(./linux B) 8 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 9 | AUX_SOURCE_DIRECTORY(./osx B) 10 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 11 | 12 | 13 | 14 | AUX_SOURCE_DIRECTORY(. A) 15 | 16 | 17 | 18 | add_executable(networktest ${A} ${B}) 19 | 20 | target_link_libraries(networktest PRIVATE nng::nngpp PRIVATE nng::nng PRIVATE Catch2::Catch2 Catch2::Catch2WithMain PRIVATE hashlibrary PRIVATE Utils PRIVATE configs PRIVATE networkinterface PRIVATE spdlog::spdlog PRIVATE hotupdate) 21 | 22 | 23 | set_target_properties(networktest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/config") 24 | 25 | set_target_properties(networktest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") 26 | set_target_properties(networktest PROPERTIES VS_GLOBAL_VcpkgEnabled true) 27 | -------------------------------------------------------------------------------- /tests/networktest/linux/typecheck.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/tests/networktest/linux/typecheck.cpp -------------------------------------------------------------------------------- /tests/networktest/win/winspec.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "catch.hpp" 3 | 4 | #include 5 | #include 6 | 7 | TEST_CASE("type check", "32 bit") 8 | { 9 | REQUIRE(sizeof(u_long) == 4); 10 | } 11 | -------------------------------------------------------------------------------- /tests/rayXtest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(rayXtest) 3 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 4 | add_definitions(-D _CRT_SECURE_NO_WARNINGS) 5 | AUX_SOURCE_DIRECTORY(./win B) 6 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 7 | AUX_SOURCE_DIRECTORY(./linux B) 8 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 9 | AUX_SOURCE_DIRECTORY(./osx B) 10 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 11 | 12 | 13 | 14 | AUX_SOURCE_DIRECTORY(. A) 15 | 16 | 17 | 18 | add_executable(rayXtest ${A} ${B}) 19 | 20 | target_link_libraries(rayXtest PRIVATE function2::function2 PRIVATE rayX PRIVATE Utils PRIVATE nng::nng PRIVATE hotupdate PRIVATE OpenSSL::SSL OpenSSL::Crypto PUBLIC Utils PRIVATE Catch2::Catch2 Catch2::Catch2WithMain) 21 | 22 | 23 | set_target_properties(rayXtest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/config") 24 | 25 | set_target_properties(rayXtest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") 26 | set_target_properties(rayXtest PROPERTIES VS_GLOBAL_VcpkgEnabled true) 27 | -------------------------------------------------------------------------------- /tests/rayXtest/Common.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "catch.hpp" 8 | #include "jwt-cpp/jwt.h" 9 | #include "Utils.h" 10 | #include "endian/big_endian.hpp" 11 | #include "endian/little_endian.hpp" 12 | #include "endian/network.hpp" 13 | #include "endian/stream_reader.hpp" 14 | #include "endian/stream_writer.hpp" 15 | #include "function2/function2.hpp" 16 | #include "templatefuncs.h" 17 | #include "spdlog/spdlog.h" 18 | #include "boost/callable_traits/return_type.hpp" 19 | #include "boost/callable_traits/args.hpp" 20 | #include 21 | 22 | 23 | #include "Utils.h" 24 | 25 | #include "ray/api.h" 26 | 27 | #include "rayx.h" 28 | 29 | int Plus(int a, int b) 30 | { 31 | return a + b; 32 | } 33 | RAY_REMOTE(Plus); 34 | 35 | int main(int argc, char* argv[]) 36 | { 37 | // your setup ... 38 | RAYX::Init(); 39 | int result = Catch::Session().run(argc, argv); 40 | // your clean-up... 41 | RAYX::Shutdown(); 42 | //https://github.com/gabime/spdlog/issues/1533 43 | spdlog::shutdown(); 44 | return result; 45 | } 46 | 47 | TEST_CASE("int value", "put and get") 48 | { 49 | /*auto object = ray::Put(int{100}); 50 | auto put_get_result = *(ray::Get(object));*/ 51 | } 52 | 53 | TEST_CASE("function2", "lambda") 54 | { 55 | 56 | } 57 | 58 | TEST_CASE("big endian", "endian convert") 59 | { 60 | uint64_t a = 111, b = 222; 61 | uint64_t x, y; 62 | 63 | uint8_t buffer[sizeof(uint64_t) * 2]; 64 | 65 | endian::network::put(a, buffer); 66 | endian::network::put(b, buffer + sizeof(a)); 67 | 68 | endian::network::get(x, buffer); 69 | endian::network::get(y, buffer + sizeof(x)); 70 | 71 | REQUIRE(a == x); 72 | REQUIRE(b == y); 73 | } 74 | -------------------------------------------------------------------------------- /tests/rayXtest/linux/typecheck.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/tests/rayXtest/linux/typecheck.cpp -------------------------------------------------------------------------------- /tests/rayXtest/win/typecheck.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "catch.hpp" 3 | 4 | #include 5 | #include 6 | 7 | TEST_CASE("type check", "32 bit") 8 | { 9 | REQUIRE(sizeof(u_long) == 4); 10 | } 11 | -------------------------------------------------------------------------------- /tests/sqlppTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(sqlppTest) 3 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 4 | add_definitions(-D _CRT_SECURE_NO_WARNINGS) 5 | AUX_SOURCE_DIRECTORY(./win B) 6 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 7 | AUX_SOURCE_DIRECTORY(./linux B) 8 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 9 | AUX_SOURCE_DIRECTORY(./osx B) 10 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 11 | 12 | include_directories(../../configs) 13 | 14 | AUX_SOURCE_DIRECTORY(. A) 15 | 16 | 17 | 18 | add_executable(sqlppTest ${A} ${B}) 19 | 20 | target_link_libraries(sqlppTest PRIVATE unofficial::sqlite3::sqlite3 PRIVATE OpenSSL::SSL OpenSSL::Crypto PRIVATE sqlpp11::sqlpp11 PRIVATE database PRIVATE Catch2::Catch2 Catch2::Catch2WithMain PRIVATE hashlibrary PRIVATE Utils PRIVATE configs PRIVATE networkinterface PRIVATE spdlog::spdlog PRIVATE hotupdate) 21 | 22 | 23 | set_target_properties(sqlppTest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/config") 24 | 25 | set_target_properties(sqlppTest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") 26 | set_target_properties(sqlppTest PROPERTIES VS_GLOBAL_VcpkgEnabled true) 27 | -------------------------------------------------------------------------------- /tests/sqlppTest/Common.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "spdlog/spdlog.h" 3 | #include "catch.hpp" 4 | #include "../conf/dbTest.h" 5 | #include "Configs.h" 6 | #include "DBManager.h" 7 | #include "database.h" 8 | #include "conf.h" 9 | 10 | using Catch::Matchers::Equals; 11 | 12 | using namespace std; 13 | 14 | TEST_CASE("select unconditionally", "success") 15 | { 16 | SMDB::DBInst db; 17 | dbTest::Forsumgroup tb; 18 | tb.price = 0; 19 | 20 | auto& vx = *db; 21 | bool bret = false; 22 | for (const auto& row : vx(select(tb.name).from(tb).unconditionally())) 23 | { 24 | bret = true; 25 | } 26 | REQUIRE(bret); 27 | } 28 | 29 | TEST_CASE("select sum group by", "success") 30 | { 31 | SMDB::DBInst db; 32 | dbTest::Forsumgroup tb; 33 | tb.price = 0; 34 | 35 | for (const auto& r : (*db)(select(sum(tb.price)).from(tb).group_by(tb.name).unconditionally())) 36 | { 37 | size_t v = r.sum.value(); 38 | REQUIRE(v > 0); 39 | } 40 | } 41 | 42 | TEST_CASE("select sum, count group by", "success") 43 | { 44 | SMDB::DBInst db; 45 | dbTest::Forsumgroup tb; 46 | tb.price = 0; 47 | 48 | for (const auto& r : (*db)(select(sum(tb.price), count(tb.name)).from(tb).group_by(tb.name).unconditionally())) 49 | { 50 | size_t v = r.sum.value(); 51 | REQUIRE(v > 0); 52 | size_t c = r.count.value(); 53 | REQUIRE(c > 0); 54 | } 55 | } 56 | 57 | TEST_CASE("select count", "success") 58 | { 59 | SMDB::DBInst db; 60 | dbTest::Forsumgroup tb; 61 | tb.price = 0; 62 | 63 | auto vv = (*db)(select(count(tb.name)).from(tb).unconditionally()).front().count.value(); 64 | REQUIRE(vv > 0); 65 | } 66 | 67 | int main(int argc, char* argv[]) 68 | { 69 | //setup ... 70 | auto dbconf = SMCONF::getDBConfig(); 71 | dbconf->_sqlite._file = "dbTest.db"; 72 | dbconf->_sqlite._filepath = "./conf"; 73 | spdlog::set_level(spdlog::level::level_enum::warn); 74 | SMDB::init(true); 75 | int result = Catch::Session().run(argc, argv); 76 | //clean-up... 77 | //https://github.com/gabime/spdlog/issues/1533 78 | spdlog::shutdown(); 79 | return result; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /tzdata2022a.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/tzdata2022a.tar.gz -------------------------------------------------------------------------------- /uSockets2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | PROJECT(uSockets2) 3 | 4 | #add_definitions(-DLIBUS_NO_SSL) 5 | add_definitions(-DLIBUS_USE_OPENSSL) 6 | #set(CMAKE_BUILD_TYPE "Release") 7 | include_directories(src) 8 | AUX_SOURCE_DIRECTORY(./src/ DIR_SRCS) 9 | AUX_SOURCE_DIRECTORY(./src/eventing/ DIR_SRCS2) 10 | 11 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 12 | #on windows use libuv 13 | add_definitions(-DLIBUS_USE_LIBUV) 14 | ADD_LIBRARY(uSockets2 SHARED ${DIR_SRCS} src/eventing/libuv.c src/crypto/openssl.c src/crypto/sni_tree.cpp) 15 | set_target_properties(uSockets2 PROPERTIES VS_GLOBAL_VcpkgEnabled true) 16 | target_link_libraries(uSockets2 PRIVATE $,uv_a,uv>) 17 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 18 | #on linux use epoll 19 | add_definitions(-DLIBUS_USE_EPOLL) 20 | ADD_LIBRARY(uSockets2 ${DIR_SRCS} src/eventing/epoll_kqueue.c src/crypto/openssl.c src/crypto/sni_tree.cpp) 21 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 22 | include_directories(/usr/local/include) 23 | link_directories(/usr/local/lib) 24 | link_libraries(uv) 25 | ADD_LIBRARY(uSockets2 ${DIR_SRCS} ${DIR_SRCS2} src/crypto/openssl.c src/crypto/sni_tree.cpp) 26 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /uSockets2/src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/uSockets2/src/.DS_Store -------------------------------------------------------------------------------- /uSockets2/src/internal/eventing/asio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Authored by Alex Hultman, 2018-2021. 3 | * Intellectual property of third-party. 4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef ASIO_H 19 | #define ASIO_H 20 | 21 | #include "internal/loop_data.h" 22 | 23 | #define LIBUS_SOCKET_READABLE 1 24 | #define LIBUS_SOCKET_WRITABLE 2 25 | 26 | struct us_loop_t { 27 | alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data; 28 | 29 | // a loop is an io_context 30 | void *io; 31 | 32 | // whether or not we got an io_context as hint or not 33 | int is_default; 34 | }; 35 | 36 | // it is no longer valid to cast a pointer to us_poll_t to a pointer of uv_poll_t 37 | struct us_poll_t { 38 | void *boost_block; 39 | 40 | LIBUS_SOCKET_DESCRIPTOR fd; 41 | unsigned char poll_type; 42 | int events; 43 | }; 44 | 45 | #endif // ASIO_H 46 | -------------------------------------------------------------------------------- /uSockets2/src/internal/eventing/epoll_kqueue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Authored by Alex Hultman, 2018-2019. 3 | * Intellectual property of third-party. 4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef EPOLL_KQUEUE_H 19 | #define EPOLL_KQUEUE_H 20 | 21 | #include "internal/loop_data.h" 22 | 23 | #ifdef LIBUS_USE_EPOLL 24 | #include 25 | #include 26 | #include 27 | #define LIBUS_SOCKET_READABLE EPOLLIN 28 | #define LIBUS_SOCKET_WRITABLE EPOLLOUT 29 | #else 30 | #include 31 | /* Kqueue's EVFILT_ is NOT a bitfield, you cannot OR together them. 32 | * We therefore have our own bitfield we then translate in every call */ 33 | #define LIBUS_SOCKET_READABLE 1 34 | #define LIBUS_SOCKET_WRITABLE 2 35 | #endif 36 | 37 | struct us_loop_t { 38 | alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data; 39 | 40 | /* Number of non-fallthrough polls in the loop */ 41 | int num_polls; 42 | 43 | /* Number of ready polls this iteration */ 44 | int num_ready_polls; 45 | 46 | /* Current index in list of ready polls */ 47 | int current_ready_poll; 48 | 49 | /* Loop's own file descriptor */ 50 | int fd; 51 | 52 | /* The list of ready polls */ 53 | #ifdef LIBUS_USE_EPOLL 54 | struct epoll_event ready_polls[1024]; 55 | #else 56 | struct kevent ready_polls[1024]; 57 | #endif 58 | }; 59 | 60 | struct us_poll_t { 61 | alignas(LIBUS_EXT_ALIGNMENT) struct { 62 | signed int fd : 28; // we could have this unsigned if we wanted to, -1 should never be used 63 | unsigned int poll_type : 4; 64 | } state; 65 | }; 66 | 67 | #endif // EPOLL_KQUEUE_H 68 | -------------------------------------------------------------------------------- /uSockets2/src/internal/eventing/gcd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Authored by Alex Hultman, 2018-2019. 3 | * Intellectual property of third-party. 4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef GCD_H 19 | #define GCD_H 20 | 21 | #include "internal/loop_data.h" 22 | 23 | #include 24 | #define LIBUS_SOCKET_READABLE 1 25 | #define LIBUS_SOCKET_WRITABLE 2 26 | 27 | struct us_loop_t { 28 | alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data; 29 | 30 | dispatch_queue_t gcd_queue; 31 | }; 32 | 33 | struct us_poll_t { 34 | int events; 35 | dispatch_source_t gcd_read, gcd_write; 36 | LIBUS_SOCKET_DESCRIPTOR fd; 37 | unsigned char poll_type; 38 | }; 39 | 40 | #endif // GCD_H 41 | -------------------------------------------------------------------------------- /uSockets2/src/internal/eventing/libuv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Authored by Alex Hultman, 2018-2019. 3 | * Intellectual property of third-party. 4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef LIBUV_H 19 | #define LIBUV_H 20 | 21 | #include "internal/loop_data.h" 22 | 23 | #include 24 | #define LIBUS_SOCKET_READABLE UV_READABLE 25 | #define LIBUS_SOCKET_WRITABLE UV_WRITABLE 26 | 27 | struct us_loop_t { 28 | alignas(LIBUS_EXT_ALIGNMENT) struct us_internal_loop_data_t data; 29 | 30 | uv_loop_t *uv_loop; 31 | int is_default; 32 | 33 | uv_prepare_t *uv_pre; 34 | uv_check_t *uv_check; 35 | }; 36 | 37 | // it is no longer valid to cast a pointer to us_poll_t to a pointer of uv_poll_t 38 | struct us_poll_t { 39 | /* We need to hold a pointer to this uv_poll_t since we need to be able to resize our block */ 40 | uv_poll_t *uv_p; 41 | LIBUS_SOCKET_DESCRIPTOR fd; 42 | unsigned char poll_type; 43 | }; 44 | 45 | #endif // LIBUV_H 46 | -------------------------------------------------------------------------------- /uSockets2/src/internal/loop_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Authored by Alex Hultman, 2018-2019. 3 | * Intellectual property of third-party. 4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef LOOP_DATA_H 19 | #define LOOP_DATA_H 20 | 21 | struct us_internal_loop_data_t { 22 | struct us_timer_t *sweep_timer; 23 | struct us_internal_async *wakeup_async; 24 | int last_write_failed; 25 | struct us_socket_context_t *head; 26 | struct us_socket_context_t *iterator; 27 | char *recv_buf; 28 | void *ssl_data; 29 | void (*pre_cb)(struct us_loop_t *); 30 | void (*post_cb)(struct us_loop_t *); 31 | struct us_socket_t *closed_head; 32 | struct us_socket_t *low_prio_head; 33 | int low_prio_budget; 34 | /* We do not care if this flips or not, it doesn't matter */ 35 | long long iteration_nr; 36 | }; 37 | 38 | #endif // LOOP_DATA_H 39 | -------------------------------------------------------------------------------- /uSockets2/uSockets-0.8.2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/uSockets2/uSockets-0.8.2.zip -------------------------------------------------------------------------------- /uSockets2/uSockets-0.8.3.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxingpping/SM/b9cb6b12ab26a4601d24aa406f349c1eda36b38b/uSockets2/uSockets-0.8.3.zip -------------------------------------------------------------------------------- /whereami/.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | 3 | # sources 4 | *.h text diff=cpp 5 | *.c text diff=cpp 6 | *.cpp text diff=cpp 7 | *.rb text diff=ruby 8 | *.html text diff=html 9 | *.m text diff=objc 10 | 11 | # shell scripts 12 | *.sh eol=lf 13 | 14 | # GNU Makefile 15 | Makefile text eol=lf 16 | 17 | # Autotools 18 | *.am text eol=lf 19 | 20 | # Android 21 | *.mk text eol=lf 22 | 23 | # Xcode files 24 | *.pbxproj text eol=lf merge=union 25 | 26 | # Visual Studio files 27 | *.sln text eol=crlf merge=union 28 | *.vcxproj text eol=crlf merge=union 29 | *.vcxproj.filters text eol=crlf merge=union 30 | *.props text eol=crlf 31 | -------------------------------------------------------------------------------- /whereami/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | *.swp 4 | 5 | /bin 6 | 7 | /build_win/ 8 | /_win-vs14/ 9 | /.vscode/ 10 | -------------------------------------------------------------------------------- /whereami/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.27) 2 | PROJECT(whereami) 3 | 4 | set(CMAKE_CXX_STANDARD 20) 5 | 6 | add_definitions(-DBUILD_SHARED_LIBS -Dwhereami_EXPORTS) 7 | 8 | include_directories() 9 | 10 | FILE(GLOB_RECURSE CUR_HEADER "./*.h") 11 | source_group("IncCur" FILES ${CUR_HEADER}) 12 | 13 | FILE(GLOB_RECURSE COMMN_HEADER "../inc/*.h") 14 | source_group("IncComm" FILES ${COMMN_HEADER}) 15 | 16 | #FILE(GLOB_RECURSE MY_INTERFACEHEADERS "../../inc/networks/*.h") 17 | #source_group("Headers" FILES ${MY_INTERFACEHEADERS}) 18 | 19 | 20 | 21 | include_directories(../conf ./src ../inc ../Utils ../hotupdate ../networkinterface ../configs) 22 | 23 | AUX_SOURCE_DIRECTORY(./src A) 24 | 25 | include(GenerateExportHeader) 26 | 27 | IF(CMAKE_SYSTEM_NAME MATCHES "Windows") 28 | ADD_LIBRARY(whereami SHARED ${A} ${CUR_HEADER} ${COMMN_HEADER}) 29 | set_target_properties(whereami PROPERTIES VS_GLOBAL_VcpkgEnabled true) 30 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux") 31 | add_definitions(-w) 32 | add_compile_options(-fcoroutines) 33 | link_libraries(dl) 34 | ADD_LIBRARY(whereami ${A} ) 35 | ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") 36 | ADD_LIBRARY(whereami ${A} ) 37 | ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows") 38 | 39 | 40 | 41 | generate_export_header(whereami EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/../inc/os/${CMAKE_SYSTEM_NAME}/export_flags/${PROJECT_NAME}Export.h) 42 | 43 | 44 | 45 | #target_link_libraries(whereami PRIVATE nng::nngpp PRIVATE ZLIB::ZLIB PRIVATE OpenSSL::SSL OpenSSL::Crypto PRIVATE unofficial::sqlite3::sqlite3 PRIVATE sqlpp11::sqlpp11 PRIVATE hashlibrary PRIVATE Utils PRIVATE configs PRIVATE networkinterface PRIVATE spdlog::spdlog PRIVATE hotupdate PRIVATE date::date date::date-tz) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /whereami/LICENSE.MIT: -------------------------------------------------------------------------------- 1 | Copyright Gregory Pakosz 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /whereami/LICENSE.WTFPLv2: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 3 | Version 2, December 2004 4 | 5 | Copyright (C) 2004 Sam Hocevar 6 | 7 | Everyone is permitted to copy and distribute verbatim or modified 8 | copies of this license document, and changing it is allowed as long 9 | as the name is changed. 10 | 11 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 12 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 13 | 14 | 0. You just DO WHAT THE FUCK YOU WANT TO. 15 | 1. Bla bla bla 16 | 2. Montesqieu et camembert, vive la France, zut alors! 17 | 18 | -------------------------------------------------------------------------------- 19 | 20 | WTFPLv2 is very permissive, see http://www.wtfpl.net/faq/ 21 | 22 | However, if this WTFPLV2 is REALLY a blocker and is the reason you can't use 23 | this project, contact me and I'll dual license it. 24 | 25 | -------------------------------------------------------------------------------- 26 | -------------------------------------------------------------------------------- /whereami/_ios-xcode/.gitignore: -------------------------------------------------------------------------------- 1 | xcuserdata/ 2 | xcshareddata/ 3 | -------------------------------------------------------------------------------- /whereami/_ios-xcode/WhereAmI-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | net.pempek.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UIRequiresFullScreen 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /whereami/_ios-xcode/WhereAmI.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /whereami/_mac-xcode/.gitignore: -------------------------------------------------------------------------------- 1 | xcuserdata/ 2 | xcshareddata/ 3 | -------------------------------------------------------------------------------- /whereami/_mac-xcode/WhereAmI.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /whereami/example/library.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #if defined(__GNUC__) && !defined(_WIN32) 7 | __attribute__((constructor)) 8 | #endif 9 | static void load(void) 10 | { 11 | char* path = NULL; 12 | int length, dirname_length; 13 | 14 | printf("library loaded\n"); 15 | 16 | length = wai_getExecutablePath(NULL, 0, &dirname_length); 17 | if (length > 0) 18 | { 19 | path = (char*)malloc(length + 1); 20 | wai_getExecutablePath(path, length, &dirname_length); 21 | path[length] = '\0'; 22 | 23 | printf("executable path: %s\n", path); 24 | path[dirname_length] = '\0'; 25 | printf(" dirname: %s\n", path); 26 | printf(" basename: %s\n", path + dirname_length + 1); 27 | free(path); 28 | } 29 | 30 | length = wai_getModulePath(NULL, 0, &dirname_length); 31 | if (length > 0) 32 | { 33 | path = (char*)malloc(length + 1); 34 | wai_getModulePath(path, length, &dirname_length); 35 | path[length] = '\0'; 36 | 37 | printf("module path: %s\n", path); 38 | path[dirname_length] = '\0'; 39 | printf(" dirname: %s\n", path); 40 | printf(" basename: %s\n", path + dirname_length + 1); 41 | free(path); 42 | } 43 | } 44 | 45 | #if defined(__GNUC__) && !defined(_WIN32) 46 | __attribute__((destructor)) 47 | #endif 48 | static void unload(void) 49 | { 50 | printf("library unloaded\n"); 51 | } 52 | 53 | #if defined(_WIN32) 54 | 55 | #define WIN32_LEAN_AND_MEAN 56 | #if defined(_MSC_VER) 57 | #pragma warning(push, 3) 58 | #endif 59 | #include 60 | 61 | BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 62 | { 63 | switch (ul_reason_for_call) 64 | { 65 | case DLL_PROCESS_ATTACH: 66 | load(); 67 | break; 68 | case DLL_THREAD_ATTACH: 69 | break; 70 | case DLL_THREAD_DETACH: 71 | break; 72 | case DLL_PROCESS_DETACH: 73 | unload(); 74 | break; 75 | } 76 | return TRUE; 77 | } 78 | 79 | #if defined(_MSC_VER) 80 | #pragma warning(pop) 81 | #endif 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /whereami/src/whereami.h: -------------------------------------------------------------------------------- 1 | // (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses 2 | // without any warranty. 3 | // by Gregory Pakosz (@gpakosz) 4 | // https://github.com/gpakosz/whereami 5 | 6 | #ifndef WHEREAMI_H 7 | #define WHEREAMI_H 8 | 9 | #include "whereamiExport.h" 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #ifndef WAI_FUNCSPEC 16 | #define WAI_FUNCSPEC 17 | #endif 18 | #ifndef WAI_PREFIX 19 | #define WAI_PREFIX(function) wai_##function 20 | #endif 21 | 22 | /** 23 | * Returns the path to the current executable. 24 | * 25 | * Usage: 26 | * - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to 27 | * retrieve the length of the path 28 | * - allocate the destination buffer with `path = (char*)malloc(length + 1);` 29 | * - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the 30 | * path 31 | * - add a terminal NUL character with `path[length] = '\0';` 32 | * 33 | * @param out destination buffer, optional 34 | * @param capacity destination buffer capacity 35 | * @param dirname_length optional recipient for the length of the dirname part 36 | * of the path. 37 | * 38 | * @return the length of the executable path on success (without a terminal NUL 39 | * character), otherwise `-1` 40 | */ 41 | WHEREAMI_EXPORT 42 | int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length); 43 | 44 | /** 45 | * Returns the path to the current module 46 | * 47 | * Usage: 48 | * - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve 49 | * the length of the path 50 | * - allocate the destination buffer with `path = (char*)malloc(length + 1);` 51 | * - call `wai_getModulePath(path, length, NULL)` again to retrieve the path 52 | * - add a terminal NUL character with `path[length] = '\0';` 53 | * 54 | * @param out destination buffer, optional 55 | * @param capacity destination buffer capacity 56 | * @param dirname_length optional recipient for the length of the dirname part 57 | * of the path. 58 | * 59 | * @return the length of the module path on success (without a terminal NUL 60 | * character), otherwise `-1` 61 | */ 62 | WHEREAMI_EXPORT 63 | int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif // #ifndef WHEREAMI_H 70 | --------------------------------------------------------------------------------