Пример #1
0
static int
discover_services(char *server)
{
    char buf[BUFSIZE];
    struct sockaddr_in addr;
    ssize_t rcount;
    socklen_t addr_len = sizeof(addr);
    int sock = create_sock(NULL, 5555);

    mcast_connect(sock, 51234);
    rcount = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*) &addr,
                      &addr_len);

    printf("recvd >> %s\n", buf);

    double diff = get_tcp_bw(&addr, 10);

    if (diff > 10) {
        inet_ntop(AF_INET, &(addr.sin_addr), server, 50);
        printf("server %s\n", server);
        printf("BW is greated than 10 MB, running over RPC\n");
        return 0;
    }
    else { 
        printf("BW is less than 10 MB, running locally\n");
        return -1;
    }
}
Пример #2
0
ne_socket *ne_sock_connect(const ne_inet_addr *addr, unsigned int portnum)
{
    int fd;
    int len, val;

#ifdef USE_GETADDRINFO
    /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
     * implementations do not set ai_socktype, e.g. RHL6.2. */
    fd = socket(addr->ai_family, SOCK_STREAM, addr->ai_protocol);
#else
    fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
    if (fd < 0)
	return NULL;

    val = 1;
   if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val)) < 0){
       	perror("setsockopt() :");
	return -1;
   }


    if (raw_connect(fd, addr, ntohs(portnum))) {
	ne_close(fd);
	return NULL;
    }

    return create_sock(fd);
}
Пример #3
0
ne_socket *ne_sock_accept(int listener) 
{
    int fd = accept(listener, NULL, NULL);
    if (fd > 0)
	return create_sock(fd);
    return NULL;
}
Пример #4
0
int main(int argc, char **argv)
{
	int sock;
	int new_sock;
	pid_t pid = 0;
	int opt = 1;

	parse_options(argc, argv);

	sock = create_sock();

	while (1) {
		new_sock = accept_request(sock);
		if (new_sock < 1) {
			break;
		}

		if ((setsockopt(new_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) < 0) {
			perror("setsockopt");
		}

		pid = client_fork();
		if (pid == 0) {
			printf("Client spawned\n");
			manage_client(new_sock, "A_PASS_HERE", "ENC_TYPE_HERE");
			close(new_sock);
		}
	}

	return 0;
}
Пример #5
0
static int lua_connect_to(lua_State* state)
{
	int workerIdx = 0;
	while(state != g_workers[workerIdx].state)
	{
		workerIdx++;
	}

	const char *ip = lua_tostring(state, 1);
	unsigned short port = lua_tointeger(state, 2);
	
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    
    //sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;

    struct hostent *host;
	if(host = gethostbyname(ip))
	{
        memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);
	}
    else
	{
		addr.sin_addr.s_addr = inet_addr(ip);
	}

    //addr.sin_addr.s_addr = inet_addr(host);
    addr.sin_port = htons(port);

	
    Sock *sock = create_sock(sockfd, g_epollfds[workerIdx], workerIdx);
    g_sock_fd_map[sockfd] = sock;
    
    int flags = fcntl(sockfd, F_GETFL, 0);
	fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

    if(connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == 0)
    {
    }
    else
    {
    }
    
    heart_beat_sock(sock, time(0));

    lua_pushinteger(state, sockfd);
    //printf("workerIdx: %d \n", workerIdx);
    add_connector_to_worker(&g_workers[workerIdx], sock);
    
    //lua_pop(state, 1);

    return 1;
}
/*------------------------------------------------------------------*/
void
traitement_serveur ()
{
    //Socket file descriptor.
    int sockfd;
    //sockaddr serveur
    //struct sockaddr_in sa;
    char hostname[26];
    //On récupère le hostname
    gethostname (hostname, 25);

    //Socket client file descriptor
    int clientfd=0;
    struct sockaddr_in addr_client;

    //Pour récupérer les opérations
    prot_requete_t rqt_client;
    prot_reponse_t rep_client;
    prot_ret_e rqt_status;
    //Pour le fork()
    int pid_fils=getpid();
    //Création de la socket serveur
    sockfd=create_sock(prot_params, hostname);
    //Si tcp, On passe en mode écoute
    serveur_ecoute(prot_params, sockfd, 10);
    while (1)
    {
        if(prot_params.type==sock_tcp)
        {
            clientfd = h_accept (sockfd, &addr_client);

            pid_fils = fork ();
        } else {
            printf("attente udp\n");
            h_recvfrom(sockfd,(char * ) & rqt_client, sizeof(prot_requete_t), &addr_client);
            printf("RECU !!\n");
        }
        if (pid_fils == 0)
        {
            while((serveur_lire(prot_params, clientfd, &rqt_client)) >0)
            {
                affiche_requete_informations (rqt_client);
                rqt_status = traite_requete (rqt_client, &rep_client);
                affiche_requete_status (rqt_status);
                serveur_ecrire(prot_params, clientfd, &rep_client);
            }
            serveur_fermer_client(clientfd);
            return exit (EXIT_SUCCESS);
        } else {
            affiche_requete_informations(rqt_client);
            rqt_status= traite_requete(rqt_client, &rep_client);
            affiche_requete_status(rqt_status);
            serveur_ecrire_udp(prot_params, sockfd, &rep_client, &addr_client);
        }
    }

}
Пример #7
0
Файл: socket.c Проект: lb1a/avfs
nsocket *sock_accept(int listener) 
{
    int fd = accept(listener, NULL, NULL);
    if (fd > 0) {
	return create_sock(fd);
    } else {
	return NULL;
    }
}
Пример #8
0
static
bool
on_startup (void)
{
	bool status = (create_sock() && create_nak_thread());
	if (status)
		puts ("Startup complete.");
	return status;
}
Пример #9
0
int setup_mjpeg_tcp_server()
{
	/* create a server socket */
	if (create_sock(SERVER_PORT))
		return 1;

	/* spwan a thread to accept connections */
	pthread_create(&handler_tid, NULL, handler, NULL);

        return 0;
}
Пример #10
0
int main( int argc, char *argv[])
{
#define W 40
#define H 40
#define X 100
#define Y 100
#define ADDR "127.0.0.1"
#define PORT 12345
	pixel_t *dest = NULL;
	int size;
	int x = X;
	int y = Y;
	int w = W;
	int h = H;
	SOCKET sock;
	int port = PORT;
	char *addr = ADDR;
	int arg = 1;

	if (argc > arg)
	{
		addr = argv[arg++];
	}
	sock = create_sock( port, addr);
	size = sizeof(*dest) * w * h;
	dest = malloc( sizeof( *dest) * w * h);
	int frame = 0;
	while (1)
	{
		get_bits( x, y, w, h, dest);
#if 0
		int i, j;
		printf( "about to send frame %d, dims=%dx%d :\n", frame, w, h);
		for (j = 0; j < h; j++)
		{
			for (i = 0; i < w; i++)
			{
				printf( " %02X:%02X:%02X", dest[j * w + i].r, dest[j * w + i].g, dest[j * w + i].b);
			}
			printf( "\n");
		}
#endif
		send_bits( sock, dest, size, w, h, frame++);
#ifdef _WIN32
		Sleep( 900);
#else
		usleep( 900000);
#endif

	}

	return 0;
}
int main(int argc, char **argv) {
    char * fname = NULL;
    int c;

    pid1 = pid2 = -1;
    sock = -1;

    while ((c = getopt(argc, argv, "hus:f:")) != -1) {
        switch (c) {
            case 'f':
                fname = optarg;
                break;
            case 's':
                sock = atoi(optarg);
                if (sock <= 2 || sock > 1024)
                    usage("bad descriptor number for sock");
                break;
            case 'h':
            case 'u':
                usage(NULL);
            default:
                usage("unknown argument");
        }
    }

    if (argc == 1)
        usage(NULL);

    if (optind < argc && fname)
        usage("can't load shellcode both from argument and file");
    
    if (!(optind < argc) && !fname)
        usage("please provide shellcode via either argument or file");

    if (optind < argc) {
        copy_from_argument(argv[optind]);
    }
    else {
        load_from_file(fname);
    }

    //create socket if needed
    if (sock != -1) {
        int created_sock = create_sock(sock);
        printf("Created socket %d\n", created_sock);
    }

    run_shellcode(buf);
    return 100;
}
Пример #12
0
void servidor_inic () {

	int i;
	int seq, socket_servidor;
	char dir[63];
	char *t, *m, *str;
	msg pacote, r;

	system ("clear");

	t = malloc (40 * sizeof (char));

	seq = 0;

	socket_servidor = create_sock (SOCKET_SERVER_DEVICE);

	recv (socket_servidor, t, 40 * sizeof (char), 0);
	printf ("%s\n\n" , t); 


	m = malloc (40 * sizeof (char));
	m = "Conexão estabelecida com o servidor";

	send (socket_servidor, m, 40 * sizeof (char), 0);
	
	getcwd (dir, 63);
	send (socket_servidor, dir, 63, 0);

	while (1) {

		pacote = recebe_pacote (socket_servidor);

		if (pacote == NULL) {
			printf ("Pacote não foi recebido!\n");
			r = cria_msg (0, 0, tipo_nack, NULL);
			envia_pacote (socket_servidor, r);
		}

		if (pacote->tipo == tipo_cd)
			cd_cmd_servidor (socket_servidor, pacote);
		if (pacote->tipo == tipo_ls)
			ls_cmd_servidor (socket_servidor, pacote);
		if (pacote->tipo == tipo_put)
			put_cmd_servidor (socket_servidor, pacote);
		if (pacote->tipo == tipo_get)
			get_cmd_servidor (socket_servidor, pacote);
		
  	}
}
Пример #13
0
int
main (
	int	argc,
	char   *argv[]
	)
{
	pgm_error_t* pgm_err = NULL;

	setlocale (LC_ALL, "");

	if (!pgm_init (&pgm_err)) {
		fprintf (stderr, "Unable to start PGM engine: %s\n", pgm_err->message);
		pgm_error_free (pgm_err);
		return EXIT_FAILURE;
	}

	pgm_sock_t*	sock = NULL;
	const char*	network = argv[1];
	int udp_encap_port = 7510;
	int xxx = 0;
	char buf[4196];
	memset(buf, 0, 4196);

	sock = create_sock(network, udp_encap_port);
	int p_status = pthread_create (&nak_thread, NULL, &nak_routine, sock);
	if ((sock != NULL) && (p_status == 0))
	{
	    do {
		sprintf(buf, "%d", xxx);
		const int status = pgm_send (sock, buf, 4196, NULL);
		if (PGM_IO_STATUS_NORMAL != status) {
		    fprintf (stderr, "pgm_send() failed.\n");
		}
		xxx ++;
		usleep(10*1000);
	    } while(1);
	}

/* cleanup */
	if (sock) {
		pgm_close (sock, TRUE);
		sock = NULL;
	}
	pgm_shutdown();
	return EXIT_SUCCESS;
}
Пример #14
0
/*------------------------------------------------------------------------------------------------------------------
  -- FUNCTION: connectToServer
  --
  -- DATE: March 20, 2011
  --
  -- REVISIONS: (Date and Description)
  --
  -- DESIGNER: Duncan Donaldson.
  --
  -- PROGRAMMER: Duncan Donaldson.
  --
  -- INTERFACE: void MainWindow::connectToServer(QString& servaddr)
  --                        - servaddr -- the address of the server to connect to.
  --
  -- RETURNS: void.
  --
  -- NOTES:
  -- connects to the server and starts the listen thread.
  ----------------------------------------------------------------------------------------------------------------------*/
void MainWindow::connectToServer(QString& servaddr) {
    if(sock_ != 0) {
        return;
    }

    if(log_ == true) {
        logfd_ = open_log_file();
    }

    sock_ = create_sock();
    sock_ = connect_client_sock(sock_, (char*)servaddr.toAscii().constData());

    if(pthread_create(&tid_, NULL, readThread, this) != 0 ) {
	    serv_err(THREAD_ERR, (char*)"pthread_create");
    }

}
Пример #15
0
static int
discover_services(char *server)
{
    char buf[BUFSIZE];
    struct sockaddr_in addr;
    ssize_t rcount;
    socklen_t addr_len = sizeof(addr);
    int sock = create_sock(NULL, 5555);

    mcast_connect(sock, 51234);
    rcount = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*) &addr,
                      &addr_len);
	printf("rcount: %s\n", buf);


    double diff = get_tcp_bw(&addr, 10);
	
    tcp_connect(&addr, 0);
}
Пример #16
0
Файл: socket.c Проект: lb1a/avfs
/* Opens a socket to the given port at the given address.
 * Returns -1 on failure, or the socket on success. 
 * portnum must be in HOST byte order */
nsocket *sock_connect_u(const struct in_addr addr, 
			unsigned short int portnum, 
			int call_fe) 
{
    struct sockaddr_in sa;
    int fd;

    /* Create the socket */
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0)
	return NULL;
    /* Connect the nsocket */
    sa.sin_family = AF_INET;
    sa.sin_port = htons(portnum); /* host -> net byte orders */
    sa.sin_addr = addr;
    if (call_fe && notify_cb) (*notify_cb)(notify_ud, sock_connecting, NULL);
    if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
	(void) NEON_CLOSE(fd);
	return NULL;
    }
    if (call_fe && notify_cb) (*notify_cb)(notify_ud, sock_connected, NULL);
    /* Success - return the nsocket */
    return create_sock(fd);
}
Пример #17
0
int
main (
	int	argc,
	char   *argv[]
	)
{
	pgm_error_t* pgm_err = NULL;

	setlocale (LC_ALL, "");

	if (!pgm_init (&pgm_err)) {
		fprintf (stderr, "Unable to start PGM engine: %s\n", pgm_err->message);
		pgm_error_free (pgm_err);
		return EXIT_FAILURE;
	}

/* parse program arguments */
#ifdef _WIN32
	const char* binary_name = strrchr (argv[0], '\\');
#else
	const char* binary_name = strrchr (argv[0], '/');
#endif
	if (NULL == binary_name)	binary_name = argv[0];
	else				binary_name++;

	static struct option long_options[] = {
		{ "network",        required_argument, NULL, 'n' },
		{ "service",        required_argument, NULL, 's' },
		{ "port",           required_argument, NULL, 'p' },
		{ "speed-limit",    required_argument, NULL, 'r' },
		{ "enable-loop",    no_argument,       NULL, 'l' },
		{ "enable-fec",     required_argument, NULL, 'f' },
		{ "list",           no_argument,       NULL, 'i' },
		{ "help",           no_argument,       NULL, 'h' },
		{ NULL, 0, NULL, 0 }
	};

	int c;
	while ((c = getopt_long (argc, argv, "s:n:p:r:f:K:N:lih", long_options, NULL)) != -1)
	{
		switch (c) {
		case 'n':	network = optarg; break;
		case 's':	port = atoi (optarg); break;
		case 'p':	udp_encap_port = atoi (optarg); break;
		case 'r':	max_rte = atoi (optarg); break;
		case 'f':	use_fec = TRUE; break;
		case 'K':	rs_k = atoi (optarg); break;
		case 'N':	rs_n = atoi (optarg); break;

		case 'l':	use_multicast_loop = TRUE; break;

		case 'i':
			pgm_if_print_all();
			return EXIT_SUCCESS;

		case 'h':
		case '?':
			usage (binary_name);
		}
	}

	if (use_fec && ( !rs_n || !rs_k )) {
		fprintf (stderr, "Invalid Reed-Solomon parameters RS(%d,%d).\n", rs_n, rs_k);
		usage (binary_name);
	}

	if (create_sock())
	{
		while (optind < argc) {
			const int status = pgm_send (sock, argv[optind], strlen (argv[optind]) + 1, NULL);
		        if (PGM_IO_STATUS_NORMAL != status) {
				fprintf (stderr, "pgm_send() failed.\n");
		        }
			optind++;
		}
	}

/* cleanup */
	if (sock) {
		pgm_close (sock, TRUE);
		sock = NULL;
	}
	pgm_shutdown();
	return EXIT_SUCCESS;
}
Пример #18
0
int
main (
	int	argc,
	char   *argv[]
	)
{
	pgm_error_t* pgm_err = NULL;

	setlocale (LC_ALL, "");

	if (!pgm_init (&pgm_err)) {
		fprintf (stderr, "Unable to start PGM engine: %s\n", pgm_err->message);
		pgm_error_free (pgm_err);
		return EXIT_FAILURE;
	}

/* parse program arguments */
	const char* binary_name = strrchr (argv[0], '/');
	int c;
	while ((c = getopt (argc, argv, "s:n:p:r:f:K:N:lih")) != -1)
	{
		switch (c) {
		case 'n':	network = optarg; break;
		case 's':	port = atoi (optarg); break;
		case 'p':	udp_encap_port = atoi (optarg); break;
		case 'r':	max_rte = atoi (optarg); break;
		case 'f':	use_fec = TRUE; break;
		case 'K':	rs_k = atoi (optarg); break;
		case 'N':	rs_n = atoi (optarg); break;

		case 'l':	use_multicast_loop = TRUE; break;

		case 'i':
			pgm_if_print_all();
			return EXIT_SUCCESS;

		case 'h':
		case '?':
			usage (binary_name);
		}
	}

	if (use_fec && ( !rs_n || !rs_k )) {
		fprintf (stderr, "Invalid Reed-Solomon parameters RS(%d,%d).\n", rs_n, rs_k);
		usage (binary_name);
	}

	if (create_sock())
	{
		while (optind < argc) {
			const int status = pgm_send (sock, argv[optind], strlen (argv[optind]) + 1, NULL);
		        if (PGM_IO_STATUS_NORMAL != status) {
				fprintf (stderr, "pgm_send() failed.\n");
		        }
			optind++;
		}
	}

/* cleanup */
	if (sock) {
		pgm_close (sock, TRUE);
		sock = NULL;
	}
	pgm_shutdown();
	return EXIT_SUCCESS;
}
Пример #19
0
int main(int argc, char ** argv)
{
    int ret, socket;
    unsigned pid, nb_ports, lcore_id, rx_lcore_id;
    struct sock_parameter sk_param;
    struct sock *sk;
    struct txrx_queue *rxq;
    struct port_queue_conf *port_q;
    struct lcore_queue_conf *lcore_q;

    ret = rte_eal_init(argc, argv);
    if (ret < 0)
        return -1;
    argc -= ret;
    argv += ret;

    /*parse gw ip and mac from cmdline*/
    if (argc > 1) {
        default_host_addr = argv[1];
        if (argc == 3)
            default_gw_addr = argv[2];
        else if (argc == 4)
            default_gw_mac = argv[3];
        else
            rte_exit(EXIT_FAILURE, "invalid arguments\n");
    }

    /*config nic*/
    nb_ports = rte_eth_dev_count();
    if (nb_ports == 0)
        rte_exit(EXIT_FAILURE, "No available NIC\n");
    for (pid = 0; pid < nb_ports; pid++) {
        ret = net_device_init(pid);
        if (ret) {
            RTE_LOG(WARNING, LDNS, "fail to initialize port %u\n", pid);
            goto release_net_device;
        }
    }
    pkt_rx_pool = rte_pktmbuf_pool_create("ldns rx pkt pool",
            PKT_RX_NB,
            32,
            0,
            RTE_MBUF_DEFAULT_BUF_SIZE,
            rte_socket_id());
    if (pkt_rx_pool == NULL)
        rte_exit(EXIT_FAILURE, "cannot alloc rx_mbuf_pool");
    
    /*sock create*/
    sk_param.mode = SOCK_MODE_COMPLETE;
    sk_param.func = dns_process;
    sk = create_sock(0, SOCK_PTOTO_IPPROTO_UDP, &sk_param);
    if (sk == NULL)
        rte_exit(EXIT_FAILURE, "cannot create sock\n");
    if (sock_bind(sk, inet_network(default_host_addr), DNS_PORT))
        rte_exit(EXIT_FAILURE, "cannot bind addr:%s port:%u",
                default_host_addr, DNS_PORT);

    /*init ethdev*/
    lcore_id = 0;
    lcore_q = lcore_q_conf_get(lcore_id);
    for (pid = 0; pid < nb_ports; pid++) {
        port_q = port_q_conf_get(pid);
        ret = rte_eth_dev_configure(pid, rx_rings, tx_rings, &default_rte_eth_conf);
        if (ret != 0)
            rte_exit(EXIT_FAILURE, "port %u configure error\n", pid);

        while (rx_lcore_id == rte_get_master_lcore()
                || !rte_lcore_is_enabled(rx_lcore_id)
                || lcore_q->nb_rxq == nb_rx_queue_per_core) {
            rx_lcore_id++;
            if (rx_lcore_id == RTE_MAX_LCORE)
                rte_exit(EXIT_FAILURE, "not enough core for port %u\n", pid);
            lcore_q = lcore_q_conf_get(lcore_id);
        }

        rxq = &lcore_q->rxq[lcore_q->nb_rxq];
        rxq->port = pid;
        rxq->lcore = rx_lcore_id;
        rxq->qid = port_q->nb_rxq;
        lcore_q->nb_rxq++;
        port_q->nb_rxq++;

        socket = rte_lcore_to_socket_id(rx_lcore_id);
        if (socket == SOCKET_ID_ANY)
            socket = 0;

        ret = rte_eth_tx_queue_setup(pid, rxq->qid, nb_txd, socket, NULL);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, "fail to setup txq %u on port %u",
                    rxq->qid, pid);
        ret = rte_eth_rx_queue_setup(pid, rxq->qid, nb_rxd, socket, NULL, pkt_rx_pool);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, "failt to setup rxq %u on port %u",
                    rxq->qid, pid);

        ret = rte_eth_dev_start(pid);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, "fail to start port %u\n", pid);
    }

	if (dns_set_cfg(&default_dns_cfg))
		rte_exit(EXIT_FAILURE, "fail to set dns configuration%u\n", pid);

    rte_eal_mp_remote_launch(packet_launch_one_lcore, NULL, SKIP_MASTER);
    RTE_LCORE_FOREACH_SLAVE(lcore_id) {
        if (rte_eal_wait_lcore(lcore_id) < 0)
            return -1;
    }

    return 0;

release_net_device:
    for (pid; pid != 0; pid--) {
        net_device_release(pid - 1);
    }
    return -1;
}
Пример #20
0
static int lua_connect_to(lua_State* state)
{
	int workerIdx = 0;
	while(state != g_workers[workerIdx].state)
	{
		workerIdx++;
	}

	const char *ip = lua_tostring(state, 1);
	unsigned short port = lua_tointeger(state, 2);
	
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;

    struct hostent *host;
	if(host = gethostbyname(ip))
	{
        memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);
	}
    else
	{
		addr.sin_addr.s_addr = inet_addr(ip);
	}

    //addr.sin_addr.s_addr = inet_addr(host);
    addr.sin_port = htons(port);

	
    Sock *sock = create_sock(sockfd, g_epollfds[workerIdx], workerIdx);
    g_sock_fd_map[sockfd] = sock;
    
    
    if(connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == 0)
    {
        //printf("connect success %d %d !\n", workerIdx, g_epollfds[workerIdx]);

        int flags = fcntl(sockfd, F_GETFL, 0);
		fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

	    struct epoll_event event;
	    event.data.fd = sockfd;
	    //event.events = EPOLLIN | EPOLLOUT | EPOLLONESHOT;
	    event.events = EPOLLIN | EPOLLONESHOT;

	    epoll_ctl(g_epollfds[workerIdx], EPOLL_CTL_ADD, sockfd, &event);

	    lua_pushinteger(state, sockfd);
    }
    else
    {
    	//printf("connecting %d %d!\n", workerIdx, g_epollfds[workerIdx]);
    	lua_pushinteger(state, -1);
    }

    
    //lua_pop(state, 1);

    return 1;
}
Пример #21
0
static void *server_thread(void *arg)
{
    Server *server = (Server*)arg;

    int nfds;

    struct epoll_event event;
    struct epoll_event events[1024];
    int epoll_fd = epoll_create(MAX_CLIENT_NUM);

    int curr_reader = 0;


    memset(&event, 0, sizeof(event));
    event.data.fd = server->fd;
    event.events = EPOLLIN | EPOLLET;
    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server->fd, &event);

    int sockfd;
    Sock *psock;

    while(server->running)
    {
        nfds = epoll_wait(epoll_fd, events, 20, sizeof(events));  
        for(int i = 0; i < nfds; i++)  
        {
            if(events[i].data.fd == server_sock) //有新的连接  
            {
                sockfd= accept(server_sock, 0, 0); //accept这个连接  
                printf("++++ 000 sockfd: %d\n", sockfd);
                if((SAFE_LIST_SIZE(g_sock_list) < MAX_CLIENT_NUM) && (sockfd < MAX_FD_NUM))
                {
                    int flags = fcntl(sockfd, F_GETFL, 0);
                    fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

                    event.data.fd = sockfd;
                    event.events = EPOLLIN | EPOLLET;  
                    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, event.data.fd, &event); //将新的fd添加到epoll的监听队列中

                    Sock *sock = create_sock(sockfd, epoll_fd);
                    
                    g_sock_fd_map[sockfd] = sock;

                    SAFE_LIST_PUSH(g_sock_list, sock, &sock->list_node);
                    printf("create_sock: %d\n", SAFE_LIST_SIZE(g_sock_list));
                }
                else
                {
                    close(sockfd);
                }
            }
            else if(events[i].events & EPOLLIN ) //接收到数据,读socket  
            {
                printf("==== 111\n");
                read_data(g_server->state, g_sock_fd_map[events[i].data.fd]);
                //write(g_handlers[events[i].data.fd % HANDLER_NUM]->fds[1], (char*)&g_sock_fd_map[events[i].data.fd], sizeof(Sock*));
            }
            /*
            else if(events[i].events & EPOLLOUT) //有数据待发送,写socket  
            {
            }
            else  
            {  
            }
            */
        }
    }
}
Пример #22
0
JNIEXPORT jint JNICALL Java_com_net_NetClient_open(JNIEnv *env, jclass obj)
{
    return create_sock();
}