Permalink
Browse files

update

Signed-off-by: yayanyang <yayanyang@gmail.com>
  • Loading branch information...
yayanyang committed Sep 27, 2012
1 parent 614038e commit d2c4802bb99c05103919e4f4e0ab29984ea5b7d5
View
Binary file not shown.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -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
@@ -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
@@ -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
@@ -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
No changes.
View
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit d2c4802

Please sign in to comment.