[C++ Advanced Tutorial] Developing a server framework from scratch (sylar)
Supports streaming log writing style and formatting log writing style, supports log format customization, log level, multi-log separation and other functions, streaming log usage: SYLAR_LOG_INFO(g_logger) << "this is a log"; Format log usage: SYLAR_LOG_FMT_INFO(g_logger, "%s", "this is a log"); Supports free configuration of time, thread id, thread name, log level, log name, file name, line number, etc.
Adopt the idea of convention due to configuration. Definition is available. No need to parse separately. Supports change notification function. Use YAML files as configuration content. Supports data types in level formats, supports STL containers (vector, list, set, map, etc.), supports customized types (requires serialization and deserialization methods) as follows:
static sylar::ConfigVar< int >::ptr g_tcp_connect_timeout =
sylar::Config::Lookup ( " tcp.connect.timeout " , 5000 , " tcp connect timeout " );A tcp connection timeout parameter is defined. You can directly use g_tcp_connect_timeout->getValue() to get the value of the parameter. When the configuration is modified and reloaded, the value will automatically update the above configuration format as follows:
tcp:
connect:
timeout: 10000The thread module encapsulates some commonly used functions in pthread, such as Thread, Semaphore, Mutex, RWMutex, Spinlock, etc., which can facilitate the development of threads for daily use. Why is thread in c++11 not applicable to threads? This framework is developed using C++11, but does not use thread because thread is actually implemented based on pthread. Moreover, C++11 does not provide read and write mutex, RWMutex, Spinlock, etc., and in high concurrency scenarios, these objects are often used. So I chose to encapsulate pthread by myself
Coroutine: User-mode thread, equivalent to threads in threads, is lighter. The socket hook is configured in the subsequent configuration, which can encapsulate complex asynchronous calls into synchronous operations. Reduce the complexity of writing business logic. Currently, the coroutine is implemented based on ucontext_t, and will be implemented in the subsequent way by using fcontext_t in boost.context.
The coroutine scheduler manages the scheduling of coroutines and is implemented internally as a thread pool, which supports coroutines to switch between multiple threads, and can also specify that coroutines be executed in fixed threads. It is an NM coroutine scheduling model, with N threads and M coroutines. Reuse each thread.
Inherit and coroutine scheduler, encapsulate epoll (Linux), and supports timer function (using epoll to implement timer, milliseconds level), and supports the addition, deletion and cancellation of Socket read and write time. Supports one-time timer, cycle timer, condition timer and other functions
The underlying hook system and socket-related APIs, socket io-related APIs, and sleep series APIs. The hook's turn-on control is thread granular. Free choice. Through the hook module, some APIs without asynchronous functions can be used to show asynchronous performance. For example (mysql)
Encapsulates the Socket class, provides all socket API functions, unify the address class, and unify IPv4, IPv6, and Unix addresses. It also provides domain name and IP resolution functions.
ByteArray binary serialization module provides common operations on binary data. Read and write basic types int8_t, int16_t, int32_t, int64_t, etc., support Varint, std::string to read and write support, support byte conversion, support serialization to files, and deserialization from files.
Based on the Socket class, it encapsulates a general TcpServer server class, provides a simple API, is easy to use, and can quickly bind one or more addresses, start services, listen to ports, accept connections, handle socket connections and other functions. Server implementation of specific business functions can be implemented quickly by inheriting this class.
Encapsulated streaming unified interface. Encapsulate files and sockets into unified interfaces. When using it, use a unified style. Based on a unified style, it can provide more flexible expansion. SocketStream is currently implemented
Ragel (finite state machine, performance comparable to assembly), implements simple protocol implementation of HTTP/1.1 and parsing of uri. Based on SocketStream, HttpConnection (HTTP client) and HttpSession (HTTP server link) are implemented. HttpServer is implemented based on TcpServer. Provides complete HTTP client API request function and HTTP basic API server function
Following Java's servlet, a set of Servlet interfaces is implemented, ServletDispatch and FunctionServlet. NotFoundServlet. Supports URI's precise matching, fuzzy matching and other functions. Together with the HTTP module, provide HTTP server functions
Contact information: QQ: 564628276 Email: [email protected] WeChat: sylar-yin QQ group: 8151915 (sylar technology group) Personal homepage: www.sylar.top github: https://github.com/sylar-yin/sylar