Esempio n. 1
0
File: net.c Progetto: jxva/miaoox
int netTcpConnect(char *err, char *addr, int port){
    int so;
    struct sockaddr_in sa;

    if ((so = netCreateSocket(err, AF_INET)) == -1)
        return -1;

    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    if (inet_aton(addr, &sa.sin_addr) == 0) {
        struct hostent *he;
        he = gethostbyname(addr);
        if (he == NULL) {
            netPrintError(err, "can't resolve: %s", addr);
            close(so);
            return -1;
        }
        memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
    }
    if (netSetNonBlock(err, so) == -1) {
        return -1;
    }
    if (connect(so, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
        if (errno = EINPROGRESS)
            return so;
        netPrintError(err, "connect: %s", strerror(errno));
        close(so);
        return -1;
    }
    return so;
}
Esempio n. 2
0
void ExecutorInit() {
	g_executor.clients = Map_Init(
									Map_DefaultIntCmpFunc,
									Map_DefaultIntDupFunc,
									Map_DefaultFreeFunc,
									_client_dup_func,
									Map_DefaultFreeFunc
									);
	g_executor.cur_recv_buffer_ = 0;
	g_executor.conf_lock_ = WRLock_Init();
	for(int i = 0; i != CLIENT_RECV_QUEUES; ++i) {
		g_executor.recv_buffer_[i] = Queue_Init(sizeof(Message_t));
		sem_init(&g_executor.recv_buffer_num_[i] , 0 , 0);
	}
	g_executor.cur_send_buffer_ = 0;
	for(int i = 0; i != CLIENT_SEND_QUEUES; ++i) {
		g_executor.send_buffer_[i] = Queue_Init(sizeof(Message_t));
		sem_init(&g_executor.send_buffer_num_[i] , 0, 0);
	}
	g_executor.evloop = Event_CreateLoop(MAX_CLIENTS);
	if (g_executor.evloop == NULL) {
		fprintf(stderr, "[ERROR]Create server loop error.\n");
		exit(2);
	}
	g_executor.core_fd = netListenAndBind(NULL, g_executor.port_);
	if (g_executor.core_fd == ERR_NETWORK_EXCEPTION) {
		fprintf(stderr , "[ERROR]Create server error. Server quit. Specific binding port is [%d]\n" , g_executor.port_);
		exit(2);
	}
	if (netSetNonBlock(NULL, g_executor.core_fd) != NET_OK) {
		fprintf(stderr , "[ERROR]Set server main fd to non block failed\n");
		exit(2);
	}
	if ( EV_ERR == Event_CreateFileEvent(g_executor.evloop , g_executor.core_fd , EV_READABLE , _request_accept_func , NULL ) ) {
		fprintf(stderr, "[ERROR]Create server main event failed\n");
		exit(2);
	}
	InitPBAllocator();
	_executor_start_printscreen();
	Log(LOG_FORCE,"Executor Running at 0.0.0.0:%d",g_executor.port_);
	Log(LOG_FORCE,"Logs will save to \"%s\"" , g_executor.log_path_);
	Log(LOG_FORCE,"Task result will save to \"%s:%d\"" , g_executor.store_server_addr_ , g_executor.store_server_port_);
	Log(LOG_FORCE,"The CallBack URL for a finish task is [%s]" , g_executor.save_result_to);
	if (InitAndStartTaskMonitor(DefaultFuncOnTaskFinish)) {
		exit(2);
	}
	Log(LOG_FORCE,"Task Executor Start...");
}
Esempio n. 3
0
static void _create_new_client(int fd, const char* ip, int port) {
	if (fd <= 0)
		return;
	netSetNonBlock(NULL, fd);
	netEnableTcpNoDelay(NULL, fd);
	Client_t client;
	client.fd = fd;
	snprintf(client.addr , IP_ADDR_LEN , "%s" , ip);
	client.port = port;
	client.last_seen = time(NULL);
	client.status = CLI_INIT;
	client.buffer_len = 0;
	client.oplock = StdMutex_Init();

	Client_t* stored_client = Map_SetAndGetPtr(g_executor.clients , &fd , &client);
	if (Event_CreateFileEvent(g_executor.evloop , fd , EV_READABLE , _read_query_from_client , stored_client) != EV_OK) {
		Log(LOG_ERROR,"Create new client failed.[%s:%d]" , stored_client->addr ,stored_client->port);
		close(fd);
		Map_Del(g_executor.clients , &fd);
		return;
	}
}