四,连接处理
在mio_imph.h文件中实现,如下:
- static mio_fd_t _mio_connect(mio_t m, int port, char *hostip, mio_handler_t app, void *arg)
- {
- int fd, flag, flags;
- mio_fd_t mio_fd;
- struct sockaddr_storage sa;
- memset(&sa, 0, sizeof(sa));
- if(m == NULL || port <= 0 || hostip == NULL) return NULL;
- mio_debug(ZONE, "mio connecting to %s, port=%d",hostip,port);
- /* convert the hostip */
- if(j_inet_pton(hostip, &sa)<=0) {
- MIO_SETERROR(EFAULT);
- return NULL;
- }
- if(!sa.ss_family) sa.ss_family = AF_INET;
- /* attempt to create a socket */
- if((fd = socket(sa.ss_family,SOCK_STREAM,0)) < 0) return NULL;
- /* set the socket to non-blocking before connecting */
- #if defined(HAVE_FCNTL)
- flags = fcntl(fd, F_GETFL);
- flags |= O_NONBLOCK;
- fcntl(fd, F_SETFL, flags);
- #elif defined(HAVE_IOCTL)
- flags = 1;
- ioctl(fd, FIONBIO, &flags);
- #endif
- /* set up address info */
- j_inet_setport(&sa, port);
- /* try to connect */
- flag = connect(fd,(struct sockaddr*)&sa,j_inet_addrlen(&sa));
- mio_debug(ZONE, "connect returned %d and %s", flag, MIO_STRERROR(MIO_ERROR));
- /* already connected? great! */
- if(flag == 0)
- {
- mio_fd = _mio_setup_fd(m,fd,app,arg);
- if(mio_fd != NULL) return mio_fd;
- }
- /* gotta wait till later */
- #ifdef _WIN32
- if(flag == -1 && WSAGetLastError() == WSAEWOULDBLOCK)
- #else
- if(flag == -1 && errno == EINPROGRESS)
- #endif
- {
- mio_fd = _mio_setup_fd(m,fd,app,arg);
- if(mio_fd != NULL)
- {
- mio_debug(ZONE, "connect processing non-blocking mode");
- FD(m,mio_fd)->type = type_CONNECT;
- MIO_SET_WRITE(m,FD(m,mio_fd));
- return mio_fd;
- }
- }
- /* bummer dude */
- close(fd);
- return NULL;
- }
在调用 connect连接成功之后,得到的是一个连接描述符,需要将这个SOCKET描述符和我们的mio_st模型连接起来,怎么连接呢?_mio_setup_fd方法完成这一步:
- static mio_fd_t _mio_setup_fd(mio_t m, int fd, mio_handler_t app, void *arg)
- {
- int flags;
- mio_fd_t mio_fd;
- mio_fd = MIO_ALLOC_FD(m, fd);
- /* ok to process this one, welcome to the family */
- FD(m,mio_fd)->type = type_NORMAL;
- FD(m,mio_fd)->app = app;
- FD(m,mio_fd)->arg = arg;
- /* set the socket to non-blocking */
- #if defined(HAVE_FCNTL)
- flags = fcntl(fd, F_GETFL);
- flags |= O_NONBLOCK;
- fcntl(fd, F_SETFL, flags);
- #elif defined(HAVE_IOCTL)
- flags = 1;
- ioctl(fd, FIONBIO, &flags);
- #endif
- return mio_fd;
- }
这究竟做了哪些事情呢?MIO_ALLOC_FD是IO策略相关的,定义如下:
- static mio_fd_t _mio_alloc_fd(mio_t m, int fd) \
- { \
- struct epoll_event event; \
- mio_priv_fd_t priv_fd = malloc(sizeof (struct mio_priv_fd_st)); \
- memset(priv_fd, 0, sizeof (struct mio_priv_fd_st)); \
- \
- priv_fd->mio_fd.fd = fd; \
- priv_fd->events = 0; \
- \
- event.events = priv_fd->events; \
- event.data.u64 = 0; \
- event.data.ptr = priv_fd; \
- epoll_ctl(MIO(m)->epoll_fd, EPOLL_CTL_ADD, fd, &event); \
- \
- return (mio_fd_t)priv_fd; \
- }
将fd和mio_st关联起来,通过mio_priv_fd_t结构,这个结构的定义如下:
- typedef enum {
- type_CLOSED = 0x00,
- type_NORMAL = 0x01,
- type_LISTEN = 0x02,
- type_CONNECT = 0x10,
- type_CONNECT_READ = 0x11,
- type_CONNECT_WRITE = 0x12
- } mio_type_t;
- typedef struct mio_priv_fd_st
- {
- struct mio_fd_st mio_fd;
- mio_type_t type;
- mio_handler_t app;
- void *arg;
- MIO_FD_VARS
- } *mio_priv_fd_t;
这里的MIO_FD_VARS宏是IO策略相关的,当用EPOLL的时候,其定义如下:
- #define MIO_FD_VARS \
- uint32_t events;
回顾一下connect的过程:
根据IP和端口调用系统调用connect,返回连接文件描述符fd。
调用_mio_alloc_fd函数将fd和mio_st实例关联起来,通过mio_priv_fd_t结构关联起来,并且将该fd添加到epoll实例中。从这以后,如果这个描述符(fd)有读写或者错误时,epoll会通知给上层应用。
