예제 #1
0
//connect remote host
int nd_tcpnode_connect(const char *host, int port, struct nd_tcp_node *node, struct nd_proxy_info *proxy)
{
	ENTER_FUNC()
		nd_assert(node);
	nd_assert(host);

	node->sys_error = 0;
	node->last_push = nd_time();
	ndlbuf_reset(&(node->recv_buffer));		/* buffer store data recv from net */
	ndlbuf_reset(&(node->send_buffer));		/* buffer store data send from net */

	if (proxy && proxy->proxy_type != ND_PROXY_NOPROXY) {
		node->fd = nd_proxy_connect(host, port, &(node->remote_addr), proxy, 0);
	}
	else {
		node->fd = nd_socket_tcp_connect(host, (short)port, &(node->remote_addr));
	}

	if (node->fd <= 0) {
		node->myerrno = NDERR_OPENFILE;
		LEAVE_FUNC();
		return -1;
	}
	TCPNODE_SET_OK(node);
	nd_socket_nonblock(node->fd, 1);
	_set_ndtcp_conn_dft_option(node->fd);
	node->start_time = nd_time();
	if (node->remote_addr.sin_family == AF_INET6) {
		node->is_ipv6 = 1;
	}
	LEAVE_FUNC();
	return 0;
}
예제 #2
0
파일: srv_listen.c 프로젝트: a1406/ndproj
/* accept incoming  connect*/
struct ne_client_map * accetp_client_connect(struct listen_contex *listen_info)
{
	NEUINT16  session_id ;
	nesocket_t newconnect_fd ;
	socklen_t cli_len ;				/* client socket lenght */
	struct sockaddr_in client_addr ;
	
	struct ne_client_map *client_map ;
	struct cm_manager *pmanger = ne_listensrv_get_cmmamager(listen_info) ;

	cli_len = sizeof (client_addr);
	newconnect_fd = accept(get_listen_fd(listen_info), (struct sockaddr*)&client_addr, &cli_len);
	if(newconnect_fd < 0)
		return 0 ;
	
	//alloc a connect node struct 	
	client_map =(struct ne_client_map*) pmanger->alloc (ne_listensrv_get_cmallocator(listen_info)) ;
	if(!client_map){
		host_congest(newconnect_fd) ;
		//ne_logerror("server congest!") ;
		return 0;
	}
	if(pmanger->init )
		pmanger->init (client_map, (ne_handle)listen_info) ;
	else 
		ne_tcpcm_init(client_map,(ne_handle)listen_info);

	if(-1== ne_socket_nonblock(newconnect_fd,1)) {
		ne_socket_close(newconnect_fd);
		pmanger->dealloc (client_map,ne_listensrv_get_cmallocator(listen_info));
		return 0 ;
	}
	_set_socket_addribute(newconnect_fd) ;

	client_map->connect_node.fd = newconnect_fd ;
//	client_map->connect_node.remote_len = cli_len ;
	memcpy(&(client_map->connect_node.remote_addr),&client_addr, sizeof(client_addr)) ;
	TCPNODE_SET_OK(&client_map->connect_node) ;
	client_map->connect_node.srv_root = (ne_handle)&(listen_info->tcp) ;

	client_map->connect_node.session_id = session_id = pmanger->accept (pmanger,client_map);
	if(0==client_map->connect_node.session_id) {
		ne_socket_close(newconnect_fd);
		pmanger->unlock (pmanger,session_id);
		pmanger->dealloc (client_map,ne_listensrv_get_cmallocator(listen_info));
		return 0;
	}

	if(listen_info->tcp.connect_in_callback){		
		if(-1==listen_info->tcp.connect_in_callback(client_map,&client_addr,(ne_handle)listen_info) ){
			ne_socket_close(newconnect_fd);
			pmanger->deaccept (pmanger,client_map->connect_node.session_id);
			
			pmanger->unlock (pmanger,session_id);
			pmanger->dealloc (client_map,ne_listensrv_get_cmallocator(listen_info));
			return 0 ;
		}
	}
	client_map->connect_node.start_time = ne_time();
	client_map->connect_node.last_recv = ne_time(); 
	INIT_LIST_HEAD(&client_map->map_list) ;
	
	pmanger->unlock (pmanger,client_map->connect_node.session_id);
	return client_map ;

}