当前位置:网站首页>How to play with the reading and writing operations of blocking sockets?
How to play with the reading and writing operations of blocking sockets?
2022-07-01 12:41:00 【Tao song remains the same】
Blocking sockets Read and write operations , I referred to redis Source code , And then I made a change , It's comfortable to use :
/* ----------------- Blocking sockets I/O with timeouts --------------------- */
/* Redis performs most of the I/O in a nonblocking way, with the exception
* of the SYNC command where the slave does it in a blocking way, and
* the MIGRATE command that must be blocking in order to be atomic from the
* point of view of the two instances (one migrating the key and one receiving
* the key). This is why need the following blocking I/O functions.
*
* All the functions take the timeout in milliseconds. */
#define SYNCIO__RESOLUTION 10 /* Resolution in milliseconds */
/* Write the specified payload to 'fd'. If writing the whole payload will be
* done within 'timeout' milliseconds the operation succeeds and 'size' is
* returned. Otherwise the operation fails, -1 is returned, and an unspecified
* partial write could be performed against the file descriptor. */
ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout) {
ssize_t nwritten, ret = size;
long long start = mstime();
long long remaining = timeout;
while(1) {
long long wait = (remaining > SYNCIO__RESOLUTION) ?
remaining : SYNCIO__RESOLUTION;
long long elapsed;
/* Optimistically try to write before checking if the file descriptor
* is actually writable. At worst we get EAGAIN. */
nwritten = write(fd,ptr,size);
if (nwritten == -1) {
if (errno != EAGAIN) return -1;
} else {
ptr += nwritten;
size -= nwritten;
}
if (size == 0) return ret;
/* Wait */
aeWait(fd,AE_WRITABLE,wait);
elapsed = mstime() - start;
if (elapsed >= timeout) {
errno = ETIMEDOUT;
return -1;
}
remaining = timeout - elapsed;
}
}
/* Read the specified amount of bytes from 'fd'. If all the bytes are read
* within 'timeout' milliseconds the operation succeed and 'size' is returned.
* Otherwise the operation fails, -1 is returned, and an unspecified amount of
* data could be read from the file descriptor. */
ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout) {
ssize_t nread, totread = 0;
long long start = mstime();
long long remaining = timeout;
if (size == 0) return 0;
while(1) {
long long wait = (remaining > SYNCIO__RESOLUTION) ?
remaining : SYNCIO__RESOLUTION;
long long elapsed;
/* Optimistically try to read before checking if the file descriptor
* is actually readable. At worst we get EAGAIN. */
nread = read(fd,ptr,size);
if (nread == 0) return -1; /* short read. */
if (nread == -1) {
if (errno != EAGAIN) return -1;
} else {
ptr += nread;
size -= nread;
totread += nread;
}
if (size == 0) return totread;
/* Wait */
aeWait(fd,AE_READABLE,wait);
elapsed = mstime() - start;
if (elapsed >= timeout) {
errno = ETIMEDOUT;
return -1;
}
remaining = timeout - elapsed;
}
}
/* Read a line making sure that every char will not require more than 'timeout'
* milliseconds to be read.
*
* On success the number of bytes read is returned, otherwise -1.
* On success the string is always correctly terminated with a 0 byte. */
ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout) {
ssize_t nread = 0;
size--;
while(size) {
char c;
if (syncRead(fd,&c,1,timeout) == -1) return -1;
if (c == '\n') {
*ptr = '\0';
if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
return nread;
} else {
*ptr++ = c;
*ptr = '\0';
nread++;
}
size--;
}
return nread;
}More conventional operation .
边栏推荐
- [brain opening] west tide and going to the world series
- [20220605] Literature Translation -- visualization in virtual reality: a systematic review
- 队列的链式存储
- BIM and safety in road maintenance-buildSmart Spain
- ustime写出了bug
- Blocking sockets的读写操作该怎么玩?
- 【邂逅Django】——(二)数据库配置
- Use Net core access wechat official account development
- How to install php7 and perform performance test using yum
- [Maui] add click events for label, image and other controls
猜你喜欢

项目部署,一点也不难!

leetcode:241. 为运算表达式设计优先级【dfs + eval】

Logstash error: cannot reload pipeline, because the existing pipeline is not reloadable

Sort out relevant contents of ansible

Chained storage of queues

Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.

leetcode:226. 翻转二叉树【dfs翻转】

基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能

VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
![[Suanli network] technological innovation of Suanli Network -- key technology of operation service](/img/80/6e3648c88d309516d4bc29db9c153c.jpg)
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
随机推荐
运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
79. Word search [DFS + backtracking visit + traversal starting point]
Eurake分区理解
R language builds a binary classification model based on H2O package: using H2O GBM build gradient hoist model GBM, use H2O AUC value of AUC calculation model
ASTM D 3801固体塑料垂直燃烧试验
R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
Queue operation---
Which securities company has a low, safe and reliable account opening commission
leetcode:226. Flip binary tree [DFS flip]
高薪程序员&面试题精讲系列118之Session共享有哪些方案?
项目部署,一点也不难!
腾讯黎巍:深耕“监管科技”,护航数字经济行稳致远
Application of stack -- bracket matching problem
BIM and safety in road maintenance-buildSmart Spain
[today in history] July 1: the father of time sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
哪个券商公司开户佣金低又安全又可靠
数论基础及其代码实现
6.30模拟赛总结