Skip to content
Browse files

update

Signed-off-by: yayanyang <yayanyang@gmail.com>
  • Loading branch information...
1 parent 614038e commit d2c4802bb99c05103919e4f4e0ab29984ea5b7d5 yayanyang committed
View
BIN io/.DS_Store
Binary file not shown.
View
480 io/abi.cpp
@@ -1,480 +0,0 @@
-#include <lemon/io/abi.h>
-#ifdef LEMON_IO_IOCP
-#include <lemon/io/io_service_iocp.hpp>
-#elif defined(LEMON_IO_EPOLL)
-#include <lemon/io/io_service_epoll.hpp>
-#endif //
-
-using namespace lemon;
-
-using namespace lemon::io;
-
-using namespace lemon::io::core;
-
-LEMON_IO_API
- LemonIOService
- LemonCreateIOService(
- __lemon_inout LemonErrorInfo * errorCode)
-{
- try
- {
- return reinterpret_cast<LemonIOService>(new io_service);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
-
- return LEMON_HANDLE_NULL_VALUE;
- }
-}
-
-LEMON_IO_API
- void
- LemonNewIOServiceWorkThreads(
- __lemon_in LemonIOService service,
- __lemon_in size_t newThreads,
- __lemon_inout LemonErrorInfo * errorCode)
-{
- try
- {
- reinterpret_cast<io_service*>(service)->start(newThreads);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-LEMON_IO_API
- void
- LemonIOServiceStopAll(
- __lemon_in LemonIOService service,
- __lemon_inout LemonErrorInfo * errorCode)
-{
- try
- {
- reinterpret_cast<io_service*>(service)->stop();
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-LEMON_IO_API
- void LemonCloseIOService(
- __lemon_in LemonIOService service)
-{
- delete reinterpret_cast<io_service*>(service);
-}
-
-
-LEMON_IO_API
- void LemonIOServicePostJob(
- __lemon_in LemonIOService service,
- __lemon_in LemonIOCallback callback,
- __lemon_in void * userdata,
- __lemon_inout LemonErrorInfo * errorCode)
-{
- try
- {
- reinterpret_cast<io_service*>(service)->post_one(callback,userdata,errorCode);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-LEMON_IO_API
- void LemonIOServiceJoin(
- __lemon_in LemonIOService service,
- __lemon_inout LemonErrorInfo * errorCode)
-{
- try
- {
- reinterpret_cast<io_service*>(service)->join();
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-
-
-}
-
-LEMON_IO_API
- void LemonIOServiceReset(
- __lemon_in LemonIOService service,
- __lemon_inout LemonErrorInfo * errorCode)
-{
- try
- {
- reinterpret_cast<io_service*>(service)->reset();
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-
-LEMON_IO_API
- LemonIO
- LemonSock(
- __lemon_in int af,
- __lemon_in int type,
- __lemon_in int protocol,
- __lemon_in LemonIOService service,
- __lemon_inout LemonErrorInfo *errorCode)
-{
-
- try
- {
- return reinterpret_cast<LemonIO>(reinterpret_cast<io_service*>(service)->create_socket(af,type,protocol));
- }
- catch(const error_info & e)
- {
- *errorCode = e;
-
- return LEMON_HANDLE_NULL_VALUE;
- }
-
-}
-
-LEMON_IO_API
- void
- LemonCloseIO(
- __lemon_free LemonIO io)
-{
- reinterpret_cast<io_object*>(io)->release();
-}
-
-LEMON_IO_API
- void LemonBind(
- __lemon_in LemonIO sock,
- __lemon_in const struct sockaddr * name,
- __lemon_in socklen_t nameLength,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(sock)->bind(name,nameLength);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-
-LEMON_IO_API
- void LemonShutdown(
- __lemon_in __lemon_in LemonIO sock,
- __lemon_in __lemon_in int how,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(sock)->shutdown(how);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-LEMON_IO_API
- void LemonGetSockName(
- __lemon_in LemonIO sock,
- __lemon_inout struct sockaddr * name,
- __lemon_inout socklen_t* bufferSize,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(sock)->sockname(name,bufferSize);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-
-LEMON_IO_API
- size_t
- LemonSend(
- __lemon_in LemonIO socket,
- __lemon_in const lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- return reinterpret_cast<core::socket*>(socket)->send(buffer,bufferSize,flags);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
-
- return size_t(-1);
- }
-}
-
-
-LEMON_IO_API
- void
- LemonAsyncSend(
- __lemon_in LemonIO socket,
- __lemon_in const lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_in LemonIOCallback callback,
- __lemon_in void *userData,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->async_send(buffer,bufferSize,flags,callback ,userData,errorCode);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-
-LEMON_IO_API
- size_t
- LemonRecv(
- __lemon_in LemonIO socket,
- __lemon_in lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- return reinterpret_cast<core::socket*>(socket)->receive(buffer,bufferSize,flags);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
-
- return size_t(-1);
- }
-}
-
-
-LEMON_IO_API
- void
- LemonAsyncRecv(
- __lemon_in LemonIO socket,
- __lemon_in lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_in LemonIOCallback callback,
- __lemon_in void *userData,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->async_receive(buffer,bufferSize,flags,callback ,userData,errorCode);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-
-LEMON_IO_API
- void
- LemonConnect(
- __lemon_in LemonIO socket,
- __lemon_in const struct sockaddr * addr,
- __lemon_in socklen_t addrlen,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->connect(addr,addrlen);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-LEMON_IO_API
- void
- LemonAsyncConnect(
- __lemon_in LemonIO socket,
- __lemon_in const struct sockaddr * addr,
- __lemon_in socklen_t addrlen,
- __lemon_in LemonIOCallback callback,
- __lemon_in void * userData,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->async_connect(addr,addrlen,callback ,userData,errorCode);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-LEMON_IO_API
- void LemonListen(
- __lemon_in LemonIO socket,
- __lemon_in int backlog,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->listen(backlog);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-
-LEMON_IO_API
- LemonIO LemonAccept(
- __lemon_in LemonIO socket,
- __lemon_inout struct sockaddr * addr,
- __lemon_inout socklen_t * addrlen,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- return reinterpret_cast<LemonIO>(reinterpret_cast<core::socket*>(socket)->accept(addr,addrlen));
- }
- catch(const error_info & e)
- {
- *errorCode = e;
-
- return LEMON_HANDLE_NULL_VALUE;
- }
-}
-
-LEMON_IO_API
- void
- LemonAsyncAccept(
- __lemon_in LemonIO socket,
- __lemon_inout struct sockaddr * addr,
- __lemon_inout socklen_t * addrlen,
- __lemon_in LemonAcceptCallback callback,
- __lemon_in void * userData,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->async_accept(addr,addrlen,callback,userData,errorCode);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-
-LEMON_IO_API
- size_t
- LemonSendTo(
- __lemon_in LemonIO socket,
- __lemon_in const lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_in const struct sockaddr * address,
- __lemon_in socklen_t addressSize,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- return reinterpret_cast<core::socket*>(socket)->sendto(buffer,bufferSize,flags,address,addressSize);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
-
- return size_t(-1);
- }
-}
-
-
-LEMON_IO_API
- void
- LemonAsyncSendTo(
- __lemon_in LemonIO socket,
- __lemon_in const lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_in const struct sockaddr * address,
- __lemon_in socklen_t addressSize,
- __lemon_in LemonIOCallback callback,
- __lemon_in void *userData,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->async_sendto(buffer,bufferSize,flags,address,addressSize,callback ,userData,errorCode);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
-
-LEMON_IO_API
- size_t
- LemonReceiveFrom(
- __lemon_in LemonIO socket,
- __lemon_in lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_in struct sockaddr * address,
- __lemon_in socklen_t *addressSize,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- return reinterpret_cast<core::socket*>(socket)->recvfrom(buffer,bufferSize,flags,address,addressSize);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
-
- return size_t(-1);
- }
-}
-
-
-LEMON_IO_API
- void
- LemonAsyncReceiveFrom(
- __lemon_in LemonIO socket,
- __lemon_in lemon_byte_t * buffer __lemon_buffer(bufferSize),
- __lemon_in size_t bufferSize,
- __lemon_in int flags,
- __lemon_in struct sockaddr * address,
- __lemon_in socklen_t *addressSize,
- __lemon_in LemonIOCallback callback,
- __lemon_in void *userData,
- __lemon_inout LemonErrorInfo *errorCode)
-{
- try
- {
- reinterpret_cast<core::socket*>(socket)->async_recvfrom(buffer,bufferSize,flags,address,addressSize,callback ,userData,errorCode);
- }
- catch(const error_info & e)
- {
- *errorCode = e;
- }
-}
View
4 io/abi.h
@@ -104,9 +104,9 @@ LEMON_IO_API
//////////////////////////////////////////////////////////////////////////
#ifdef WIN32
-typedef SOCKET LemonNativeSock;
+typedef SOCKET __lemon_os_socket;
#else
-typedef int LemonNativeSock;
+typedef int __lemon_os_socket;
#endif //WIN32
#ifndef SD_BOTH
View
93 io/allocator.hpp
@@ -0,0 +1,93 @@
+/**
+*
+* @file allocator
+* @brief Copyright (C) 2012 yayanyang All Rights Reserved
+* @author yayanyang
+* @version 1.0.0.0
+* @date 2012/09/26
+*/
+#ifndef LEMON_IO_ALLOCATOR_HPP
+#define LEMON_IO_ALLOCATOR_HPP
+
+#include <lemon/io/abi.h>
+#include <lemonxx/sys/sys.hpp>
+#include <lemonxx/mpl/list.hpp>
+#include <lemonxx/mpl/list/end.hpp>
+#include <lemonxx/mpl/list/begin.hpp>
+#include <lemonxx/mpl/list/iterator.hpp>
+#include <lemonxx/mpl/generate.hpp>
+#include <lemonxx/memory/fixobj.hpp>
+#include <lemonxx/utility/utility.hpp>
+
+namespace lemon{namespace io{namespace impl{
+
+ //////////////////////////////////////////////////////////////////////////
+ //allocator generate class
+
+ template<typename B,typename E> struct basic_allocator_helper;
+
+ template<typename B,typename E>
+ struct basic_allocator_helper : public basic_allocator_helper<typename mpl::next<B>::type,E>
+ {
+ public:
+
+ typedef typename mpl::deref<B> current_type;
+
+ typedef memory::fixed::allocator<sizeof(current_type)> allocator_type;
+
+ void* alloc()
+ {
+ mutex_t::scope_lock lock(_mutex);
+
+ return _allocator.alloc();
+ }
+
+ void free(void * block)
+ {
+ mutex_t::scope_lock lock(_mutex);
+
+ _allocator.free(block);
+ }
+
+ private:
+
+ allocator_type _allocator;
+
+ mutex_t _mutex;
+ };
+
+
+ template<typename I> struct basic_allocator_helper<I,I>
+ {
+
+ };
+
+ template<typename Seq,typename T> struct basic_allocator_cast
+ {
+ typedef typename mpl::begin<Seq>::type B;
+
+ typedef typename mpl::end<Seq>::type E;
+
+ typedef typename mpl::find<B,E,T>::result_type I;
+
+ typedef basic_allocator_helper<I,E> type;
+ };
+
+ template<typename Seq> struct basic_allocator
+ : public basic_allocator_helper<typename mpl::begin<Seq>::type, typename mpl::end<Seq>::type>
+ {
+ template<typename T> void * alloc()
+ {
+ return ((typename basic_allocator_cast<Seq,T>::type &)*this).alloc();
+ }
+
+ template<typename T> void free(void * block)
+ {
+ ((typename basic_allocator_cast<Seq,T>::type &)*this).free(block);
+ }
+ };
+
+}}}
+
+#endif //LEMON_IO_ALLOCATOR_HPP
+
View
29 io/basic_io_service.hpp
@@ -0,0 +1,29 @@
+/**
+*
+* @file basic_io_service
+* @brief Copyright (C) 2012 yayanyang All Rights Reserved
+* @author yayanyang
+* @version 1.0.0.0
+* @date 2012/09/27
+*/
+#ifndef LEMON_IO_BASIC_IO_SERVICE_HPP
+#define LEMON_IO_BASIC_IO_SERVICE_HPP
+#include <lemon/io/abi.h>
+#include <lemonxx/sys/sys.hpp>
+#include <lemon/io/basic_socket.hpp>
+#include <lemonxx/utility/utility.hpp>
+
+namespace lemon{namespace io{namespace impl{
+
+ template<typename Impl> class basic_io_service : private lemon::nocopyable
+ {
+ public:
+
+ typedef basic_io_service<Impl> self_type;
+
+ typedef basic_socket<self_type,typename Impl::socket_service> socket_type;
+ };
+
+}}}
+#endif //LEMON_IO_BASIC_IO_SERVICE_HPP
+
View
30 io/basic_socket.hpp
@@ -0,0 +1,30 @@
+/**
+*
+* @file basic_socket
+* @brief Copyright (C) 2012 yayanyang All Rights Reserved
+* @author yayanyang
+* @version 1.0.0.0
+* @date 2012/09/27
+*/
+#ifndef LEMON_IO_BASIC_SOCKET_HPP
+#define LEMON_IO_BASIC_SOCKET_HPP
+#include <lemon/io/abi.h>
+#include <lemonxx/sys/sys.hpp>
+#include <lemonxx/utility/utility.hpp>
+
+namespace lemon{namespace io{namespace impl{
+
+ template<typename Service,typename Impl>
+ class basic_socket : private lemon::nocopyable
+ {
+ public:
+
+ typedef Service io_service_type;
+
+
+
+ };
+}}}
+
+#endif //LEMON_IO_BASIC_SOCKET_HPP
+
View
0 io/io_service.cpp
No changes.
View
86 io/io_service.hpp
@@ -1,86 +0,0 @@
-/**
-*
-* @file io_service
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_IO_SERVICE_HPP
-#define LEMON_IO_IO_SERVICE_HPP
-#include <lemon/io/object.hpp>
-#include <lemonxx/function/bind.hpp>
-#include <lemonxx/memory/fixobj.hpp>
-
-namespace lemon{namespace io{namespace core{
-
- template<typename Impl,typename Socket>
- class basic_io_service : private lemon::nocopyable
- {
- public:
-
- typedef Impl io_service_type;
-
- typedef Socket socket_type;
-
- typedef memory::fixed::allocator<sizeof(socket_type)> socket_allocator;
-
- public:
-
- void start(size_t numbers)
- {
- _threads.start(lemon::function<void()>(&io_service_type::attach,reinterpret_cast<io_service_type*>(this)),numbers);
- }
-
- void stop()
- {
- reinterpret_cast<io_service_type*>(this)->detach();
- }
-
- void join()
- {
- _threads.join();
- }
-
- void reset()
- {
- _threads.reset();
-
- reinterpret_cast<io_service_type*>(this)->reset();
- }
-
- public:
-
- socket_type* create_socket(int af, int type, int protocol)
- {
- mutex_t::scope_lock lock(_socketAllocatorMutex);
-
- return new(_socketAllocator.alloc()) socket_type(af,type,protocol,reinterpret_cast<io_service_type*>(this));
- }
-
- socket_type* create_socket(int af, int type, int protocol, LemonNativeSock socket)
- {
- mutex_t::scope_lock lock(_socketAllocatorMutex);
-
- return new(_socketAllocator.alloc()) socket_type(af,type,protocol,socket,reinterpret_cast<io_service_type*>(this));
- }
-
- void close_socket(void * object)
- {
- mutex_t::scope_lock lock(_socketAllocatorMutex);
-
- _socketAllocator.free(object);
- }
-
- private:
-
- socket_allocator _socketAllocator;
-
- mutex_t _socketAllocatorMutex;
-
- thread_group _threads;
- };
-
-}}}
-
-#endif //LEMON_IO_IO_SERVICE_HPP
View
123 io/io_service_epoll.cpp
@@ -1,123 +0,0 @@
-#include <errno.h>
-#include <lemonxx/function/bind.hpp>
-#include <lemon/io/io_service_epoll.hpp>
-
-#ifdef LEMON_IO_EPOLL
-
-#include <unistd.h>
-#include <sys/epoll.h>
-#ifdef LEMON_HAS_EVENTFD_H
-#include <sys/eventfd.h>
-#else
-# error "not support platform"
-#endif
-
-namespace lemon{namespace io{namespace core{
-
- io_service::io_service()
- :_exit(false),_eventfd(-1),_epollfd(-1)
- {
- try
- {
- _eventfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
-
- if(-1 == _eventfd)
- {
- error_info errorCode;
-
- LEMON_POSIX_ERROR(errorCode,errno);
-
- errorCode.check_throw();
- }
-
- _epollfd = epoll_create(1);
-
- if(-1 == _epollfd)
- {
- error_info errorCode;
-
- LEMON_POSIX_ERROR(errorCode,errno);
-
- errorCode.check_throw();
- }
-
- epoll_data data;
-
- data.ptr = NULL;
-
- epoll_event epevent = {EPOLLIN,data};
-
- if(-1 == epoll_ctl(_epollfd, EPOLL_CTL_ADD, _eventfd, &epevent))
- {
- error_info errorCode;
-
- LEMON_POSIX_ERROR(errorCode,errno);
-
- errorCode.check_throw();
- }
-
- _ioworker.start(lemon::bind(&io_service::epoll_loop,this));
- }
- catch(...)
- {
- if(_epollfd != -1) close(_epollfd);
-
- if(_eventfd != -1) close(_eventfd);
-
- throw;
- }
-
-
- }
-
- io_service::~io_service()
- {
- eventfd_write(_eventfd, 1);
-
- _ioworker.join();
-
- if(_epollfd != -1) close(_epollfd);
-
- if(_eventfd != -1) close(_eventfd);
- }
-
- void io_service::epoll_loop()
- {
- epoll_event events[LEMON_IO_EPOLL_MAX_EVENTS];
-
- for(;;)
- {
- int result = 0;
-
- result = epoll_wait(_epollfd, events, LEMON_IO_EPOLL_MAX_EVENTS, -1);
-
- if(-1 == result)
- {
- if(EINTR != errno)
- {
- error_info errorCode;
-
- LEMON_POSIX_ERROR(errorCode,errno);
-
- errorCode.check_throw();
- }
- else
- {
- perror(strerror(errno));
-
- return;
- }
- }
-
- for(int i = 0; i < result; ++ i)
- {
- if(events[i].data.ptr == NULL) return;
-
- post_one((io_data *)events[i].data.ptr);
- }
- }
- }
-
-}}}
-
-#endif //LEMON_IO_EPOLL
View
101 io/io_service_epoll.hpp
@@ -1,101 +0,0 @@
-/**
-*
-* @file io_service_epoll
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_SERVICE_EPOLL_HPP
-#define LEMON_IO_SERVICE_EPOLL_HPP
-
-#include <lemon/io/socket_reactor.hpp>
-
-#ifdef LEMON_IO_EPOLL
-
-#include <lemon/io/io_service_reactor.hpp>
-
-namespace lemon{namespace io{namespace core{
-
- class io_service : public basic_io_service<io_service,socket>
- {
- public:
-
- io_service();
-
- ~io_service();
-
- void reset(){ _reactor.reset(); }
-
- void attach() { _reactor.attach(); }
-
- void detach() { _reactor.detach(); }
-
- void post_one(LemonIOCallback callback,void * userdata,LemonErrorInfo *errorCode)
- {
- _reactor.post_one(callback, userdata, 0, 0, errorCode);
- }
-
- void post_one(io_data * iodata,LemonErrorInfo *errorCode)
- {
- _reactor.post_one(iodata,errorCode);
- }
-
- void post_one(io_data * iodata)
- {
- error_info errorCode;
-
- _reactor.post_one(iodata,errorCode);
-
- errorCode.check_throw();
- }
-
- io_data * alloc_io_data(size_t type,int fd,void * userdata, LemonIOCallback callback, void * buffer, size_t bufferSize)
- {
- return _reactor.alloc_io_data(type,fd,userdata,callback,buffer,bufferSize);
- }
-
- void free_io_data(io_data * iodata)
- {
- _reactor.free_io_data(iodata);
- }
-
- accept_io_data * alloc_io_data
- (
- int fd,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- )
- {
- return _reactor.alloc_io_data(fd,callback,userdata,address,addressSize);
- }
-
- void free_accept_io_data(io_data * iodata)
- {
- _reactor.free_accept_io_data(iodata);
- }
-
- private:
-
- void epoll_loop();
-
- private:
-
- bool _exit;
-
- io_service_reactor _reactor;
-
- thread_t _ioworker;
-
- int _eventfd;
-
- int _epollfd;
- };
-
-}}}
-
-#endif //
-#endif //LEMON_IO_SERVICE_EPOLL_HPP
-
View
239 io/io_service_iocp.cpp
@@ -1,239 +0,0 @@
-#include <lemon/io/io_service_iocp.hpp>
-
-#ifdef LEMON_IO_IOCP
-
-namespace lemon{namespace io{namespace core{
-
- io_service::io_service()
- : _completionPort(CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,0,0))
- {
- if(NULL == _completionPort)
- {
- error_info errorCode;
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
-
- errorCode.check_throw();
- }
- }
-
- io_service::~io_service()
- {
- ::CloseHandle(_completionPort);
- }
-
- void io_service::attach()
- {
- error_info errorCode;
-
- DWORD numberOfBytes;
-
- ULONG_PTR completionKey;
-
- io_data *iodata = NULL;
-
- scope_counter counter(_workThreads);
-
- for(;;)
- {
- LEMON_RESET_ERRORINFO(errorCode);
-
- if(!GetQueuedCompletionStatus(_completionPort,&numberOfBytes,&completionKey,(LPOVERLAPPED*)&iodata,INFINITE))
- {
- if(NULL == iodata)
- {
- error_info errorCode;
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
-
- errorCode.check_throw();
- }
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
- }
-
- if(LEMON_IOCP_EXIT == completionKey) break;
-
- iodata->call(numberOfBytes,errorCode);
-
- if(LEMON_IOCP_IO == iodata->completekey())
- {
- free_io_data(iodata);
- }
- else
- {
- free_accept_io_data(iodata);
- }
- }
- }
-
- void io_service::detach()
- {
- size_t workingThreads = _workThreads;
-
- for(size_t i = 0; i < workingThreads; ++ i)
- {
- if(!PostQueuedCompletionStatus(_completionPort,0,LEMON_IOCP_EXIT,NULL))
- {
- error_info errorCode;
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
-
- errorCode.check_throw();
- }
- }
- }
-
- void io_service::post_one(LemonIOCallback callback,void * userdata,LemonErrorInfo *errorCode)
- {
- io_data * iodata = alloc_io_data(userdata,callback,WSABUF());
-
- if(!PostQueuedCompletionStatus(_completionPort,(DWORD)0,LEMON_IOCP_CUSTOMER_EVENT, *iodata))
- {
- LEMON_WIN32_ERROR(*errorCode,GetLastError());
- }
- }
-
- io_data * io_service::alloc_io_data(void * userdata, LemonIOCallback callback, WSABUF buffer)
- {
- mutex_t::scope_lock lock(_iodataAllocatorMutex);
-
- return new(_iodataAllocator.alloc()) io_data(LEMON_IOCP_IO,userdata,callback,buffer);
- }
-
- void io_service::free_io_data(io_data * iodata)
- {
- mutex_t::scope_lock lock(_iodataAllocatorMutex);
-
- _iodataAllocator.free(iodata);
- }
-
- accept_io_data * io_service::alloc_io_data
- (
- socket * listen,
- socket *peer,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- )
- {
- mutex_t::scope_lock lock(_acceptIODataAllocatorMutex);
-
- return new(_acceptIODataAllocator.alloc()) accept_io_data(listen,peer,callback,userdata,address,addressSize);
- }
-
- void io_service::free_accept_io_data(io_data * iodata)
- {
- mutex_t::scope_lock lock(_acceptIODataAllocatorMutex);
-
- _acceptIODataAllocator.free(iodata);
- }
-
- void io_service::bind(HANDLE handle,ULONG_PTR completekey)
- {
- if(NULL == CreateIoCompletionPort(handle,_completionPort,completekey,0))
- {
- error_info errorCode;
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
-
- errorCode.check_throw();
- }
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- io_data::io_data(ULONG_PTR completeKey,void * userdata, LemonIOCallback callback, WSABUF buffer)
- :_completeKey(completeKey)
- {
- reset(userdata,callback,buffer);
- }
-
- void io_data::reset(void * userdata, LemonIOCallback callback, WSABUF buffer)
- {
- _userdata = userdata; _callback = callback; _buffer = buffer;
- }
-
- void io_data::call(size_t numberOfBytesTransferred, LemonErrorInfo * errorCode)
- {
- _callback(_userdata,numberOfBytesTransferred,errorCode);
- }
-
- //////////////////////////////////////////////////////////////////////////
-
-
- accept_io_data::accept_io_data
- (
- socket * listen,
- socket *peer,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- )
- :io_data(LEMON_IOCP_ACCEPT)
- ,_listen(listen)
- ,_peer(peer)
- ,_callback(callback)
- ,_userdata(userdata)
- ,_address(address)
- ,_addresslen(addressSize)
- {
- WSABUF buffer = {0,0};
-
- io_data::reset(this,&accept_io_data::callback,buffer);
- }
-
- void accept_io_data::callback(void *userData,size_t numberOfBytesTransferred,const LemonErrorInfo * ec)
- {
- error_info errorCode(ec);
-
- accept_io_data * self = (accept_io_data*)userData;
-
- if(LEMON_SUCCESS(errorCode))
- {
- if(SOCKET_ERROR == setsockopt(
- self->_peer->handle(),
- SOL_SOCKET,
- SO_UPDATE_ACCEPT_CONTEXT,
- (char *)&(self->_listen->handle()),sizeof(SOCKET)))
- {
- LEMON_WIN32_ERROR(errorCode,WSAGetLastError());
- }
- else
- {
- sockaddr * localAddress,*remoteAddress;
-
- int localAddressSize,remoteAddressSize;
-
- self->_listen->get_acceptex_sockaddrs()
- (
- self->_acceptAddressBuffer,
- 0,
- LEMON_ACCEPTEX_ADDRESS_LENGTH,
- LEMON_ACCEPTEX_ADDRESS_LENGTH,
- &localAddress,
- &localAddressSize,
- &remoteAddress,
- &remoteAddressSize
- );
-
- memcpy(self->_address,remoteAddress,remoteAddressSize);
-
- *self->_addresslen = remoteAddressSize;
- }
- }
- else
- {
- self->_peer->release();
-
- self->_peer = NULL;
- }
-
- self->_callback(self->_userdata,reinterpret_cast<LemonIO>(self->_peer),numberOfBytesTransferred,errorCode);
-
- }
-}}}
-
-#endif //
View
178 io/io_service_iocp.hpp
@@ -1,178 +0,0 @@
-/**
-*
-* @file io_service_epoll
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_IO_SERVICE_IOCP_HPP
-#define LEMON_IO_IO_SERVICE_IOCP_HPP
-#include <lemon/io/io_service.hpp>
-
-#ifdef LEMON_IO_IOCP
-
-#include <lemon/io/socket_iocp.hpp>
-
-
-#define LEMON_IOCP_EXIT (ULONG_PTR)0
-
-#define LEMON_IOCP_CUSTOMER_EVENT (ULONG_PTR)1
-
-#define LEMON_IOCP_IO (ULONG_PTR)2
-
-#define LEMON_IOCP_ACCEPT (ULONG_PTR)3
-
-#ifdef LEMON_SUPPORT_IPV6
-
-#define LEMON_ACCEPTEX_ADDRESS_LENGTH (sizeof(sockaddr_in6) + 16)
-
-#else
-
-#define LEMON_ACCEPTEX_ADDRESS_LENGTH (sizeof(sockaddr_in) + 16)
-
-#endif //LEMON_SUPPORT_IPV6
-
-namespace lemon{namespace io{namespace core{
-
- class socket;
-
- //////////////////////////////////////////////////////////////////////////
-
- class io_data
- {
- public:
-
- io_data(ULONG_PTR completeKey):_completeKey(completeKey){}
-
- io_data(ULONG_PTR completeKey,void * userdata, LemonIOCallback callback, WSABUF buffer);
-
- void call(size_t numberOfBytesTransferred, LemonErrorInfo * errorCode);
-
- void reset(void * userdata, LemonIOCallback callback, WSABUF buffer);
-
- WSABUF & buffer() {return _buffer;}
-
- operator OVERLAPPED * () {return &_overlapped;}
-
- ULONG_PTR completekey() { return _completeKey;}
-
- private:
-
- OVERLAPPED _overlapped;
-
- ULONG_PTR _completeKey;
-
- void *_userdata;
-
- LemonIOCallback _callback;
-
- WSABUF _buffer;
- };
-
- //////////////////////////////////////////////////////////////////////////
-
- class accept_io_data : public io_data
- {
- public:
-
- accept_io_data
- (
- socket * listen,
- socket *peer,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- );
-
- byte_t * accept_address_buffer() {return _acceptAddressBuffer;}
-
- private:
-
- static void callback(void *userData,size_t numberOfBytesTransferred,const LemonErrorInfo * errorCode);
-
- private:
-
- socket *_listen;
-
- socket *_peer;
-
- LemonAcceptCallback _callback;
-
- void *_userdata;
-
- sockaddr *_address;
-
- socklen_t *_addresslen;
-
- lemon_byte_t _acceptAddressBuffer[LEMON_ACCEPTEX_ADDRESS_LENGTH * 2];
- };
-
- //////////////////////////////////////////////////////////////////////////
-
- class io_service : public basic_io_service<io_service,socket>
- {
- public:
-
- typedef lemon::memory::fixed::allocator<sizeof(socket)> socket_allocator;
-
- typedef lemon::memory::fixed::allocator<sizeof(io_data)> io_data_allocator;
-
- typedef lemon::memory::fixed::allocator<sizeof(accept_io_data)> accept_io_data_allocator;
-
-
- io_service();
-
- ~io_service();
-
- void reset(){}
-
- void attach();
-
- void detach();
-
- void post_one(LemonIOCallback callback,void * userdata,LemonErrorInfo *errorCode);
-
- public:
-
- void bind(HANDLE handle,ULONG_PTR completekey);
-
- public:
-
- io_data * alloc_io_data(void * userdata, LemonIOCallback callback, WSABUF buffer);
-
- void free_io_data(io_data * iodata);
-
- accept_io_data * alloc_io_data
- (
- socket * listen,
- socket *peer,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- );
-
- void free_accept_io_data(io_data * iodata);
-
- private:
-
- atomic_t _workThreads;
-
- HANDLE _completionPort;
-
- io_data_allocator _iodataAllocator;
-
- mutex_t _iodataAllocatorMutex;
-
- accept_io_data_allocator _acceptIODataAllocator;
-
- mutex_t _acceptIODataAllocatorMutex;
-
- };
-}}}
-
-#endif //LEMON_IO_IOCP
-
-#endif //LEMON_IO_IO_SERVICE_IOCP_HPP
View
0 io/io_service_kqueue.cpp
No changes.
View
27 io/io_service_kqueue.hpp
@@ -1,27 +0,0 @@
-/**
-*
-* @file io_service_kequeue
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_SERVCIE_KEQUEUE_HPP
-#define LEMON_IO_SERVCIE_KEQUEUE_HPP
-
-#ifdef LEMON_IO_KQUEUE
-#include <lemon/io/socket_reactor.hpp>
-#include <lemon/io/io_service_reactor.hpp>
-
-namespace lemon{namespace io{namespace core{
-
- class io_service : public basic_io_service<io_service,socket>
- {
-
- };
-
-}}}
-
-#endif //
-
-#endif //LEMON_IO_SERVCIE_KEQUEUE_HPP
View
326 io/io_service_reactor.cpp
@@ -1,326 +0,0 @@
-#include <lemon/io/io_service_reactor.hpp>
-
-#ifndef LEMON_IO_IOCP
-
-namespace lemon{namespace io{namespace core{
-
- io_data::io_data(size_t type, int fd):_type(type),_fd(fd) {}
-
- io_data::io_data(size_t type, int fd,void * userdata, LemonIOCallback callback, void * buffer, size_t bufferSize)
- :_type(type),_fd(fd),_userdata(userdata),_callback(callback),_buffer(buffer),_bufferSize(bufferSize)
- {
-
- }
-
- void io_data::reset(void * userdata, LemonIOCallback callback, void * buffer, size_t bufferSize)
- {
- _userdata = userdata; _callback = callback; _buffer = buffer; _bufferSize = bufferSize;
- }
-
- void io_data::call()
- {
- switch(_type)
- {
- case LEMON_REACTOR_READ:
- {
- ssize_t length = read(_fd, _buffer, _bufferSize);
-
- if(length == -1)
- {
- LEMON_POSIX_ERROR(_errorCode,errno);
- }
- else
- {
- _numberOfBytesTransferred = length;
- }
-
- break;
- }
- case LEMON_REACTOR_WRITE:
- {
- ssize_t length = write(_fd, _buffer, _bufferSize);
-
- if(length == -1)
- {
- LEMON_POSIX_ERROR(_errorCode,errno);
- }
- else
- {
- _numberOfBytesTransferred = length;
- }
-
- break;
- }
- case LEMON_REACTOR_SENDTO:
- {
- ssize_t length = read(_fd, _buffer, _bufferSize);
-
- if(length == -1)
- {
- LEMON_POSIX_ERROR(_errorCode,errno);
- }
- else
- {
- _numberOfBytesTransferred = length;
- }
-
- break;
- }
- case LEMON_REACTOR_RECVFROM:
- {
- ssize_t length = write(_fd, _buffer, _bufferSize);
-
- if(length == -1)
- {
- LEMON_POSIX_ERROR(_errorCode,errno);
- }
- else
- {
- _numberOfBytesTransferred = length;
- }
-
- break;
- }
- }
-
- _callback(_userdata,_numberOfBytesTransferred,&_errorCode);
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- accept_io_data::accept_io_data
- (
- int fd,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- )
- :io_data(LEMON_REACTOR_ACCEPT,fd)
- ,_address(address)
- ,_addresslen(addressSize)
- {
- reset(this,&accept_io_data::callback,NULL,0);
- }
-
- void accept_io_data::callback(void *userData,size_t numberOfBytesTransferred,const LemonErrorInfo * errorCode)
- {
- accept_io_data * self = (accept_io_data*)userData;
-
- //TODO: call accept function
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- sendto_io_data::sendto_io_data
- (
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- const sockaddr *address,
- socklen_t addressSize
- )
- :io_data(LEMON_REACTOR_SENDTO,fd,userdata,callback,buffer,bufferSize)
- ,_address(address)
- ,_addresslen(addressSize)
- {
-
- }
-
- ///////////////////////////////////////////////////////////////////////////
-
- recvfrom_io_data::recvfrom_io_data
- (
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- sockaddr *address,
- socklen_t *addressSize
- )
- :io_data(LEMON_REACTOR_RECVFROM,fd,userdata,callback,buffer,bufferSize)
- ,_address(address)
- ,_addresslen(addressSize)
- {
-
- }
-
-
- //////////////////////////////////////////////////////////////////////////
- io_service_reactor::io_service_reactor()
- :_completeQueue(LEMON_IO_REACTOR_SESSIONS),_exit(false)
- {
-
- }
-
- io_service_reactor::~io_service_reactor()
- {
-
- }
-
- void io_service_reactor::attach()
- {
- mutex_t::scope_lock lock(_mutex);
-
- while(!_exit)
- {
- while(!_exit && _completeQueue.empty()) _condition.wait(lock);
-
- if(!_exit)
- {
- io_data* iodata = (io_data*)_completeQueue.pop_front();
-
- iodata->call();
-
- switch(iodata->type())
- {
- case LEMON_REACTOR_ACCEPT:
- {
- free_accept_io_data(iodata);
-
- break;
- }
- case LEMON_REACTOR_SENDTO:
- {
- free_sendto_io_data(iodata);
-
- break;
- }
- case LEMON_REACTOR_RECVFROM:
- {
- free_recvfrom_io_data(iodata);
-
- break;
- }
- default:
- free_io_data(iodata);
-
- break;
- }
- }
- }
- }
-
- void io_service_reactor::detach()
- {
- mutex_t::scope_lock lock(_mutex);
-
- _exit = true;
-
- _condition.notifyall();
- }
-
- void io_service_reactor::post_one(LemonIOCallback callback,void * userdata, void * buffer, size_t bufferSize,LemonErrorInfo *errorCode)
- {
- mutex_t::scope_lock lock(_mutex);
-
- io_data * iodata = alloc_io_data(LEMON_REACTOR_POST_ONE,-1,userdata,callback,buffer,bufferSize);
-
- post_one(iodata,errorCode);
- }
-
- void io_service_reactor::post_one(io_data * iodata,LemonErrorInfo *errorCode)
- {
- LEMON_RESET_ERRORINFO(*errorCode);
-
- bool used = false;
-
- io_data* replace = (io_data*)_completeQueue.push_back(used);
-
- if(used)
- {
- iodata->call();
-
- if(iodata->type() == LEMON_REACTOR_ACCEPT)
- {
- free_accept_io_data(iodata);
- }
- else
- {
- free_io_data(iodata);
- }
- }
- }
-
- io_data * io_service_reactor::alloc_io_data(size_t type,int fd,void * userdata, LemonIOCallback callback, void * buffer, size_t bufferSize)
- {
- mutex_t::scope_lock lock(_iodataAllocatorMutex);
-
- return new(_iodataAllocator.alloc()) io_data(type,fd,userdata,callback,buffer,bufferSize);
- }
-
- void io_service_reactor::free_io_data(io_data * iodata)
- {
- mutex_t::scope_lock lock(_iodataAllocatorMutex);
-
- _iodataAllocator.free(iodata);
- }
-
- accept_io_data * io_service_reactor::alloc_io_data
- (
- int fd,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- )
- {
- mutex_t::scope_lock lock(_acceptIODataAllocatorMutex);
-
- return new(_acceptIODataAllocator.alloc()) accept_io_data(fd,callback,userdata,address,addressSize);
- }
-
- void io_service_reactor::free_accept_io_data(io_data * iodata)
- {
- mutex_t::scope_lock lock(_acceptIODataAllocatorMutex);
-
- _acceptIODataAllocator.free(iodata);
- }
-
- sendto_io_data * io_service_reactor::alloc_sendto_io_data(
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- const sockaddr *address,
- socklen_t addressSize)
- {
- mutex_t::scope_lock lock(_sendtoIODataAllocatorMutex);
-
- return new(_sendtoIODataAllocator.alloc()) sendto_io_data(fd,userdata,callback,buffer, bufferSize,address,addressSize);
- }
-
- void io_service_reactor::free_sendto_io_data(io_data * iodata)
- {
- mutex_t::scope_lock lock(_sendtoIODataAllocatorMutex);
-
- _sendtoIODataAllocator.free(iodata);
- }
-
- recvfrom_io_data * io_service_reactor::alloc_recvfrom_io_data(
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- sockaddr *address,
- socklen_t *addressSize)
- {
- mutex_t::scope_lock lock(_recvfromIODataAllocatorMutex);
-
- return new(_recvfromIODataAllocator.alloc()) sendto_io_data(fd,userdata,callback,buffer, bufferSize,address,addressSize);
- }
-
- void io_service_reactor::free_recvfrom_io_data(io_data * iodata)
- {
- mutex_t::scope_lock lock(_recvfromIODataAllocatorMutex);
-
- _recvfromIODataAllocator.free(iodata);
- }
-}}}
-
-#endif //
View
247 io/io_service_reactor.hpp
@@ -1,247 +0,0 @@
-/**
-*
-* @file io_service_reactor
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_SERVICE_REACTOR_HPP
-#define LEMON_IO_SERVICE_REACTOR_HPP
-#include <lemon/io/io_service.hpp>
-#include <lemonxx/memory/fixobj.hpp>
-#include <lemonxx/memory/ringbuffer.hpp>
-
-#ifndef LEMON_IO_IOCP
-
-#define LEMON_REACTOR_READ 0x01
-
-#define LEMON_REACTOR_WRITE 0x02
-
-#define LEMON_REACTOR_ACCEPT 0x03
-
-#define LEMON_REACTOR_POST_ONE 0x04
-
-#define LEMON_REACTOR_SENDTO 0x05
-
-#define LEMON_REACTOR_RECVFROM 0x06
-
-namespace lemon{namespace io{namespace core{
-
- class socket;
-
- class io_data
- {
- public:
-
- io_data(size_t type, int fd);
-
- io_data(size_t type, int fd,void * userdata, LemonIOCallback callback, void * buffer, size_t bufferSize);
-
- void reset(void * userdata, LemonIOCallback callback, void * buffer, size_t bufferSize);
-
- void call();
-
- void * buffer() { return _buffer; }
-
- size_t buffersize() const { return _bufferSize; }
-
- void numberOfBytesTransferred(size_t val) { _numberOfBytesTransferred = val; }
-
- LemonErrorInfo errorcode() { return _errorCode; }
-
- size_t type() { return _type; }
-
- private:
-
- size_t _type;
-
- int _fd;
-
- void *_userdata;
-
- LemonIOCallback _callback;
-
- void *_buffer;
-
- size_t _bufferSize;
-
- size_t _numberOfBytesTransferred;
-
- LemonErrorInfo _errorCode;
-
- };
-
-
- class accept_io_data : public io_data
- {
- public:
-
- accept_io_data
- (
- int fd,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- );
-
- private:
-
- static void callback(void *userData,size_t numberOfBytesTransferred,const LemonErrorInfo * errorCode);
-
- private:
-
- LemonAcceptCallback _callback;
-
- void *_userdata;
-
- sockaddr *_address;
-
- socklen_t *_addresslen;
- };
-
- class sendto_io_data : public io_data
- {
- public:
-
- sendto_io_data
- (
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- const sockaddr *address,
- socklen_t addressSize
- );
-
- private:
- const sockaddr *_address;
-
- socklen_t _addresslen;
- };
-
- class recvfrom_io_data : public io_data
- {
- public:
-
- recvfrom_io_data
- (
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- sockaddr *address,
- socklen_t *addressSize
- );
-
- private:
- sockaddr *_address;
-
- socklen_t *_addresslen;
- };
-
-
-
- class io_service_reactor : private lemon::nocopyable
- {
- public:
-
- typedef memory::fixed::allocator<sizeof(io_data)> io_data_allocator;
-
- typedef memory::fixed::allocator<sizeof(accept_io_data)> accept_io_data_allocator;
-
- typedef memory::fixed::allocator<sizeof(sendto_io_data)> sendto_io_data_allocator;
-
- typedef memory::fixed::allocator<sizeof(recvfrom_io_data)> recvfrom_io_data_allocator;
-
- typedef memory::ringbuffer::allocator<sizeof(io_data*)> complete_queue;
-
- io_service_reactor();
-
- ~io_service_reactor();
-
- void reset() {_exit = false; }
-
- void attach();
-
- void detach();
-
- void post_one(LemonIOCallback callback,void * userdata,void * buffer, size_t bufferSize,LemonErrorInfo *errorCode);
-
- void post_one(io_data * iodata,LemonErrorInfo *errorCode);
-
- public:
-
- io_data * alloc_io_data(size_t type,int fd,void * userdata, LemonIOCallback callback, void * buffer, size_t bufferSize);
-
- void free_io_data(io_data * iodata);
-
- accept_io_data * alloc_io_data
- (
- int fd,
- LemonAcceptCallback callback,
- void * userdata,
- sockaddr *address,
- socklen_t *addressSize
- );
-
- void free_accept_io_data(io_data * iodata);
-
-
- sendto_io_data * alloc_sendto_io_data(
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- const sockaddr *address,
- socklen_t addressSize);
-
- void free_sendto_io_data(io_data * iodata);
-
- recvfrom_io_data * alloc_recvfrom_io_data(
- int fd,
- void * userdata,
- LemonIOCallback callback,
- void * buffer,
- size_t bufferSize,
- sockaddr *address,
- socklen_t *addressSize);
-
- void free_recvfrom_io_data(io_data * iodata);
-
- private:
-
- io_data_allocator _iodataAllocator;
-
- mutex_t _iodataAllocatorMutex;
-
- accept_io_data_allocator _acceptIODataAllocator;
-
- mutex_t _acceptIODataAllocatorMutex;
-
- sendto_io_data_allocator _sendtoIODataAllocator;
-
- mutex_t _sendtoIODataAllocatorMutex;
-
- recvfrom_io_data_allocator _recvfromIODataAllocator;
-
- mutex_t _recvfromIODataAllocatorMutex;
-
- mutex_t _mutex;
-
- condition_variable _condition;
-
- complete_queue _completeQueue;
-
- bool _exit;
- };
-
-}}}
-
-#endif //LEMON_IO_IOCP
-
-#endif //LEMON_IO_SERVICE_REACTOR_HPP
View
42 io/object.hpp
@@ -1,42 +0,0 @@
-/**
-*
-* @file object
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_OBJECT_HPP
-#define LEMON_IO_OBJECT_HPP
-#include <cstddef>
-#include <lemon/io/abi.h>
-#include <lemonxx/sys/sys.hpp>
-#include <lemonxx/utility/utility.hpp>
-
-namespace lemon{namespace io{namespace core{
-
- class io_service;
-
- class io_object : private lemon::nocopyable
- {
- protected:
-
- io_object(io_service * service):_ioService(service) {}
-
- virtual ~io_object(){}
-
- public:
-
- virtual void release() = 0;
-
- io_service* service() { return _ioService; }
-
- const io_service* service() const { return _ioService; }
-
- private:
-
- io_service *_ioService;
- };
-}}}
-
-#endif //LEMON_IO_OBJECT_HPP
View
180 io/socket.cpp
@@ -1,180 +0,0 @@
-#include <lemon/io/socket.hpp>
-#ifdef LEMON_IO_IOCP
-#include <lemon/io/io_service_iocp.hpp>
-#elif defined(LEMON_IO_KQUEUE)
-#include <lemon/io/io_service_kqueue.hpp>
-#elif defined(LEMON_IO_EPOLL)
-#include <lemon/io/io_service_epoll.hpp>
-#endif //LEMON_IO_IOCP
-
-#ifndef WIN32
-#include <unistd.h>
-#endif //WIN32
-
-
-namespace lemon{namespace io{namespace core{
-
- socket_base::socket_base(int af, int type, int protocol,LemonNativeSock sock, io_service * service)
- :io_object(service),_handle(sock),_af(af),_type(type),_protocol(protocol)
- {
-
- }
-
- socket_base::~socket_base()
- {
-#ifdef WIN32
- ::closesocket(_handle);
-#else
- ::close(_handle);
-#endif //WIN32
- }
-
- void socket_base::release()
- {
- this->~socket_base();
-
- service()->close_socket(this);
- }
-
- void socket_base::bind(const sockaddr * name ,socklen_t length)
- {
- if(SOCKET_ERROR == ::bind(_handle,name,length))
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
- }
-
- void socket_base::shutdown(int how)
- {
- if(SOCKET_ERROR == ::shutdown(_handle,how))
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
- }
-
- void socket_base::sockname(sockaddr * name, socklen_t* bufferSize)
- {
- if(SOCKET_ERROR == ::getsockname(_handle,name,bufferSize))
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
- }
-
- size_t socket_base::send(const byte_t * buffer, size_t length, int flag)
- {
- size_t sendSize = ::send(_handle,(const char*)buffer,(int)length,flag);
-
- if(SOCKET_ERROR == sendSize)
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
-
- return sendSize;
- }
-
- size_t socket_base::sendto(const byte_t * buffer, size_t length, int flag,const sockaddr * addr, socklen_t addrlen)
- {
- size_t sendSize = ::sendto(_handle,(const char*)buffer,(int)length,flag,addr,addrlen);
-
- if(SOCKET_ERROR == sendSize)
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
-
- return sendSize;
- }
-
- size_t socket_base::receive(byte_t * buffer, size_t length, int flag)
- {
- size_t sendSize = ::recv(_handle,(char*)buffer,(int)length,flag);
-
- if(SOCKET_ERROR == sendSize)
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
-
- return sendSize;
- }
-
- size_t socket_base::recvfrom(byte_t * buffer, size_t length, int flag,sockaddr * addr,socklen_t * addrlen)
- {
- size_t sendSize = ::recvfrom(_handle,(char*)buffer,(int)length,flag,addr,addrlen);
-
- if(SOCKET_ERROR == sendSize)
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
-
- return sendSize;
- }
-
- void socket_base::connect(const sockaddr * addr, socklen_t addrlen)
- {
- if(SOCKET_ERROR == ::connect(_handle,addr,addrlen))
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
- }
-
- void socket_base::listen(int backlog)
- {
- if(SOCKET_ERROR == ::listen(_handle,backlog))
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
- }
-
- socket* socket_base::accept(sockaddr * addr,socklen_t * addrlen)
- {
- LemonNativeSock handle = ::accept(_handle,addr,addrlen);
-
- if(INVALID_SOCKET == handle)
- {
- error_info errorCode;
-
- LEMON_SOCKET_ERROR(errorCode);
-
- errorCode.check_throw();
- }
-
- return service()->create_socket(_af,_type,_protocol,handle);
- }
-
-
-}}}
View
83 io/socket.hpp
@@ -1,83 +0,0 @@
-/**
-*
-* @file socket
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_SOCKET_HPP
-#define LEMON_IO_SOCKET_HPP
-#include <lemon/io/object.hpp>
-
-#ifndef WIN32
-#define SOCKET_ERROR -1
-#define INVALID_SOCKET -1
-#define LEMON_SOCKET_ERROR(errorCode) LEMON_POSIX_ERROR(errorCode,errno)
-#else
-#define LEMON_SOCKET_ERROR(errorCode) LEMON_WIN32_ERROR(errorCode,WSAGetLastError())
-#endif //WIN32
-
-namespace lemon{namespace io{namespace core{
-
- class socket;
-
- class socket_base : public io_object
- {
- public:
-
- socket_base(int af, int type, int protocol,LemonNativeSock sock, io_service * service);
-
- virtual ~socket_base();
-
- LemonNativeSock& handle() { return _handle; }
-
- void handle(LemonNativeSock val) { _handle = val; }
-
- int af() {return _af; }
-
- int type() {return _type; }
-
- int protocol() {return _protocol; }
-
- public:
-
- void release();
-
- public:
-
- void bind(const sockaddr * name ,socklen_t length);
-
- void shutdown(int how);
-
- void sockname(sockaddr * name, socklen_t* bufferSize);
-
- size_t send(const byte_t * buffer, size_t length, int flag);
-
- size_t sendto(const byte_t * buffer, size_t length, int flag,const sockaddr * addr, socklen_t addrlen);
-
- size_t receive(byte_t * buffer, size_t length, int flag);
-
- size_t recvfrom(byte_t * buffer, size_t length, int flag,sockaddr * addr,socklen_t * addrlen);
-
- void connect(const sockaddr * addr, socklen_t addrlen);
-
- socket* accept(sockaddr * addr,socklen_t * addrlen);
-
- void listen(int backlog);
-
- private:
-
- LemonNativeSock _handle;
-
- int _af;
-
- int _type;
-
- int _protocol;
-
- };
-
-}}}
-
-#endif //LEMON_IO_SOCKET_HPP
View
230 io/socket_iocp.cpp
@@ -1,230 +0,0 @@
-#include <lemon/io/socket_iocp.hpp>
-
-#ifdef LEMON_IO_IOCP
-#include <lemon/io/io_service_iocp.hpp>
-
-const static GUID GuidAcceptEx = WSAID_ACCEPTEX;
-
-const static GUID GuidConnectionEx = WSAID_CONNECTEX;
-
-const static GUID GuidGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
-
-namespace lemon{namespace io{namespace core{
-
- template<class LPFN>
- LPFN LoadWinSocketExtension(SOCKET socket,const GUID *guid)
- {
- DWORD dwBytes;
-
- LPFN fn;
-
- if(SOCKET_ERROR == WSAIoctl(socket,
- SIO_GET_EXTENSION_FUNCTION_POINTER,
- (LPVOID)guid,
- sizeof(GUID),
- &fn,
- sizeof(fn),
- &dwBytes,
- NULL,
- NULL))
- {
- error_info errorCode;
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
-
- errorCode.check_throw();
- }
-
- return fn;
- }
-
- socket::socket(int af, int type, int protocol,io_service * service)
- :socket_base(af,type,protocol,WSASocket(af,type,protocol,NULL,0,WSA_FLAG_OVERLAPPED),service)
- {
- if(INVALID_SOCKET == handle())
- {
- error_info errorCode;
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
-
- errorCode.check_throw();
- }
-
- try
- {
- // close SIO_UDP_CONNRESET
- if(SOCK_DGRAM == type)
- {
- BOOL bNewBehavior = FALSE;
-
- DWORD dwBytesReturned = 0;
-
- DWORD status = WSAIoctl(handle(),SIO_UDP_CONNRESET,&bNewBehavior,sizeof(bNewBehavior),NULL, 0, &dwBytesReturned,NULL,NULL);
-
- if(SOCKET_ERROR == status)
- {
- error_info errorCode;
-
- LEMON_WIN32_ERROR(errorCode,GetLastError());
-
- errorCode.check_throw();
- }
- }
-
- _acceptEx = LoadWinSocketExtension<LPFN_ACCEPTEX>(handle(),&GuidAcceptEx);
-
- _connectEx = LoadWinSocketExtension<LPFN_CONNECTEX>(handle(),&GuidConnectionEx);
-
- _getAcceptExSockaddrs = LoadWinSocketExtension<LPFN_GETACCEPTEXSOCKADDRS>(handle(),&GuidGetAcceptExSockaddrs);
-
- service->bind((HANDLE)handle(),LEMON_IOCP_IO);
- }
- catch(...)
- {
- ::closesocket(handle());
-
- throw;//re throw the exception
- }
- }
-
- socket::socket(int af, int type, int protocol,LemonNativeSock sock,io_service * service)
- :socket_base(af,type,protocol,sock,service)
- {
-
- }
-
-
- void socket::async_send(const byte_t * buffer, size_t length, int flag,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode)
- {
- WSABUF wsaBuf = {(ULONG)length,(CHAR*)buffer};
-
- io_data * iodata = service()->alloc_io_data(userdata,callback,wsaBuf);
-
- if(0 != WSASend(handle(),&iodata->buffer(),1,NULL,flag,*iodata,NULL))
- {
- DWORD lastError = WSAGetLastError();
-
- if(WSA_IO_PENDING != lastError)
- {
- LEMON_WIN32_ERROR(*errorCode,lastError);
-
- service()->free_io_data(iodata);
- }
- }
- }
-
- void socket::async_sendto(const byte_t * buffer, size_t length, int flag,const sockaddr * addr, socklen_t addrlen,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode)
- {
- WSABUF wsaBuf = {(ULONG)length,(CHAR*)buffer};
-
- io_data * iodata = service()->alloc_io_data(userdata,callback,wsaBuf);
-
- if(0 != WSASendTo(handle(),&iodata->buffer(),1,NULL,flag,addr,addrlen,*iodata,NULL))
- {
- DWORD lastError = WSAGetLastError();
-
- if(WSA_IO_PENDING != lastError)
- {
- LEMON_WIN32_ERROR(*errorCode,lastError);
-
- service()->free_io_data(iodata);
- }
- }
- }
-
- void socket::async_receive(byte_t * buffer, size_t length, int flag,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode)
- {
- WSABUF wsaBuf = {(ULONG)length,(CHAR*)buffer};
-
- io_data * iodata = service()->alloc_io_data(userdata,callback,wsaBuf);
-
- DWORD placeholder = flag;
-
- if(0 != WSARecv(handle(),&iodata->buffer(),1,NULL,&placeholder,*iodata,NULL))
- {
- DWORD lastError = WSAGetLastError();
-
- if(WSA_IO_PENDING != lastError)
- {
- LEMON_WIN32_ERROR(*errorCode,lastError);
-
- service()->free_io_data(iodata);
- }
- }
- }
-
- void socket::async_recvfrom(byte_t * buffer, size_t length, int flag,sockaddr * addr,socklen_t * addrlen,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode)
- {
- WSABUF wsaBuf = {(ULONG)length,(CHAR*)buffer};
-
- io_data * iodata = service()->alloc_io_data(userdata,callback,wsaBuf);
-
- DWORD placeholder = flag;
-
- if(0 != WSARecvFrom(handle(),&iodata->buffer(),1,NULL,&placeholder,addr,addrlen,*iodata,NULL))
- {
- DWORD lastError = WSAGetLastError();
-
- if(WSA_IO_PENDING != lastError)
- {
- LEMON_WIN32_ERROR(*errorCode,lastError);
-
- service()->free_io_data(iodata);
- }
- }
- }
-
- void socket::async_connect(const sockaddr * addr, socklen_t addrlen,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode)
- {
- WSABUF wsaBuf = {0};
-
- io_data * iodata = service()->alloc_io_data(userdata,callback,wsaBuf);
-
- DWORD bytesSent;
-
- DWORD sendBytes = 0;
-
- if(!_connectEx(handle(),addr,addrlen,NULL,sendBytes,&bytesSent,*iodata))
- {
- if(ERROR_IO_PENDING != GetLastError())
- {
- LEMON_WIN32_ERROR(*errorCode,GetLastError());
-
- service()->free_io_data(iodata);
- }
- }
- }
-
- void socket::async_accept(sockaddr * addr,socklen_t * addrlen,LemonAcceptCallback callback, void * userdata,LemonErrorInfo *errorCode)
- {
- socket * peer = service()->create_socket(af(),type(),protocol());
-
- accept_io_data * iodata = service()->alloc_io_data(this,peer,callback,userdata,addr,addrlen);
-
- DWORD bytesReceived;
-
- if(!_acceptEx(
- handle(),
- peer->handle(),
- iodata->accept_address_buffer(),
- 0,
- LEMON_ACCEPTEX_ADDRESS_LENGTH,
- LEMON_ACCEPTEX_ADDRESS_LENGTH,
- &bytesReceived,
- *iodata
- ))
- {
- DWORD error = WSAGetLastError();
-
- if(ERROR_IO_PENDING != error){
-
- LEMON_WIN32_ERROR(*errorCode,error);
-
- service()->free_accept_io_data(iodata);
- }
- }
- }
-
-}}}
-
-#endif //LEMON_IO_IOCP
View
64 io/socket_iocp.hpp
@@ -1,64 +0,0 @@
-/**
-*
-* @file socket_iocp
-* @brief Copyright (C) 2012 yayanyang All Rights Reserved
-* @author yayanyang
-* @version 1.0.0.0
-* @date 2012/09/12
-*/
-#ifndef LEMON_IO_SOCKET_IOCP_HPP
-#define LEMON_IO_SOCKET_IOCP_HPP
-
-#include <lemon/io/socket.hpp>
-
-#ifdef LEMON_IO_IOCP
-
-#include <Mswsock.h>
-
-#pragma comment(lib,"Mswsock.lib")
-
-namespace lemon{namespace io{namespace core{
-
- class io_service;
-
- class socket : public socket_base
- {
- public:
-
- socket(int af, int type, int protocol,io_service * service);
-
- socket(int af, int type, int protocol,LemonNativeSock sock,io_service * service);
-
- LPFN_GETACCEPTEXSOCKADDRS get_acceptex_sockaddrs()
- {
- return _getAcceptExSockaddrs;
- }
-
- public:
-
- void async_send(const byte_t * buffer, size_t length, int flag,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode);
-
- void async_sendto(const byte_t * buffer, size_t length, int flag,const sockaddr * addr, socklen_t addrlen,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode);
-
- void async_receive(byte_t * buffer, size_t length, int flag,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode);
-
- void async_recvfrom(byte_t * buffer, size_t length, int flag,sockaddr * addr,socklen_t * addrlen,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode);
-
- void async_connect(const sockaddr * addr, socklen_t addrlen,LemonIOCallback callback, void * userdata,LemonErrorInfo *errorCode);
-
- void async_accept(sockaddr * addr,socklen_t * addrlen,LemonAcceptCallback callback, void * userdata,LemonErrorInfo *errorCode);
-
- private:
-
- LPFN_ACCEPTEX _acceptEx;
-
- LPFN_CONNECTEX _connectEx;
-
- LPFN_GETACCEPTEXSOCKADDRS _getAcceptExSockaddrs;
- };