1. Set non blocking in `example/main.c`.

2. Add some description of `ff_socket()` and `ff_write()`.
3. Ref #709.
This commit is contained in:
fengbojiang 2023-02-17 19:10:41 +08:00
parent 06553fe07a
commit 7b98ccb479
2 changed files with 37 additions and 3 deletions

View File

@ -104,13 +104,19 @@ int loop(void *arg)
} while (available);
} else if (event.filter == EVFILT_READ) {
char buf[256];
size_t readlen = ff_read(clientfd, buf, sizeof(buf));
ff_write(clientfd, html, sizeof(html) - 1);
ssize_t readlen = ff_read(clientfd, buf, sizeof(buf));
ssize_t writelen = ff_write(clientfd, html, sizeof(html) - 1);
if (writelen < 0){
printf("ff_write failed:%d, %s\n", errno,
strerror(errno));
ff_close(clientfd);
}
} else {
printf("unknown event: %8.8X\n", event.flags);
}
}
return 0;
}
int main(int argc, char * argv[])
@ -129,6 +135,10 @@ int main(int argc, char * argv[])
exit(1);
}
/* Set non blocking */
int on = 1;
ff_ioctl(sockfd, FIONBIO, &on);
struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;

View File

@ -64,6 +64,15 @@ int ff_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
int ff_ioctl(int fd, unsigned long request, ...);
/*
* While get sockfd from this API, and then need set it to non-blocking mode like this,
* Otherwise, sometimes the socket interface will not work properly, such as `ff_write()`
*
* int on = 1;
* ff_ioctl(sockfd, FIONBIO, &on);
*
* See also `example/main.c`
*/
int ff_socket(int domain, int type, int protocol);
int ff_setsockopt(int s, int level, int optname, const void *optval,
@ -87,6 +96,21 @@ int ff_getsockname(int s, struct linux_sockaddr *name,
ssize_t ff_read(int d, void *buf, size_t nbytes);
ssize_t ff_readv(int fd, const struct iovec *iov, int iovcnt);
/*
* Write data to the socket sendspace buf.
*
* Note:
* The `fd` parameter need set non-blocking mode in advance if F-Stack's APP.
* Otherwise if the `nbytes` parameter is greater than
* `net.inet.tcp.sendspace + net.inet.tcp.sendbuf_inc`,
* the API will return -1, but not the length that has been sent.
*
* You also can modify the value of `net.inet.tcp.sendspace`(default 16384 bytes)
* and `net.inet.tcp.sendbuf_inc`(default 16384 bytes) with `config.ini`.
* But it should be noted that not all parameters can take effect, such as 32768 and 32768.
* `ff_sysctl` can see there values while APP is running.
*/
ssize_t ff_write(int fd, const void *buf, size_t nbytes);
ssize_t ff_writev(int fd, const struct iovec *iov, int iovcnt);