Permalink
Browse files

implement io epoll/aio

  • Loading branch information...
1 parent b39abab commit 7690df4dd5e8b24eb4ba8762511b15c9af2cf3d0 yayanyang committed Sep 4, 2012
Showing with 290 additions and 9 deletions.
  1. +5 −4 io/abi.cpp
  2. +1 −0 io/configure.h.in
  3. +1 −0 io/epoll/epoll.hpp
  4. +2 −0 io/epoll/io_service.cpp
  5. +42 −0 io/epoll/io_service.hpp
  6. +130 −0 io/posix/socket.cpp
  7. +90 −0 io/posix/socket.hpp
  8. +14 −0 io/unix.cmake
  9. +4 −4 sys/net.cpp
  10. +1 −1 sys/unix.cmake
View
@@ -2,10 +2,11 @@
#ifdef LEMON_IO_IOCP
# include <lemon/io/iocp/iocp.hpp>
+#elif defined(LEMON_IO_EPOLL)
+# include <lemon/io/epoll/epoll.hpp>
+# include <lemon/io/posix/socket.hpp>
#else
-# error "not implement"
-#endif
-
+#endif //
using namespace lemon;
using namespace lemon::io;
@@ -482,4 +483,4 @@ LEMON_IO_API
{
*errorCode = e;
}
-}
+}
View
@@ -1,3 +1,4 @@
#cmakedefine LEMON_IO_IOCP
+#cmakedefine LEMON_IO_EPOLL
View
@@ -0,0 +1 @@
+#include <lemon/io/epoll/io_service.hpp>
View
@@ -0,0 +1,2 @@
+#include <libaio.h>
+#include <sys/epoll.h>
View
@@ -0,0 +1,42 @@
+/**
+*
+* @file io_service
+* @brief Copyright (C) 2012 yayanyang All Rights Reserved
+* @author yayanyang
+* @version 1.0.0.0
+* @date 2012/08/30
+*/
+#ifndef LEMON_IO_EPOLL_IO_SERVICE_HPP
+#define LEMON_IO_EPOLL_IO_SERVICE_HPP
+
+#include <lemon/io/io_service.hpp>
+
+namespace lemon{namespace io{
+ class IOService : public IOServiceT<IOService>
+ {
+ public:
+
+ IOService(size_t workthreads);
+
+ ~IOService();
+
+ void Run();
+
+ void Cancel();
+
+ public:
+
+ void Bind(int file);
+
+ void PostJob(LemonIOCallback callback, void * userdata);
+
+ private:
+
+ int _epollfd;
+
+ int _eventfd;
+ };
+}}
+
+#endif //LEMON_IO_EPOLL_IO_SERVICE_HPP
+
View
@@ -0,0 +1,130 @@
+#include <lemon/io/posix/socket.hpp>
+
+#ifndef LEMON_IO_IOCP
+
+#include <errno.h>
+#include <unistd.h>
+
+#define SOCKET_ERROR -1
+
+namespace lemon{namespace io{
+
+ Socket::Socket(int af, int type, int protocol,int handle,IOService * service)
+ :BaseType(service),_handle(handle),_af(af),_type(type),_protocol(protocol)
+ {}
+
+ Socket::Socket(int af, int type, int protocol,IOService * service)
+ :BaseType(service),_af(af),_type(type),_protocol(protocol)
+ {
+ _handle = socket( af, type , protocol );
+
+ if(-1 == _handle)
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(errorCode,errno);
+ }
+
+ service->Bind(_handle);
+ }
+
+ Socket::~Socket()
+ {
+ ::close(_handle);
+ }
+
+ void Socket::Bind(const sockaddr * name ,socklen_t length)
+ {
+ if(SOCKET_ERROR == ::bind(_handle,name,length))
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+ }
+
+ void Socket::Shutdown(int how)
+ {
+ if(SOCKET_ERROR == ::shutdown(_handle,how))
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+ }
+
+ void Socket::SockName(sockaddr * name, socklen_t* bufferSize)
+ {
+ if(SOCKET_ERROR == ::getsockname(_handle,name,bufferSize))
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+ }
+
+ size_t Socket::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)
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+
+ return sendSize;
+ }
+
+ size_t Socket::Recieve(byte_t * buffer, size_t length, int flag)
+ {
+ size_t sendSize = ::recv(_handle,(char*)buffer,(int)length,flag);
+
+ if(SOCKET_ERROR == sendSize)
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+
+ return sendSize;
+ }
+
+ void Socket::Connect(const sockaddr * addr, socklen_t addrlen)
+ {
+ if(SOCKET_ERROR == ::connect(_handle,addr,addrlen))
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+ }
+
+ void Socket::Listen(int backlog)
+ {
+ if(SOCKET_ERROR == ::listen(_handle,backlog))
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+ }
+
+ Socket * Socket::Accept(sockaddr * addr,socklen_t * addrlen)
+ {
+ int handle = ::accept(_handle,addr,addrlen);
+
+ if(-1 == handle)
+ {
+ scope_error_info errorCode;
+
+ LEMON_POSIX_ERROR(*errorCode,errno);
+ }
+
+ return new(Service()) Socket(_af,_type,_protocol,handle,Service());
+ }
+}}
+
+#endif //#ifndef LEMON_IO_IOCP
View
@@ -0,0 +1,90 @@
+/**
+*
+* @file socket
+* @brief Copyright (C) 2012 yayanyang All Rights Reserved
+* @author yayanyang
+* @version 1.0.0.0
+* @date 2012/08/31
+*/
+#ifndef LEMON_IO_POSIX_SOCKET_HPP
+#define LEMON_IO_POSIX_SOCKET_HPP
+#include <lemon/io/object.hpp>
+
+#ifndef LEMON_IO_IOCP
+
+#ifdef LEMON_IO_EPOLL
+#include <lemon/io/epoll/epoll.hpp>
+#else
+#endif //LEMON_IO_EPOLL
+
+namespace lemon{namespace io{
+
+ class Socket : public ObjectT<Socket,IOService>
+ {
+ public:
+ typedef ObjectT<Socket,IOService> BaseType;
+
+ Socket(int af, int type, int protocol,int handle,IOService * service);
+
+ Socket(int af, int type, int protocol,IOService * service);
+
+ ~Socket();
+
+ operator const int () const { return _handle; }
+
+ operator int () { return _handle; }
+
+ int handle() { return _handle; }
+
+ 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);
+
+ void Send(const byte_t * buffer, size_t length, int flag , LemonIOCallback callback, void * userdata);
+
+ size_t SendTo(const byte_t * buffer, size_t length, int flag,const sockaddr * addr, socklen_t addrlen);
+
+ void SendTo(const byte_t * buffer, size_t length, int flag ,const sockaddr * addr, socklen_t addrlen, LemonIOCallback callback, void * userdata);
+
+ size_t Recieve(byte_t * buffer, size_t length, int flag);
+
+ void Recieve(byte_t * buffer, size_t length, int flag , LemonIOCallback callback, void * userdata);
+
+ size_t RecieveFrom(byte_t * buffer, size_t length, int flag,sockaddr * addr,socklen_t * addrlen);
+
+ void RecieveFrom(byte_t * buffer, size_t length, int flag , sockaddr * addr,socklen_t * addrlen, LemonIOCallback callback, void * userdata);
+
+ void Connect(const sockaddr * addr, socklen_t addrlen);
+
+ void Connect(const sockaddr * addr, socklen_t addrlen, LemonIOCallback callback, void * userdata);
+
+ void Listen(int backlog);
+
+ Socket * Accept(sockaddr * addr,socklen_t * addrlen);
+
+ void Accept(sockaddr * addr,socklen_t * addrlen,LemonAcceptCallback callback, void * userdata);
+
+ private:
+
+ int _handle;
+
+ int _af;
+
+ int _type;
+
+ int _protocol;
+
+ };
+
+}}
+
+#endif //#ifndef LEMON_IO_IOCP
+
+#endif //LEMON_IO_POSIX_SOCKET_HPP
+
View
@@ -0,0 +1,14 @@
+include(CheckIncludeFiles)
+
+check_include_files(sys/epoll.h LEMON_IO_EPOLL)
+
+if(LEMON_IO_EPOLL)
+ check_include_files(libaio.h LEMON_IO_USER_LIBAIO)
+
+ if(NOT LEMON_IO_USER_LIBAIO)
+ lemon_error("on linux platform, we need libaio library,please install it.or update your linux kernel")
+ endif()
+
+ lemon_project_link_libs(aio)
+
+endif()
View
@@ -214,13 +214,13 @@ LEMON_SYS_PRIVATE int inet_pton_v6(const char *src, void *dst)
{
static const char xdigits_l[] = "0123456789abcdef",
xdigits_u[] = "0123456789ABCDEF";
- u_char tmp[sizeof(in_addr6)], *tp, *endp, *colonp;
+ u_char tmp[sizeof(in6_addr)], *tp, *endp, *colonp;
const char *xdigits, *curtok;
int ch, saw_xdigit;
u_int val;
- memset((tp = tmp), '\0', sizeof(in_addr6));
- endp = tp + sizeof(in_addr6);
+ memset((tp = tmp), '\0', sizeof(in6_addr));
+ endp = tp + sizeof(in6_addr);
colonp = NULL;
/* Leading :: requires some special handling. */
if (*src == ':')
@@ -292,7 +292,7 @@ LEMON_SYS_PRIVATE int inet_pton_v6(const char *src, void *dst)
}
if (tp != endp)
return (0);
- memcpy(dst, tmp, sizeof(in_addr6));
+ memcpy(dst, tmp, sizeof(in6_addr));
return (1);
}
View
@@ -77,7 +77,7 @@ endif()
set(LEMON_UUID_BUILDIN TRUE)
-lemon_package_lib(dl)
+#lemon_package_lib(dl)
##########################################################################################################################################
#

0 comments on commit 7690df4

Please sign in to comment.