没有合适的资源?快使用搜索试试~ 我知道了~
首页larbin源码分析全解
资源详情
资源评论
资源推荐
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 的实现代码
#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)
剩余27页未读,继续阅读
serena_charle
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论5