Update:
2010/08/21
1. Support for memorypool (for details, click here)
2. Simulate the io_service service in ASIO, publish the dispatcher interface, and post any callback.
3. Enhanced network server demo
2010/09/07
1. Save asyncresult to reduce memory application Replication
2. Provides the timer component and uses waitabletimer to distribute data through the iocp thread pool of the dispatcher.
3. modify some interfaces and internal optimization
At the same time, put the framework on Google Code, and friends who need it can download it on their own.
SVN: http://iocpframework.googlecode.com/svn/trunk/
Package: http://code.google.com/p/iocpframework/downloads/list
This framework is based on Windows overlapped Io to complete the port mode. I/O threads and work threads are completely separated. It is also scalable, reusable, and easy to maintain.
Currently, the provided Source Code provides asynchronous socket and file operations, which can be easily extended to support other operations.
Compilation environment requirements:
Vs2008 + SP1 or above (supports the C ++ tr1 component ).
The general Hierarchy Diagram is as follows:
The class diagram is as follows:
Here, the source code and simple example are provided (refer to the provided demo program)
#ifndef __SERVICE_HPP#define __SERVICE_HPP#include <array>#include "../../include/network/Socket.hpp"using namespace async::network;class Session : public std::tr1::enable_shared_from_this<Session>{private: SocketPtr socket_; SocketBufferPtr data_;public: Session(const SocketPtr &sock) : socket_(sock) , data_(new SocketBuffer) { } ~Session() { Stop(); } SocketPtr& GetSocket() { return socket_; } void Start() { try { socket_->BeginRecv(data_, 0, data_->allocSize(), std::tr1::bind(&Session::_HandleRead, shared_from_this(), std::tr1::placeholders::_1)); } catch(std::exception &e) { std::cerr << e.what() << std::endl; } } void Stop() { socket_->Close(); }private: void _HandleRead(const AsyncResultPtr &asyncResult) { try { size_t bytes = socket_->EndRecv(asyncResult); if( bytes == 0 ) { socket_->BeginDisconnect(true, std::tr1::bind(&Session::_DisConnect, shared_from_this(), std::tr1::placeholders::_1)); return; } data_->resize(bytes); socket_->BeginSend(data_, 0, data_->size(), std::tr1::bind(&Session::_HandleWrite, shared_from_this(), std::tr1::placeholders::_1)); } catch(const std::exception &e) { std::cerr << e.what() << std::endl; } } void _HandleWrite(const AsyncResultPtr &asyncResult) { Start(); } void _DisConnect(const AsyncResultPtr &asyncResult) { Stop(); }};typedef std::tr1::shared_ptr<Session> SessionPtr;class Server{private: IODispatcher &io_; SocketPtr acceptor_;public: Server(IODispatcher &io, short port) : io_(io) , acceptor_(new Socket(io_)) { acceptor_->Bind(port); acceptor_->Listen(); } ~Server() { _StopServer(); }public: void Start() { _StartAccept(); } void Stop() { _StopServer(); }private: void _StartAccept() { try { acceptor_->BeginAccept(std::tr1::bind(&Server::_OnAccept, this, std::tr1::placeholders::_1)); } catch(const std::exception &e) { std::cerr << e.what() << std::endl; } } void _StopServer() { acceptor_->Close(); }private: void _OnAccept(const AsyncResultPtr &asyncResult) { try { if( *acceptor_ == INVALID_SOCKET ) return; SocketPtr acceptSocket = acceptor_->EndAccept(asyncResult); SessionPtr session_(new Session(acceptSocket)); session_->Start(); _StartAccept(); } catch(const std::exception &e) { std::cerr << e.what() << std::endl; } }};#endif
int main(int argc, char* argv[]){ _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); IODispatcher ioService(GetFitThreadNum()); try { Server server(ioService, 5050); server.Start(); system("pause"); } catch(std::exception &e) { std::cerr << e.what() << std::endl; } system("pause"); return 0;}
Of course, there are still many areas for improvement. I hope you will give me more advice.
Here is the download link. (This version of code is not recommended)