Ejemplo n.º 1
0
Archivo: IoSocket.c Proyecto: BMeph/io
IoObject *IoSocket_asyncAccept(IoSocket *self, IoObject *locals, IoMessage *m)
{
	//doc Socket asyncAccept(addressObject) Immediately returns a socket for a connection if one is available or nil otherwise. Returns an Error object on error.

	IoObject *address = IoMessage_locals_addressArgAt_(m, locals, 0);
	Socket *socket = Socket_accept(SOCKET(self), IoSocket_rawAddressFrom_(address));

	if (socket)
	{
		IoObject *newSocket = IoSocket_newWithSocket_(IOSTATE, socket);
		newSocket = IoObject_initClone_(self, locals, m, newSocket);
		return IoSocket_rawSetupEvents(newSocket, locals, m);
	}
	else
	{
		if (Socket_asyncFailed())
		{
			return SOCKETERROR("Socket accept failed");
		}
		else
		{
			return IONIL(self);
		}
	}
}
Ejemplo n.º 2
0
/****************************************************************
Function    :   GAgent_DoTcpWebConfig
Description :   creat web config server.
return      :   NULL
Add by Alex.lin     --2015-04-25.
****************************************************************/
void GAgent_DoTcpWebConfig( pgcontext pgc )
{
    uint16 GAgentStatus=0,i=0;
    int32 newfd;
    struct sockaddr_t addr;
    int32 addrLen= sizeof(addr);
    GAgentStatus = pgc->rtinfo.GAgentStatus;

    if( (GAgentStatus&WIFI_MODE_AP)!= WIFI_MODE_AP )
    {
        if( pgc->ls.tcpWebConfigFd>0 )
        {
            close( pgc->ls.tcpWebConfigFd );
            pgc->ls.tcpWebConfigFd = INVALID_SOCKET;
        }
        return ;
    }

    if( pgc->ls.tcpWebConfigFd <= 0 )
    {
        GAgent_Printf( GAGENT_DEBUG,"Creat Tcp Web Server." );
        pgc->ls.tcpWebConfigFd = GAgent_CreateWebConfigServer( 80 );   
    }

    if(FD_ISSET(pgc->ls.tcpWebConfigFd, &(pgc->rtinfo.readfd)))
    {
        /* if nonblock, can be done in accept progress */
        newfd = Socket_accept(pgc->ls.tcpWebConfigFd, &addr, &addrLen);
        if(newfd > 0)
        {
            Lan_AddTcpNewClient(pgc, newfd, &addr);
        }
    }

    for(i = 0;i < LAN_TCPCLIENT_MAX; i++)
    {
        int32 fd=0;
        fd = pgc->ls.tcpClient[i].fd;
        if(fd <= 0)
            continue;
        if(FD_ISSET(fd, &(pgc->rtinfo.readfd)))
        {
            Lan_setClientTimeOut(pgc, i);
            handleWebConfig( pgc,fd);
            close(fd);
            fd = INVALID_SOCKET;
            pgc->ls.tcpClient[i].fd=INVALID_SOCKET;
        }
    }
}
Ejemplo n.º 3
0
int Socket_CheckNewTCPClient(void)
{
    fd_set readfds, exceptfds;
    int ret;
    int newClientfd;
    int i;
    struct sockaddr_t addr;
	struct timeval_t t;
    int addrlen= sizeof(addr);
    t.tv_sec = 0;//秒
    t.tv_usec = 0;//微秒
    /*Check status on erery sockets */
    FD_ZERO(&readfds);
    FD_SET(g_TCPServerFd, &readfds);

    ret = select((g_TCPServerFd+1), &readfds, NULL, &exceptfds, &t);

    if (ret > 0)
    {
        newClientfd = Socket_accept(g_TCPServerFd, &addr, &addrlen);
        GAgent_Printf(GAGENT_DEBUG, "detected new client as %d", newClientfd);
        if (newClientfd > 0)
        {
            if(g_TCPSocketMaxClientFd<newClientfd)
            {
                g_TCPSocketMaxClientFd=(newClientfd+1); /*Select接口要求+1*/
            }
            for(i=0;i<SOCKET_MAXTCPCLENT;i++) 
            {
                if (g_stTCPSocketClients[i].SocketFD == -1) 
                {
                    memcpy(&g_stTCPSocketClients[i].TCPClientAddr, &addr, sizeof(addr));
                    g_stTCPSocketClients[i].SocketFD = newClientfd;
                    g_stTCPSocketClients[i].TimeOut = LAN_CLIENT_MAXLIVETIME;
                    GAgent_Printf(GAGENT_INFO, "new tcp client, clientid:%d[%d] [time:%08x second: %08x MS]", 
                        newClientfd, i, DRV_GAgent_GetTime_S(), DRV_GAgent_GetTime_MS() );
                    
                    return newClientfd; //返回连接ID
                }
            }
            /*此时连接个数大于系统定义的最大个数,应该关闭释放掉对应的socket资源*/           
            close( newClientfd );
        }
        else
        {
            GAgent_Printf(GAGENT_DEBUG, "Failed to accept client");
        }
    }
    return -1;
}
Ejemplo n.º 4
0
/****************************************************************
Function    :   GAgent_DoTcpWebConfig
Description :   creat web config server.
return      :   NULL
Add by Alex.lin     --2015-04-25.
****************************************************************/
void GAgent_DoTcpWebConfig( pgcontext pgc )
{
    uint16 GAgentStatus=0;
    int32 newfd = 0;
    struct sockaddr_t addr;
    int32 addrLen= sizeof(addr);
    GAgentStatus = pgc->rtinfo.GAgentStatus;

    if( (GAgentStatus&WIFI_MODE_AP)!= WIFI_MODE_AP )
    {
        if( pgc->ls.tcpWebConfigFd>0 )
        {
            close( pgc->ls.tcpWebConfigFd );
            pgc->ls.tcpWebConfigFd = INVALID_SOCKET;
        }
        return ;
    }
    if( (GAgentStatus&WIFI_MODE_ONBOARDING)!= WIFI_MODE_ONBOARDING )
        return ;
    
    if( pgc->ls.tcpWebConfigFd <= 0 )
    {
        GAgent_Printf( GAGENT_DEBUG,"Creat Tcp Web Server." );
        pgc->ls.tcpWebConfigFd = GAgent_CreateWebConfigServer( 80 );
    }
    if( pgc->ls.tcpWebConfigFd<0 )
        return ;
    if( !FD_ISSET(pgc->ls.tcpWebConfigFd, &(pgc->rtinfo.readfd)) )
        return ;
        
    newfd = Socket_accept(pgc->ls.tcpWebConfigFd, &addr, (socklen_t *)&addrLen);
    if(newfd < 0)
        return ;
    handleWebConfig( pgc,newfd);
    close(newfd);
    newfd = INVALID_SOCKET;
}
Ejemplo n.º 5
0
static void
acceptSingleConnection(ListenState *listenState, NetDescriptor *nd) {
	Socket *sock;
	Socket *acceptResult;
	struct sockaddr_storage addr;
	socklen_t addrLen;
	NetDescriptor *newNd;

	sock = NetDescriptor_getSocket(nd);
	addrLen = sizeof (addr);
	acceptResult = Socket_accept(sock, (struct sockaddr *) &addr, &addrLen);
	if (acceptResult == Socket_noSocket) {
		switch (errno) {
			case EWOULDBLOCK:
			case ECONNABORTED:
				// Nothing serious. Keep listening.
				return;
			case EMFILE:
			case ENFILE:
			case ENOBUFS:
			case ENOMEM:
#ifdef ENOSR
			case ENOSR:
#endif
				// Serious problems, but future connections may still
				// be possible.
				log_add(log_Warning, "accept() reported '%s'",
						strerror(errno));
				return;
			default:
				// Should not happen.
				log_add(log_Fatal, "Internal error: accept() reported "
						"'%s'", strerror(errno));
				explode();
		}
	}

	(void) Socket_setReuseAddr(acceptResult);
			// Ignore errors; it's not a big deal.
	if (Socket_setNonBlocking(acceptResult) == -1) {
		int savedErrno = errno;
		log_add(log_Error, "Could not make socket non-blocking: %s.",
				strerror(errno));
		Socket_close(acceptResult);
		errno = savedErrno;
		return;
	}
	(void) Socket_setInlineOOB(acceptResult);
			// Ignore errors; it's not a big deal as the other
			// party is not not supposed to send any OOB data.
	(void) Socket_setKeepAlive(sock);
			// Ignore errors; it's not a big deal.

#ifdef DEBUG
	{
		char hostname[NI_MAXHOST];
		int gniRes;

		gniRes = getnameinfo((struct sockaddr *) &addr, addrLen,
				hostname, sizeof hostname, NULL, 0, 0);
		if (gniRes != 0) {
			log_add(log_Error, "Error while performing hostname "
					"lookup for incoming connection: %s",
					(gniRes == EAI_SYSTEM) ? strerror(errno) :
					gai_strerror(gniRes));
		} else {
			log_add(log_Debug, "Accepted incoming connection from '%s'.",
					hostname);
		}
	}
#endif
	
	newNd = NetDescriptor_new(acceptResult, NULL);
	if (newNd == NULL) {
		int savedErrno = errno;
		log_add(log_Error, "NetDescriptor_new() failed: %s.",
				strerror(errno));
		Socket_close(acceptResult);
		errno = savedErrno;
		return;
	}

	doListenConnectCallback(listenState, nd, newNd,
			(struct sockaddr *) &addr, addrLen);
	// NB: newNd is now handed over to the callback function, and should
	//     no longer be referenced from here.
}