## C++ network development framework based on coroutines.

directory

# # # #

  • Cross-platform: support both linux32/64 and OSX64 platforms, support C++11 and above;
  • Easy to develop: synchronous writing of code, support Mina filter chain, simple yet powerful;
  • High performance: can process mass connection at the same time, frame lock free design, excellent performance;
  • Multi-features: code synchronous call mode, support network overload protection, support idle connection automatic cleaning and other enterprise-class features;

#### Coroutine model In the whole CxxConet framework, there are four kinds of working coroutines:

  • Socket access coroutines
  • Global Statistics Coroutine
  • Socket clean handler
  • Connection Handler for I/O processing

Socket access coroutine: accept new client connections, then create I/O processing coroutine and send client connections to the coroutine to process transactions;

Global index statistical coroutine: periodically summarize the statistical indicators of each coroutine, and achieve global summary data without locking;

Socket clearing coroutine: Periodically clear Idle connections to prevent resource exhaustion.

I/O processing coroutine: Each Client connection has an independent processing coroutine, including read/write and upper-level transaction processing.

Socket access coroutines, global metric statistics coroutines, and Socket clearing coroutines share the same thread, while I/O processing coroutines reside in separate threads.

#### Communication process

IoAcceptor: This interface is responsible for establishing sockets on a coroutine.

IoFilter: This interface defines a set of interceptors, which can include log output, blacklist filtering, data encoding (write direction) and decoding (read direction), etc.

ConnectionHandler: This interface is responsible for writing the business logic, where data is received and sent.

# # # # sample c + + :

#include "ENaf.hh" static sp<ELogger> logger = ELoggerManager::getLogger("testnaf"); static void onListening(NSocketAcceptor* acceptor) { logger->trace("onListening..." ); while (! acceptor->isDisposed()) { sleep(10); NIoServiceStatistics* ss = acceptor->getStatistics(); logger->trace_("ReadBytes=%ld", ss->getReadBytes()); logger->trace_("WrittenBytes=%ld", ss->getWrittenBytes()); } logger->trace("Out of Listening."); } static void onConnection(NSocketSession* session) { logger->trace("onConnection..." ); sp<NIoBuffer> request; while(! session->getService()->isDisposed()) { try { request = dynamic_pointer_cast<NIoBuffer>(session->read()); } catch (ESocketTimeoutException& e) { logger->trace("session read timeout."); continue; } catch (EIOException& e) { logger->trace("session read error."); break; } if (request == null) { logger->trace("session client closed."); break; } // echo. session->write(request); } logger->trace("Out of Connection."); } int main(int argc, const char **argv) { // CxxJDK init. ESystem::init(argc, argv); // CxxLog4j init. ELoggerManager::init("log4e.conf"); NSocketAcceptor sa; NBlacklistFilter blf; blf.block("localhost"); sa.getFilterChainBuilder()->addFirst("black", &blf); sa.setListeningHandler(onListening); sa.setConnectionHandler(onConnection); sa.setMaxConnections(1000000); sa.setSoTimeout(3000); sa.setSessionIdleTime(NIdleStatus::WRITER_IDLE, 30); Sa. Bind (" 0.0.0.0 ", 8888); sa.listen(); ESystem::exit(0); return 0; }Copy the code

More examples: testnaf.cpp

# # # # performance

Test procedure:

See sample c++ code: benchmark.cpp;

Software environment:

Cxxjava 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 # cxxJava 2.6.32-358.el6.x86_64 # 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/LinuxCopy the code

Hardware environment:

Model: R6240-12 Processor: Intel Xeon E5606 Processor Speed: 2.13GHz Number of cpus: 4 Total cores: 8Copy the code

Test results:

# ab-c 20-n 50000 http://localhost:8888/ This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (be patient) Completed 5000 requests Completed 10000 requests Completed 15000 requests Completed 20000 requests Completed 25000 requests Completed 30000 requests Completed 35000 requests Completed 40000 requests Completed 45000 requests Completed 50000 requests Finished 50000 requests Server Software: Server Hostname: localhost Server Port: 8888 Document Path: / Document Length: 3 bytes Concurrency Level: 20 Time taken for tests: 4.077 seconds Complete requests: 50000 Failed requests: 0 Write Errors: 0 Total extension: 2050328 BYTES HTML transferred: 150024 Bytes Requests per second: 12264.64 [#/ SEC] (mean) Time per request: 1.631 [MS] (mean) Time per request: 0.082 [MS] (mean, across all concurrent requests) Transfer rate: 491.14 [Kbytes/ SEC] Received Connection Times (ms) min mean[+/-sd] Median Max Connect: 0 1 0.1 14 Processing: 0 1 0.3 1 16 Waiting: 0 1 0.3 1 16 Total: 0 2 0.3 2 17 Percentage of the requests served within a certain time (ms) 50% 2 66% 2 75% 2 80% 2 90% 2 95% 2 98% 2 99% 2 100% 17 (longest request)Copy the code

# # # #

  1. CxxJDK
  2. CxxFiber
  3. CxxLog4j

####Support Email: [email protected]