手机版
你好,游客 登录 注册
背景:
阅读新闻

C语言socket编程模型

[日期:2012-09-24] 来源:Linux社区  作者:zmxiangde [字体:大 中 小]

四,连接处理

在mio_imph.h文件中实现,如下:

  1. static mio_fd_t _mio_connect(mio_t m, int port, char *hostip, mio_handler_t app, void *arg)  
  2. {  
  3.     int fd, flag, flags;  
  4.     mio_fd_t mio_fd;  
  5.     struct sockaddr_storage sa;  
  6.   
  7.     memset(&sa, 0, sizeof(sa));  
  8.   
  9.     if(m == NULL || port <= 0 || hostip == NULL) return NULL;  
  10.   
  11.     mio_debug(ZONE, "mio connecting to %s, port=%d",hostip,port);  
  12.   
  13.     /* convert the hostip */  
  14.     if(j_inet_pton(hostip, &sa)<=0) {  
  15.         MIO_SETERROR(EFAULT);  
  16.         return NULL;  
  17.     }  
  18.   
  19.     if(!sa.ss_family) sa.ss_family = AF_INET;  
  20.     /* attempt to create a socket */  
  21.     if((fd = socket(sa.ss_family,SOCK_STREAM,0)) < 0) return NULL;  
  22.   
  23.     /* set the socket to non-blocking before connecting */  
  24. #if defined(HAVE_FCNTL)   
  25.     flags = fcntl(fd, F_GETFL);  
  26.     flags |= O_NONBLOCK;  
  27.     fcntl(fd, F_SETFL, flags);  
  28. #elif defined(HAVE_IOCTL)   
  29.     flags = 1;  
  30.     ioctl(fd, FIONBIO, &flags);  
  31. #endif   
  32.   
  33.     /* set up address info */  
  34.     j_inet_setport(&sa, port);  
  35.   
  36.     /* try to connect */  
  37.     flag = connect(fd,(struct sockaddr*)&sa,j_inet_addrlen(&sa));  
  38.   
  39.     mio_debug(ZONE, "connect returned %d and %s", flag, MIO_STRERROR(MIO_ERROR));  
  40.   
  41.     /* already connected?  great! */  
  42.     if(flag == 0)  
  43.     {  
  44.         mio_fd = _mio_setup_fd(m,fd,app,arg);  
  45.         if(mio_fd != NULL) return mio_fd;  
  46.     }  
  47.   
  48.     /* gotta wait till later */  
  49. #ifdef _WIN32   
  50.     if(flag == -1 && WSAGetLastError() == WSAEWOULDBLOCK)  
  51. #else   
  52.     if(flag == -1 && errno == EINPROGRESS)  
  53. #endif   
  54.     {  
  55.         mio_fd = _mio_setup_fd(m,fd,app,arg);  
  56.         if(mio_fd != NULL)  
  57.         {  
  58.             mio_debug(ZONE, "connect processing non-blocking mode");  
  59.   
  60.             FD(m,mio_fd)->type = type_CONNECT;  
  61.             MIO_SET_WRITE(m,FD(m,mio_fd));  
  62.             return mio_fd;  
  63.         }  
  64.     }  
  65.   
  66.     /* bummer dude */  
  67.     close(fd);  
  68.     return NULL;  
  69. }  

在调用 connect连接成功之后,得到的是一个连接描述符,需要将这个SOCKET描述符和我们的mio_st模型连接起来,怎么连接呢?_mio_setup_fd方法完成这一步:

  1. static mio_fd_t _mio_setup_fd(mio_t m, int fd, mio_handler_t app, void *arg)  
  2. {  
  3.     int flags;  
  4.     mio_fd_t mio_fd;  
  5.   
  6.     mio_fd = MIO_ALLOC_FD(m, fd);  
  7.     /* ok to process this one, welcome to the family */  
  8.     FD(m,mio_fd)->type = type_NORMAL;  
  9.     FD(m,mio_fd)->app = app;  
  10.     FD(m,mio_fd)->arg = arg;  
  11.   
  12.     /* set the socket to non-blocking */  
  13. #if defined(HAVE_FCNTL)   
  14.     flags = fcntl(fd, F_GETFL);  
  15.     flags |= O_NONBLOCK;  
  16.     fcntl(fd, F_SETFL, flags);  
  17. #elif defined(HAVE_IOCTL)   
  18.     flags = 1;  
  19.     ioctl(fd, FIONBIO, &flags);  
  20. #endif   
  21.   
  22.     return mio_fd;  
  23. }  

这究竟做了哪些事情呢?MIO_ALLOC_FD是IO策略相关的,定义如下:

  1. static mio_fd_t _mio_alloc_fd(mio_t m, int fd)                      \  
  2. {                                                                   \  
  3.     struct epoll_event event;                                       \  
  4.     mio_priv_fd_t priv_fd = malloc(sizeof (struct mio_priv_fd_st)); \  
  5.     memset(priv_fd, 0, sizeof (struct mio_priv_fd_st));             \  
  6.                                                                     \  
  7.     priv_fd->mio_fd.fd = fd;                                        \  
  8.     priv_fd->events = 0;                                            \  
  9.                                                                     \  
  10.     event.events = priv_fd->events;                                 \  
  11.     event.data.u64 = 0;                                             \  
  12.     event.data.ptr = priv_fd;                              \  
  13.     epoll_ctl(MIO(m)->epoll_fd, EPOLL_CTL_ADD, fd, &event);         \  
  14.                                                                     \  
  15.     return (mio_fd_t)priv_fd;                                        \  
  16. }  

将fd和mio_st关联起来,通过mio_priv_fd_t结构,这个结构的定义如下:

  1. typedef enum {   
  2.     type_CLOSED = 0x00,   
  3.     type_NORMAL = 0x01,   
  4.     type_LISTEN = 0x02,   
  5.     type_CONNECT = 0x10,   
  6.     type_CONNECT_READ = 0x11,  
  7.     type_CONNECT_WRITE = 0x12  
  8. } mio_type_t;  
  9. typedef struct mio_priv_fd_st  
  10. {  
  11.     struct mio_fd_st mio_fd;  
  12.   
  13.     mio_type_t type;   
  14.     mio_handler_t app;  
  15.     void *arg;  
  16.   
  17.     MIO_FD_VARS  
  18. } *mio_priv_fd_t;  

这里的MIO_FD_VARS宏是IO策略相关的,当用EPOLL的时候,其定义如下:

  1. #define MIO_FD_VARS \   
  2.     uint32_t events;  

回顾一下connect的过程:

根据IP和端口调用系统调用connect,返回连接文件描述符fd。
调用_mio_alloc_fd函数将fd和mio_st实例关联起来,通过mio_priv_fd_t结构关联起来,并且将该fd添加到epoll实例中。从这以后,如果这个描述符(fd)有读写或者错误时,epoll会通知给上层应用。

linux
相关资讯       C语言  Socket编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款