- 论坛徽章:
- 15
|
本帖最后由 yulihua49 于 2014-12-23 16:16 编辑
yulihua49 发表于 2014-12-22 14:32 ![]()
任务调度器:
AIO_tcp.c:- /*@(#) SDBC 7.1 TCP SERVER Tools *
- * to suport Fiberized.IO
- **********************************************/
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <scsrv.h>
- #ifndef MIN
- #define MIN(a,b) ((a)<(b))?(a):(b)
- #endif
- //timeout for second
- int RecvNet(int socket,char *buf,int n,int timeout,int TCB_no)
- {
- int bcount=0,br,ret;
- int i;
- int fflag;
- if(socket<0) return SYSERR;
- if(!buf && n<0) return 0;
- fflag=fcntl(socket,F_GETFL,0);
- if(TCB_no>=0) {
- fcntl(socket,F_SETFL,fflag|O_NONBLOCK); //异步操作
- } else {
- struct timeval tmout;
- tmout.tv_sec=timeout;
- tmout.tv_usec=0;
- ret=setsockopt(socket,SOL_SOCKET,SO_RCVTIMEO,(char *)&tmout,sizeof(tmout));
- if(ret) {
- ShowLog(5,"%s:setsockopt err=%d,%s",__FUNCTION__,
- errno,strerror(errno));
- }
- }
- *buf=0;
- br=0;
- while(bcount<n){
- if((br=read(socket,buf,n-bcount))>0){
- bcount+=br;
- buf+=br;
- continue;
- }
- if(errno==EAGAIN) return TIMEOUTERR;
- if(br<0){
- if(errno!=ECONNRESET)
- ShowLog(1,"%s:br=%d,err=%d,%s",__FUNCTION__,br,errno,strerror(errno));
- break;
- }
- //ShowLog(5,"RecvNet:read br=0,errno=%d,%s",errno,strerror(errno));
- if(bcount < n && TCB_no>=0) { //切换任务
- i=do_event(TCB_no,socket,0);//yield by EPOOLIN
- if(i<0) {
- struct timeval tmout;
- tmout.tv_sec=timeout;
- tmout.tv_usec=0;
- ret=setsockopt(socket,SOL_SOCKET,SO_RCVTIMEO,(char *)&tmout,sizeof(tmout));
- fcntl(socket,F_SETFL,fflag);
- TCB_no=-1;
- }
- }
- }
- if(TCB_no >= 0) fcntl(socket,F_SETFL,fflag);
- return bcount==0?-1:bcount;
- }
- int SendNet(int socket,char *buf,int n,int MTU,int TCB_no)
- {
- int bcount,bw;
- int sz,i=0;
- int fflag;
- size_t SendSize;
- if(socket<0) return SYSERR;
- fflag=fcntl(socket,F_GETFL,0);
- if(TCB_no >= 0) {
- fcntl(socket,F_SETFL,fflag|O_NONBLOCK); //异步操作
- }
- bcount=0;
- bw=0;
- if(MTU>500) SendSize=MTU;
- else SendSize=n;
- while(bcount<n){
- sz=MIN(n-bcount,SendSize);
- if((bw=write(socket,buf,sz))>0){
- bcount+=bw;
- buf+=bw;
- }
- if(bw<0) break;
- if(bcount < n && TCB_no >= 0) { //切换任务
- i=do_event(TCB_no,socket,1); //yield by EPOLLOUT
- if(i<0) {
- fcntl(socket,F_SETFL,fflag);
- TCB_no=-1;
- }
- }
- }
- if(TCB_no >= 0) {
- fcntl(socket,F_SETFL,fflag);
- }
- return bcount==0?-1:bcount;
- }
复制代码 比先前的简单了许多。 |
|