使用epoll php,linux - epoll的EPOLLOUT事件

天蓬老师2017-04-17 13:02:273楼

/*

* Packs up the buf and write to socket in non-blocking way

* If all data is writen, return 1

* If only part of data is writen, return 0, to be continue next time

* If error occurs, return -1

*

* Note, if j_socket_write() returns 0, then you can call it with NULL in buf next time, until all data is writen

*/

int j_socket_write(JSocket * jsock, const void *buf, guint32 count)

{

guint32 size = j_socket_wdata_length(jsock);

if (size == 0) {

/* new data to write */

if (buf == NULL) {

return 1; /* no data? must be a mistake */

}

gchar *len = pack_length4(count);

j_socket_wdata_append(jsock, len, 4);

j_socket_wdata_append(jsock, buf, count);

g_free(len);

size = j_socket_wdata_length(jsock);

}

gint n;

count = size>4096?4096:size;

while(size>0){ /* writes segmentation */

n = j_socket_write_raw(jsock, j_socket_wdata(jsock), count);

if(n<0){

if(errno==EAGAIN){

return 0;

}

return -1; /* It's a real error */

}

j_socket_wdata_pop(jsock,n);

size = j_socket_wdata_length(jsock);

count = size>4096?4096:size;

}

return 1;

}

@我是宅男小何 刚好提醒了我,其实就算整块数据很大,也不一定要调用一次write就全部写入。

上面代码,将超过4096的数据分片发送,如果阻塞,则返回等待下一次调用。

这段代码还没有测试过,基本这个逻辑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值