• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            jake1036

            larbin源碼分析(一) gloabl文件 Connexion結構

            larbin源碼分析(一) 從gloabl文件分析每一個結構

              一 本系列主要是分析larbin開源爬蟲的源代碼,主要思路是先從global文件中的各個重要的結構開始。
                 1   Connexion 此處為一個結構體
                      該結構體主要的作用是進行連接服務器的操作。其中析構函數基本不執行,因為此結構是循環利用的,在
                      程序中保持一定的數量。小擴展:FetchOpen 類主要用來建立連接,而FetchPipe類主要用來進行連接之后的數據交換。
                   結構體中成員變量
                    struct Connexion{
                        char state ; //表示socket的狀態EMPTY , CONNECTING , WRITE . OPEN
                        int pos ;    //請求被發送到的位置
                        FetchError err ; //查詢如何終止的
                        int socket ;  // number of the fds
                        int timeout ;  //鏈接的超時值
                       LarbinString request ; //http 請求報頭
                       file * parser ; //對下載的網頁進行解析
                       char buffer[maxPageSize] ;  //存儲下載的網頁數據
                      Connexion() ;
                     ~Connexion() ;
                   //recycle
                     void recycle() ; //此處主要進行循環使用
                 } ;
             2 具體成員函數的實現
              

            Connexion::Connexion() //具體將socket的狀態變為emptyC
            {                                  //將文件解析句柄變為空
              state = emptyC ;
              parser = NULL ; 

            }
             Connexion::~Connexion()  //保證一旦調用,即報告錯誤
            {
              assert(false) ;
            }
             
            /*recycle a connexion*/
            void Connexion::recycle() //循環使用該結構體
            {
              delete parser ;              //刪除解析對象
              request.recycle() ;        //對LarbinString 調用recycle函數。
            }

             3 utils包下的connexion.h 和 connexion.cc的具體代碼實現
               

            // Larbin
            // Sebastien Ailleret
            // 15-11-99 -> 14-12-99

            #ifndef CONNEXION_H
            #define CONNEXION_H

            /* make write until everything is written
             * return 0 on success, 1 otherwise
             * Don't work on non-blocking fds
             
            */

            int ecrire (int fd, char *buf);

            /* make write until everything is written
             * return 0 on success, 1 otherwise
             * Don't work on non-blocking fds
             
            */

            int ecrireBuff (int fd, char *buf, int count);

            /** Write an int on a fds
             * (uses ecrire)
             
            */

            int ecrireInt (int fd, int i);
            int ecrireInt2 (int fd, int i);
            int ecrireInti (int fd, int i, char *f);
            int ecrireIntl (int fd, long i, char *f);

            /** Write an int on a fds
             * (uses ecrire)
             
            */

            int ecrireLong (int fd, long i);

            /* Write a char on a fds
             * return 0 on success, 1 otherwise
             * Don't work on non-blocking fds
             
            */

            int ecrireChar (int fd, char c);


            #endif // CONNEXION_H


             在connexion.h中各個成員函數的作用主要是向套接字中寫入數據。
              寫入操作中主要使用了 write 系統調用。
              unistd.h中
              ssize_t write(int fd , char * buf , int count)
              若是發生寫錯誤,則返回值為-1 ,但是若此時的錯誤狀態為EINTR ,則表示是發生了中斷操作,此時應該繼續進行寫操作。
              若是當前執行的寫操作出現了等待的事情,則不需要報錯,應該繼續寫,直到等待的事情結束。
              (1) 誤區
                     write并不是立即執行寫操作,而是將數據寫入進內核緩沖區。
                     一般內核區比較穩定,不會出現問題。
                     

             (4)下面是connexion的實現代碼
                  

            // Larbin
            // Sebastien Ailleret
            // 15-11-99 -> 03-05-01

            #include 
            <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <unistd.h>
            #include 
            <string.h>
            #include 
            <sys/socket.h>
            #include 
            <netinet/in.h>
            #include 
            <netinet/tcp.h>
            #include 
            <ctype.h>
            #include 
            <netdb.h>
            #include 
            <arpa/inet.h>
            #include 
            <errno.h>
            #include 
            <iostream>

            #include 
            "options.h"
            using namespace std ;

            /*********************************/
            /* various functions for writing */

            /* make write until everything is written
             * return 0 on success, 1 otherwise
             * Don't work on non-blocking fds
             
            */

            int ecrire (int fd, char *buf) {
              
            int pos = 0 ; 
              
            int len = strlen(buf); 
              
            while(pos < len)
              
            {
                 
            int i = write(fd , buf + pos, len - pos) ; 
                 
            if(i == -1)    
                 
            {
                   
            if(errno != EINTR)
                    
            {
                      pos 
            = len + 1 ;
                    }

                 }
               
                 
            else{
                  pos 
            += i ;
                }

              }

               
            return pos != len ;
            }


            /* make write until everything is written
             * return 0 on success, 1 otherwise
             * Don't work on non-blocking fds
             
            */

            int ecrireBuff (int fd, char *buf, int count) {
              
            int pos = 0;
              
            while(pos < count)
              
            {
                 
            int i = write(fd , buf + pos , count - pos) ;
                
            if(i == -1)  
                
            {
                 
            switch(errno)
                 
            {
                   
            case EINTR :
                     
            break ;
                   
            default:
                     pos 
            = count + 1 ; 
                     perror(
            "buf error") ;
                     
            break;
                 }

                }

                
            else
                 pos 
            += i ; 
              }

              
            return pos != count;
            }




            /** Write an int on a fds
             * (uses ecrire)
             
            */

            int ecrireInt (int fd, int i) {
              
            char buf[20];
              sprintf(buf, 
            "%d", i);
              
            return ecrire(fd, buf);
            }


            int ecrireInt2 (int fd, int i) {
              
            char buf[20];
              sprintf(buf, 
            "%d%c", i/10, i%10 + '0');
              
            return ecrire(fd, buf);
            }


            int ecrireInti (int fd, int i, char *f) {
              
            char buf[100];
              sprintf(buf, f, i);
              
            return ecrire(fd, buf);
            }


            int ecrireIntl (int fd, long i, char *f) {
              
            char buf[100];
              sprintf(buf, f, i);
              
            return ecrire(fd, buf);
            }


            /** Write an int on a fds
             * (uses ecrire)
             
            */

            int ecrireLong (int fd, long i) {
              
            char buf[30];
              sprintf(buf, 
            "%ld", i);
              
            return ecrire(fd, buf);
            }


            /* Write a char on a fds
             * return 0 on success, 1 otherwise
             * Don't work on non-blocking fds
             
            */

            int ecrireChar (int fd, char c) {
              
            int pos = 0;
              
            while (pos < 1{
                
            int i = write(fd, &c, 1);
                
            if (i == -1{
                  
            if (errno != EINTR) {
                    pos 
            = 2;
                  }

                }
             else {
                  pos 
            += i;
                }

              }

              
            return pos != 1;
            }


            (5)綜上
                 Connexion主要處理的是連接相關的信息,其connexion中主要實現的是,向套接字中寫入數據。
                 下一個系列,處理的是LarbinString 相關,該類主要是處理http報頭的。

                    

            posted on 2011-06-11 16:05 kahn 閱讀(1428) 評論(0)  編輯 收藏 引用 所屬分類: larbin源碼分析

            久久久精品一区二区三区| 久久无码人妻精品一区二区三区| 久久精品桃花综合| 国产一久久香蕉国产线看观看| 久久精品国产2020| 97超级碰碰碰碰久久久久| 久久免费看黄a级毛片| 精品久久久中文字幕人妻| 精品久久亚洲中文无码| 久久精品国产亚洲一区二区三区 | 国产亚洲精午夜久久久久久| 亚洲精品无码久久久久sm| 99久久做夜夜爱天天做精品| 婷婷久久综合九色综合九七| 久久久久久久波多野结衣高潮| 国产精品热久久毛片| 久久精品无码一区二区WWW| 99久久精品费精品国产| 久久综合亚洲欧美成人| 亚洲综合久久夜AV | 国产精品一区二区久久精品无码 | 久久免费视频网站| 国产亚洲精品自在久久| 东方aⅴ免费观看久久av| 欧美激情一区二区久久久| 精品久久人人做人人爽综合| 国产精品99久久久久久猫咪 | 久久久精品午夜免费不卡| 久久精品国产99久久无毒不卡| 无码8090精品久久一区| 人妻无码精品久久亚瑟影视| 精品久久久无码21p发布| 99久久婷婷免费国产综合精品| 欧美熟妇另类久久久久久不卡| 亚洲国产精品无码久久SM| 九九久久自然熟的香蕉图片| 久久久久女教师免费一区| 香蕉久久久久久狠狠色| 久久中文骚妇内射| 怡红院日本一道日本久久| 久久有码中文字幕|