Skip to content

Commit 7b98ccb

Browse files
committed
1. Set non blocking in example/main.c.
2. Add some description of `ff_socket()` and `ff_write()`. 3. Ref #709.
1 parent 06553fe commit 7b98ccb

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

example/main.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,19 @@ int loop(void *arg)
104104
} while (available);
105105
} else if (event.filter == EVFILT_READ) {
106106
char buf[256];
107-
size_t readlen = ff_read(clientfd, buf, sizeof(buf));
108-
109-
ff_write(clientfd, html, sizeof(html) - 1);
107+
ssize_t readlen = ff_read(clientfd, buf, sizeof(buf));
108+
ssize_t writelen = ff_write(clientfd, html, sizeof(html) - 1);
109+
if (writelen < 0){
110+
printf("ff_write failed:%d, %s\n", errno,
111+
strerror(errno));
112+
ff_close(clientfd);
113+
}
110114
} else {
111115
printf("unknown event: %8.8X\n", event.flags);
112116
}
113117
}
118+
119+
return 0;
114120
}
115121

116122
int main(int argc, char * argv[])
@@ -129,6 +135,10 @@ int main(int argc, char * argv[])
129135
exit(1);
130136
}
131137

138+
/* Set non blocking */
139+
int on = 1;
140+
ff_ioctl(sockfd, FIONBIO, &on);
141+
132142
struct sockaddr_in my_addr;
133143
bzero(&my_addr, sizeof(my_addr));
134144
my_addr.sin_family = AF_INET;

lib/ff_api.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ int ff_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
6464

6565
int ff_ioctl(int fd, unsigned long request, ...);
6666

67+
/*
68+
* While get sockfd from this API, and then need set it to non-blocking mode like this,
69+
* Otherwise, sometimes the socket interface will not work properly, such as `ff_write()`
70+
*
71+
* int on = 1;
72+
* ff_ioctl(sockfd, FIONBIO, &on);
73+
*
74+
* See also `example/main.c`
75+
*/
6776
int ff_socket(int domain, int type, int protocol);
6877

6978
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,
8796
ssize_t ff_read(int d, void *buf, size_t nbytes);
8897
ssize_t ff_readv(int fd, const struct iovec *iov, int iovcnt);
8998

99+
100+
/*
101+
* Write data to the socket sendspace buf.
102+
*
103+
* Note:
104+
* The `fd` parameter need set non-blocking mode in advance if F-Stack's APP.
105+
* Otherwise if the `nbytes` parameter is greater than
106+
* `net.inet.tcp.sendspace + net.inet.tcp.sendbuf_inc`,
107+
* the API will return -1, but not the length that has been sent.
108+
*
109+
* You also can modify the value of `net.inet.tcp.sendspace`(default 16384 bytes)
110+
* and `net.inet.tcp.sendbuf_inc`(default 16384 bytes) with `config.ini`.
111+
* But it should be noted that not all parameters can take effect, such as 32768 and 32768.
112+
* `ff_sysctl` can see there values while APP is running.
113+
*/
90114
ssize_t ff_write(int fd, const void *buf, size_t nbytes);
91115
ssize_t ff_writev(int fd, const struct iovec *iov, int iovcnt);
92116

0 commit comments

Comments
 (0)