📄 httcp.c
字号:
break; } if (HTHost_retry(me)) { me->connecttime = HTGetTimeInMillis() - me->connecttime; /* Added EINVAL `invalid argument' as this is what I get back from a non-blocking connect where I should get `connection refused' on BSD. SVR4 gives SIG_PIPE */ if (HT_HOSTUNREACHABLE(socerrno)) me->connecttime += TCP_DELAY; else me->connecttime += TCP_PENALTY; HTDNS_updateWeigths(me->dns, HTHost_home(me), me->connecttime); } me->tcpstate = TCP_ERROR; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_ERROR.\n" _ me); } else { me->tcpstate = TCP_CONNECTED; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_CONNECTED.\n" _ me); } break; case TCP_CONNECTED: HTHost_unregister(me, net, HTEvent_CONNECT); if (HTHost_retry(me)) { me->connecttime = HTGetTimeInMillis() - me->connecttime; HTDNS_updateWeigths(me->dns, HTHost_home(me), me->connecttime); } HTHost_setRetry(me, 0); me->tcpstate = TCP_IN_USE; HTTRACE(PROT_TRACE, "HTHost %p connected.\n" _ me); return HT_OK; break; /* once a host is connected, subsequent connections are immediately OK */ case TCP_IN_USE: if ((status = HTHost_addNet(net->host, net)) == HT_PENDING) { HTTRACE(PROT_TRACE, "HTDoConnect. Pending...\n"); return HT_PENDING; } HTChannel_upSemaphore(me->channel); return HT_OK; case TCP_DNS_ERROR: HTHost_setRetry(me, 0); me->tcpstate = TCP_BEGIN; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_BEGIN.\n" _ me); return HT_NO_HOST; break; case TCP_NEED_BIND: case TCP_NEED_LISTEN: case TCP_ERROR: if (HTChannel_socket(me->channel) != INVSOC) { NETCLOSE(HTChannel_socket(me->channel));#if 1 /* @@@ */ if (HTHost_isPersistent(me)) { /* Inherited socket */ HTHost_setPersistent(me, NO, HT_TP_SINGLE); me->tcpstate = TCP_NEED_SOCKET; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_NEED_SOCKET.\n" _ me); break; }#endif } /* Do we have more homes to try? */ HTHost_decreaseRetry(me); if (HT_HOSTUNREACHABLE(socerrno) && HTHost_retry(me) > 0) { HTRequest_addSystemError(request, ERR_NON_FATAL, socerrno, NO,"connect"); me->tcpstate = TCP_DNS; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_DNS.\n" _ me); break; } HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO, "connect"); HTDNS_delete(hostname); HTHost_setRetry(me, 0); me->tcpstate = TCP_BEGIN; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_BEGIN.\n" _ me); return HT_ERROR; break; } }}/* HTDoAccept()** ------------** This function makes a non-blocking accept which will turn up as ready** read in the select.** Returns** HT_ERROR Error has occured or interrupted** HT_OK if connected** HT_WOULD_BLOCK if operation would have blocked*/PUBLIC int HTDoAccept (HTNet * net, HTNet ** accepted){ HTHost * me = HTNet_host(net); int status; int size = sizeof(net->host->sock_addr); HTRequest * request = HTNet_request(net); if (!request || HTNet_socket(net)==INVSOC) { HTTRACE(PROT_TRACE, "HTDoAccept.. Invalid socket\n"); return HT_ERROR; } /* Progress report */ { HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT); if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL,NULL, NULL, NULL); } status = accept(HTNet_socket(net), (struct sockaddr *) &net->host->sock_addr, &size); if (NETCALL_ERROR(status)) { if (NETCALL_WOULDBLOCK(socerrno)) { HTTRACE(PROT_TRACE, "HTDoAccept.. WOULD BLOCK %d\n" _ HTNet_socket(net)); HTHost_register(me, net, HTEvent_ACCEPT); return HT_WOULD_BLOCK; } HTRequest_addSystemError(request, ERR_WARN, socerrno, YES, "accept"); HTTRACE(PROT_TRACE, "HTDoAccept.. Accept failed\n"); return HT_ERROR; } HTTRACE(PROT_TRACE, "Accepted.... socket %d\n" _ status); /* ** If accepted is the same as the net obejct then reuse it, else create ** a new object and leave the original alone */ if (*accepted == net) HTDoClose(net); else *accepted = HTNet_dup(net); HTNet_setSocket(*accepted, status); return HT_OK;}/* HTDoListen** ----------** Listens on the specified port. 0 means that we chose it here** If master==INVSOC then we listen on all local interfaces (using a ** wildcard). If !INVSOC then use this as the local interface** returns HT_ERROR Error has occured or interrupted** HT_OK if connected*/PUBLIC int HTDoListen (HTNet * net, u_short port, SOCKET master, int backlog){ HTHost * me = HTNet_host(net); HTRequest * request = HTNet_request(net); int preemptive = net->preemptive; int status = HT_OK; char * hostname = HTHost_name(me); /* Jump into the state machine */ while (1) { switch (me->tcpstate) { case TCP_BEGIN: { /* ** Add the net object to the host object found above. If the ** host is idle then we can start the request right away, ** otherwise we must wait until it is free. */ if ((status = HTHost_addNet(net->host, net)) == HT_PENDING) HTTRACE(PROT_TRACE, "HTDoListen.. Pending...\n"); /* ** If we are pending then return here, otherwise go to next state ** which is setting up a channel */ me->tcpstate = TCP_CHANNEL; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_CHANNEL.\n" _ me); if (status == HT_PENDING) return HT_PENDING; } break; case TCP_CHANNEL: /* ** The next state depends on whether we have a connection or not. */ if (HTHost_channel(me) == NULL) { me->tcpstate = TCP_NEED_SOCKET; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_SOCKET.\n" _ me); } else { /* ** There is now one more using the channel */ HTChannel_upSemaphore(me->channel); /* ** We are now all set and can jump to connected mode */ me->tcpstate = TCP_CONNECTED; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_CONNECTED.\n" _ me); } hostname = HTHost_name(me); break; case TCP_NEED_SOCKET: if (_makeSocket(me, request, preemptive, net->transport) == -1) { me->tcpstate = TCP_ERROR; break; } /* Progress */ { HTAlertCallback *cbf = HTAlert_find(HT_PROG_ACCEPT); if (cbf) (*cbf)(request, HT_PROG_ACCEPT, HT_MSG_NULL, NULL, hostname, NULL); } me->tcpstate = TCP_NEED_BIND; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_NEED_BIND\n" _ me); break; case TCP_NEED_BIND: HTTRACE(PROT_TRACE, "Socket...... Binding socket %d\n" _ HTNet_socket(net)); status = bind(HTNet_socket(net), (struct sockaddr *) &me->sock_addr, sizeof(me->sock_addr)); if (NETCALL_ERROR(status)) { HTTRACE(PROT_TRACE, "Socket...... Bind failed %d\n" _ socerrno); me->tcpstate = TCP_ERROR; } else { HTTRACE(PROT_TRACE, "Socket...... Starting listening on socket %d\n" _ HTNet_socket(net)); me->tcpstate = TCP_NEED_LISTEN; } break; case TCP_NEED_LISTEN: status = listen(HTNet_socket(net), backlog); if (NETCALL_ERROR(status)) me->tcpstate = TCP_ERROR; else me->tcpstate = TCP_CONNECTED; break; case TCP_CONNECTED: HTHost_unregister(me, net, HTEvent_ACCEPT); HTHost_setRetry(me, 0); me->tcpstate = TCP_IN_USE; HTTRACE(PROT_TRACE, "HTHost %p listening.\n" _ me); return HT_OK; break; /* once a host is connected, subsequent connections are immediately OK */ case TCP_IN_USE: if ((status = HTHost_addNet(net->host, net)) == HT_PENDING) { HTTRACE(PROT_TRACE, "HTDoListen.. Pending...\n"); return HT_PENDING; } HTChannel_upSemaphore(me->channel); return HT_OK; case TCP_NEED_CONNECT: case TCP_DNS: case TCP_DNS_ERROR: case TCP_ERROR: if (HTChannel_socket(me->channel) != INVSOC) { NETCLOSE(HTChannel_socket(me->channel));#if 1 /* @@@ */ if (HTHost_isPersistent(me)) { /* Inherited socket */ HTHost_setPersistent(me, NO, HT_TP_SINGLE); me->tcpstate = TCP_NEED_SOCKET; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_NEED_SOCKET.\n" _ me); break; }#endif } HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO, "accept"); HTHost_setRetry(me, 0); me->tcpstate = TCP_BEGIN; HTTRACE(PROT_TRACE, "HTHost %p going to state TCP_BEGIN.\n" _ me); return HT_ERROR; break; } }}/* HTDoClose** ---------** Closes a file descriptor whatever means are available on the current** platform. If we have unix file descriptors then use this otherwise use** the ANSI C file descriptors**** returns HT_ERROR Error has occured or interrupted** HT_OK if connected** HT_WOULD_BLOCK if operation would have blocked*/PUBLIC int HTDoClose (HTNet * net){ int status = -1; if (net && HTNet_socket(net) != INVSOC) { HTTRACE(PROT_TRACE, "HTDoClose... Close %d\n" _ HTNet_socket(net)); status = NETCLOSE(HTNet_socket(net)); /* HTEvent_unregister(HTNet_socket(net), (SockOps) FD_ALL); */ HTNet_decreaseSocket(); HTNet_setSocket(net, INVSOC); /* ** As we have a socket available we check for whether ** we can start any pending requests. We do this by asking for ** pending Host objects. If none then use the current object */ HTHost_launchPending(net->host); } else HTTRACE(PROT_TRACE, "HTDoClose... No pending requests\n"); return status < 0 ? HT_ERROR : HT_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -