コード例 #1
0
ファイル: appnet_server.c プロジェクト: lchb369/appnet
void onAcceptEvent( aeEventLoop *el , int fd , void *privdata , int mask )
{
	
	if (servG->listenfd == fd)
	{
		int client_port,connfd,max = 1000;
		char client_ip[46];
		char neterr[1024];
		
		while (max--)
		{
			connfd =
					anetTcpAccept( neterr , fd , client_ip , sizeof( client_ip ) , &client_port );
			
			if (connfd == -1)
			{
				if (errno != EWOULDBLOCK)
				{
					printf( "Accepting client Error connection: %s \n" , neterr );
				}
				return;
			}
			acceptCommonHandler( servG , connfd , client_ip , client_port );
		}
	}
	else
	{
		printf( "onAcceptEvent other fd=%d \n" , fd );
	}
}
コード例 #2
0
ファイル: net_io.c プロジェクト: cruvolo/dump1090
//
//=========================================================================
//
// This function gets called from time to time when the decoding thread is
// awakened by new data arriving. This usually happens a few times every second
//
struct client * modesAcceptClients(void) {
    int fd, port;
    unsigned int j;
    struct client *c;

    for (j = 0; j < MODES_NET_SERVICES_NUM; j++) {
		if (services[j].enabled) {
			fd = anetTcpAccept(Modes.aneterr, *services[j].socket, NULL, &port);
			if (fd == -1) continue;

			anetNonBlock(Modes.aneterr, fd);
			c = (struct client *) malloc(sizeof(*c));
			c->service    = *services[j].socket;
			c->next       = Modes.clients;
			c->fd         = fd;
			c->buflen     = 0;
			Modes.clients = c;
			anetSetSendBuffer(Modes.aneterr,fd, (MODES_NET_SNDBUF_SIZE << Modes.net_sndbuf_size));

			if (*services[j].socket == Modes.sbsos) Modes.stat_sbs_connections++;
			if (*services[j].socket == Modes.ros)   Modes.stat_raw_connections++;
			if (*services[j].socket == Modes.bos)   Modes.stat_beast_connections++;

			j--; // Try again with the same listening port

			if (Modes.debug & MODES_DEBUG_NET)
				printf("Created new client %d\n", fd);
		}
    }
    return Modes.clients;
}
コード例 #3
0
static void accept_cb (EV_P_ ev_io *a, int revents) {
  printf ("accept new connection\n");
  char err[255];
  char ip[255];
  int port = 0;
  int fd = anetTcpAccept(err, a->fd, ip, 255, &port);
  if (fd < 0)
    return;

  /*
    Sadece bu iş parçacığı bu sokete yeni bağlantı ekleyip bağlantıları
    çıkarabileceği için için yeni bağlantıları sokete eklerken mutex
    kullanmıyorum. İlerde kullanırım. Çalışsın bir.
  */
  struct fsock_sock *sock = frm_cont (a, struct fsock_sock, rio);
  struct fsock_sock *root = sock->owner;
  struct fsock_sock *conn = malloc (sizeof (struct fsock_sock));
  struct fsock_thread *thr;
  int index;

  if (conn == NULL)
    return;

  /*  we check socket's owner's type here because this socket is a bind socket
      that attached to*/
  assert (sock->owner->type == FSOCK_SOCK_BASE);
  fsock_sock_init (conn, FSOCK_SOCK_IN);
  conn->owner = sock->owner;
  fsock_mutex_lock (&sock->sync);
  index = fsock_parr_insert (&sock->conns, conn);
  fsock_mutex_unlock (&sock->sync);
  if (index < 0) {
    free (conn);
    return;
  }
  fsock_mutex_lock(&root->sync);
  index = fsock_parr_insert (&root->conns, conn);
  fsock_mutex_unlock(&root->sync);
  if (index < 0) {
    printf ("can not add conection to the root  socket's connections array.");
    return;
  }
  fsock_glock_lock();
  thr = f_choose_thr();
  fsock_glock_unlock();
  conn->thr = thr;
  conn->fd = fd;
  conn->idx = index;
  fsock_thread_start_connection (thr, conn);
  // notify socket about this event or do not like zeromq
  printf ("notify socket about this event or do not like zeromq {socket: %d|%d}\n", sock->idx, sock->uniq);
}
コード例 #4
0
ファイル: server.c プロジェクト: wudikua/lookup
int serverAccept(event *v, int fd) {
	char *msg;
	char clientHost[32];
	int clientPort, clientFd;
	if ((clientFd = anetTcpAccept(msg, fd, &clientHost[0], &clientPort)) > 0) {
		lookup_log(LOG_DEBUG, "accept %s:%d fd:%d\n", clientHost, clientPort, clientFd);
		if (anetNonBlock(msg, clientFd) < 0) {
			return -1;
		}	
		add_event(pool, clientFd, EPOLLIN, serverRead);
	}
	return 0;
}
コード例 #5
0
ファイル: net_io.c プロジェクト: OzRunways/dump1090
//
//=========================================================================
//
// This function gets called from time to time when the decoding thread is
// awakened by new data arriving. This usually happens a few times every second
//
void modesAcceptClients(void) {
    int fd, port;
    unsigned int j;
    struct client *c;
    int services[6];

    services[0] = Modes.ros;
    services[1] = Modes.ris;
    services[2] = Modes.bos;
    services[3] = Modes.bis;
    services[4] = Modes.https;
    services[5] = Modes.sbsos;

    for (j = 0; j < sizeof(services)/sizeof(int); j++) {
        fd = anetTcpAccept(Modes.aneterr, services[j], NULL, &port);
        if (fd == -1) continue;

        if (fd >= MODES_NET_MAX_FD) {
            close(fd);
            return; // Max number of clients reached
        }

        anetNonBlock(Modes.aneterr, fd);
        c = (struct client *) malloc(sizeof(*c));
        c->service = services[j];
        c->fd = fd;
        c->buflen = 0;
        Modes.clients[fd] = c;
        anetSetSendBuffer(Modes.aneterr,fd,MODES_NET_SNDBUF_SIZE);

        if (Modes.maxfd < fd) Modes.maxfd = fd;
        if (services[j] == Modes.sbsos) Modes.stat_sbs_connections++;
        if (services[j] == Modes.ros)   Modes.stat_raw_connections++;
        if (services[j] == Modes.bos)   Modes.stat_beast_connections++;

        if (services[j] == Modes.sbsos) {
            // Send current traffic, if any, immediately
            int len;
            char *sbsmsg = aircraftsToSBS(&len);
            if (sbsmsg) {
                write(fd, sbsmsg, len);
                free(sbsmsg);                
            }
        }
        
        j--; // Try again with the same listening port

        if (Modes.debug & MODES_DEBUG_NET)
            printf("Created new client %d\n", fd);
    }
}
コード例 #6
0
ファイル: client_handler.c プロジェクト: fankeke/small_code
void tc_AccepTcpHandler(aeEventLoop*el,int fd,void*privdata,int mask)
{
    int cport,cfd;
    char cip[128];

    cfd=anetTcpAccept(error_msg,fd,cip,&cport);

    client*c=tc_create_client(cfd);

    if(c==NULL||aeCreateFileEvent(el,cfd,AE_READABLE,tc_readincome,c)
	    ==AE_ERR){
	error();
	tc_free_client(c);
    }
}
コード例 #7
0
ファイル: test.c プロジェクト: hiepnm/redislib
void acceptTcpHandler(aeEventLoop* el, int fd, void* privData, int mask) {
	char cip[INET6_ADDRSTRLEN];
	int cport;
	int cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
	if (cfd == ANET_ERR) {
		fprintf(stderr, "Accepting client connetion: %s", server.neterr);
		return;
	}
	testclient* c;
	if ((c= createClient(cfd))==NULL){
		fprintf(stderr, "Error registering fd event for the new client: %s (fd=%d)", strerror(errno), fd);
		return;
	}
	if (listLength(server.clients) > server.maxclients) {
		char* err = "-ERR max number of clients reached\r\n";
	}
}
コード例 #8
0
ファイル: siloserver.c プロジェクト: stnmrshx/silokatana
void accept_handler(aeEventLoop *el, int fd, void *privdata, int mask) 
{
	int cport, cfd;
	char cip[128];
	(void) el;
	(void)privdata;
	(void)mask;
	struct request *req;

	req = request_new();
	cfd = anetTcpAccept(_svr.neterr,fd,cip,&cport);
	if (cfd == AE_ERR)
		return;
	_clicount++;

	aeCreateFileEvent(_svr.el,cfd,AE_READABLE,read_handler,req);
}
コード例 #9
0
ファイル: network.c プロジェクト: ChellsChen/wind
void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    int cport, cfd;
    char cip[ADDR_IP_LEN];
    char neterr[ANET_ERR_LEN];
    vuiClient *client;

    cfd = anetTcpAccept(neterr, fd, cip, sizeof(cip), &cport);
    if (cfd == AE_ERR) {
        LogWarn("Accepting client connection: %s", neterr);
        return;
    }
    LogWarn("Accepted %s:%d", cip, cport);
    client = createClient(cfd);
    if (client == NULL)
    {
        LogWarn("just support one client one time");
    }
}
コード例 #10
0
ファイル: networking.c プロジェクト: icoulter/workspace_tubii
void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    int cport, cfd, max = MAX_ACCEPTS_PER_CALL;
    char cip[NET_IP_STR_LEN];
    UNUSED(el);
    UNUSED(mask);
    UNUSED(privdata);

    while(max--) {
        cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
        if (cfd == ANET_ERR) {
            if (errno != EWOULDBLOCK)
                Log(WARNING, "Accepting client connection: %s", server.neterr);
            return;
        }
        Log(VERBOSE,"Accepted %s:%d", cip, cport);
        acceptCommonHandler(cfd,0);
    }
}
コード例 #11
0
ファイル: 2baktcp.c プロジェクト: fankeke/small_code
void Proxy_AcceptTcpHandler(aeEventLoop*el,int fd,void*privdata,int mask)
{
    int cport,cfd;
    char cip[128];
    cfd=anetTcpAccept(anet_error,fd,cip,&cport);
    if(cfd==AE_ERR){
	LogError("accept client connection failed:%s",anet_error);
	return;
    }
    LogInfo("Accept client from:%s:%d\n",cip,cport);

    proxy_client*c=AllocClient(cfd);

    if(c==NULL||aeCreateFileEvent(el,cfd,AE_READABLE,ReadIncome,c)==AE_ERR){
	LogError("Create event failed");
	FreeClient(c);
    }
}
コード例 #12
0
ファイル: server.c プロジェクト: wshgit/redis-net
//接受新连接
void AcceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask)
{
	client_t* c = createClient();

	c->fd = anetTcpAccept(g_err_string, fd, c->ipaddr, &c->port);
	if(c->fd == ANET_ERR) {
		fprintf(stderr, "Accepting client connection: %s", g_err_string);
		free(c);
		return;	
	}
	printf("Connected from %s:%d\n", c->ipaddr, c->port);
	
	if( aeCreateFileEvent(el, c->fd, AE_READABLE, ReadFromClient, c) == AE_ERR )
	{
		fprintf(stderr, "Create File Event fail: fd(%d)\n", c->fd);
		close(c->fd);
        free(c);
	}
}
コード例 #13
0
//接受新连接
void AcceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask){
	int cfd, cport;
	char ip_addr[128] = { 0 };
	cfd = anetTcpAccept(g_err_string, fd, ip_addr, strlen(ip_addr), &cport);
	printf("Connected from %s:%d\n", ip_addr, cport);

	if(anetNonBlock(g_err_string, cfd) == ANET_ERR){
		fprintf(stderr, "set nonblock error: %d\n", fd);
	}
	if(anetEnableTcpNoDelay(g_err_string,fd) == ANET_ERR){
		fprintf(stderr, "set nodelay error: %d\n", fd);
	}
	
	if( aeCreateFileEvent(el, cfd, AE_READABLE, 
	ReadFromClient, NULL) == AE_ERR ){
		fprintf(stderr, "client connect fail: %d\n", fd);
		close(fd);
	}
}
コード例 #14
0
ファイル: main.c プロジェクト: fgd1987/elua
//接受新连接
void AcceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask)
{
	int cfd, cport;
	char ip_addr[128] = { 0 };
	cfd = anetTcpAccept(g_err_string, fd, ip_addr, &cport);
	printf("Connected from %s:%d\n", ip_addr, cport);


	lua_State *L = luaL_newstate();
	luaL_openlibs(L);

        lua_register(L, "lwrite", lwrite);

        int r = luaL_loadfile(L, "script.lua");
        if(r != 0) {
                fprintf(stderr, "loadfile error\n");
		close(fd);
                return;
        };      
        lua_pcall(L, 0, 0, 0);

        lua_getglobal(L, "OnConnect");

        lua_pushinteger(L, cfd);

        lua_pcall(L, 1, 0, 0);	

	struct Client *p = (struct Client*)malloc(sizeof(*p));
	p->fd = cfd;
	p->L = L;

	if( aeCreateFileEvent(el, cfd, AE_READABLE,
		ReadFromClient, p) == AE_ERR )
	{
		fprintf(stderr, "client connect fail: %d\n", fd);
		close(fd);
	}
}
コード例 #15
0
void tAcceptProc(struct aeEventLoop* eventLoop, int fd, void* clientData, int mask) {
    int cport;
    char cip[SHTTPSVR_IP_STR_LEN];

    int cfd = anetTcpAccept(NULL, fd, cip, sizeof(cip), &cport);

    if (cfd != -1) {
        anetNonBlock(NULL, cfd);
        anetEnableTcpNoDelay(NULL, cfd);
        if (server.tcpkeepalive) {
            anetKeepAlive(NULL, cfd, server.tcpkeepalive);
        }

        client_data_t* c = create_client(cfd);
        if (aeCreateFileEvent(eventLoop, cfd, AE_READABLE,
                              tReadProc, c) == AE_ERR) {
            DBGLOG("aeCreateFileEvent error.", cip, cport);
            free_client(c);
            return;
        }
    }
    DBGLOG("Accepted %s:%d", cip, cport);
    return;
}