Esempio n. 1
0
/*通过poll函数循环监视套结字*/
int poll_server (int *sock_fd)
{
    int server_recv_return = 0 ;
    int n ;
    int poll_num = 0 ;      /*记录poll函数返回 发生事件的套结字的个数*/
    int count = 0 ;         /*需要监视的套结字个数*/
    struct pollfd poll_fd[POLLMAX] ;        /*监视列表数组*/
    struct pollfd *poll_fd_temp ;

    memset (&poll_fd[0], 0, POLLMAX*sizeof (struct pollfd)) ;       /*监视列表数组全部清0*/
    /*将侦听套结字存入poll_fd[0]元素中,监听事件为有普通可读数据就绪*/
    poll_fd[0].fd = *sock_fd ;
    poll_fd[0].events = POLLRDNORM ;
    count = 1 ;

    /*循环监视事件*/
    while((poll_num = poll (&poll_fd[0], count, -1)) >= 0) {printf ("start deal\n") ;
        if(poll_fd[0].revents == POLLRDNORM) {      /*侦听套结字上有可读数据时,说明有客户端请求连接*/
            /*从监视列表中寻找第一个未使用的元素*/
            if((poll_fd_temp = poll_get (&poll_fd[0])) == NULL) {
                printf ("too many client\n") ;
                break ;
            }
            if(server_accept (&poll_fd[0].fd, poll_fd_temp) < 0) {
                break ;
            }
            count++ ;
        }
        
        /*将整个监视列表查看一遍,对发生事件的套结字进行处理*/
        for(n = 1; n < POLLMAX; n++) {
            if(poll_fd[n].revents == POLLRDNORM) {
                /*接收命令包*/
                if((server_recv_return = server_recv (&poll_fd[n])) < 0) {
                    continue ;
                }
                /*如果该套结字断开连接,将poll_fd数组中该元素清空*/
                else if(server_recv_return == 1) {
                    memset (&poll_fd[n], 0, sizeof (struct pollfd)) ;
                    printf ("a client gone\n") ;
                }
                else {
                    poll_fd[n].revents = 0 ;        /*重新将发生的事件置为0,以便下一次监视*/
                }
                poll_num-- ;
            }
            if(poll_num <= 0) {         /*发生事件的套结字处理完,直接退出循环*/
                break ;
            }
        }
        printf ("deal end\n") ;
    }
    return 0 ;
}
Esempio n. 2
0
static int
_gnutls_status_request_recv_params(gnutls_session_t session,
				   const uint8_t * data, size_t size)
{
	extension_priv_data_t epriv;
	status_request_ext_st *priv;
	int ret;

	ret = _gnutls_ext_get_session_data(session,
					   GNUTLS_EXTENSION_STATUS_REQUEST,
					   &epriv);
	if (ret < 0 || epriv.ptr == NULL)	/* it is ok not to have it */
		return 0;

	priv = epriv.ptr;

	if (session->security_parameters.entity == GNUTLS_CLIENT)
		return client_recv(session, priv, data, size);
	return server_recv(session, priv, data, size);
}
Esempio n. 3
0
File: rping.c Progetto: hkimura/pib
static int rping_cq_event_handler(struct rping_cb *cb)
{
	struct ibv_wc wc;
	struct ibv_recv_wr *bad_wr;
	int ret;
	int flushed = 0;

	while ((ret = ibv_poll_cq(cb->cq, 1, &wc)) == 1) {
		ret = 0;

		if (wc.status) {
			if (wc.status == IBV_WC_WR_FLUSH_ERR) {
				flushed = 1;
				continue;

			}
			fprintf(stderr,
				"cq completion failed status %d\n",
				wc.status);
			ret = -1;
			goto error;
		}

		switch (wc.opcode) {
		case IBV_WC_SEND:
			DEBUG_LOG("send completion\n");
			break;

		case IBV_WC_RDMA_WRITE:
			DEBUG_LOG("rdma write completion\n");
			cb->state = RDMA_WRITE_COMPLETE;
			sem_post(&cb->sem);
			break;

		case IBV_WC_RDMA_READ:
			DEBUG_LOG("rdma read completion\n");
			cb->state = RDMA_READ_COMPLETE;
			sem_post(&cb->sem);
			break;

		case IBV_WC_RECV:
			DEBUG_LOG("recv completion\n");
			ret = cb->server ? server_recv(cb, &wc) :
					   client_recv(cb, &wc);
			if (ret) {
				fprintf(stderr, "recv wc error: %d\n", ret);
				goto error;
			}

			ret = ibv_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
			if (ret) {
				fprintf(stderr, "post recv error: %d\n", ret);
				goto error;
			}
			sem_post(&cb->sem);
			break;

		default:
			DEBUG_LOG("unknown!!!!! completion\n");
			ret = -1;
			goto error;
		}
	}
	if (ret) {
		fprintf(stderr, "poll error %d\n", ret);
		goto error;
	}
	return flushed;

error:
	cb->state = ERROR;
	sem_post(&cb->sem);
	return ret;
}
Esempio n. 4
0
static int cq_event_handler(struct thread_data *td, enum ibv_wc_opcode opcode)
{
	struct rdmaio_data *rd = td->io_ops->data;
	struct ibv_wc wc;
	struct rdma_io_u_data *r_io_u_d;
	int ret;
	int compevnum = 0;
	int i;

	while ((ret = ibv_poll_cq(rd->cq, 1, &wc)) == 1) {
		ret = 0;
		compevnum++;

		if (wc.status) {
			log_err("fio: cq completion status %d(%s)\n",
				wc.status, ibv_wc_status_str(wc.status));
			return -1;
		}

		switch (wc.opcode) {

		case IBV_WC_RECV:
			if (rd->is_client == 1)
				ret = client_recv(td, &wc);
			else
				ret = server_recv(td, &wc);

			if (ret)
				return -1;

			if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
				break;

			for (i = 0; i < rd->io_u_flight_nr; i++) {
				r_io_u_d = rd->io_us_flight[i]->engine_data;

				if (wc.wr_id == r_io_u_d->rq_wr.wr_id) {
					rd->io_us_flight[i]->resid =
					    rd->io_us_flight[i]->buflen
					    - wc.byte_len;

					rd->io_us_flight[i]->error = 0;

					rd->io_us_completed[rd->
							    io_u_completed_nr]
					    = rd->io_us_flight[i];
					rd->io_u_completed_nr++;
					break;
				}
			}
			if (i == rd->io_u_flight_nr)
				log_err("fio: recv wr %" PRId64 " not found\n",
					wc.wr_id);
			else {
				/* put the last one into middle of the list */
				rd->io_us_flight[i] =
				    rd->io_us_flight[rd->io_u_flight_nr - 1];
				rd->io_u_flight_nr--;
			}

			break;

		case IBV_WC_SEND:
		case IBV_WC_RDMA_WRITE:
		case IBV_WC_RDMA_READ:
			if (wc.wr_id == FIO_RDMA_MAX_IO_DEPTH)
				break;

			for (i = 0; i < rd->io_u_flight_nr; i++) {
				r_io_u_d = rd->io_us_flight[i]->engine_data;

				if (wc.wr_id == r_io_u_d->sq_wr.wr_id) {
					rd->io_us_completed[rd->
							    io_u_completed_nr]
					    = rd->io_us_flight[i];
					rd->io_u_completed_nr++;
					break;
				}
			}
			if (i == rd->io_u_flight_nr)
				log_err("fio: send wr %" PRId64 " not found\n",
					wc.wr_id);
			else {
				/* put the last one into middle of the list */
				rd->io_us_flight[i] =
				    rd->io_us_flight[rd->io_u_flight_nr - 1];
				rd->io_u_flight_nr--;
			}

			break;

		default:
			log_info("fio: unknown completion event %d\n",
				 wc.opcode);
			return -1;
		}
		rd->cq_event_num++;
	}

	if (ret) {
		log_err("fio: poll error %d\n", ret);
		return 1;
	}

	return compevnum;
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
  // server info
  struct addrinfo hints, *serv_info;
  char serv_ip[INET6_ADDRSTRLEN];
  unsigned short int serv_port;
  int max_clients;

  int serv_sockfd;
  int status;

  if (argc != 4) {
    std::cout << "Usage: ./server server_ip server_port max_clients" << std::endl;
    return -1;
  }
  /**
   * get address info of the server
   */
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  status = getaddrinfo(argv[1], argv[2], &hints, &serv_info);
  max_clients = atoi(argv[3]);
  if (status != 0) {
    std::cout << "ERROR GETTING ADDRESS INFO: " << gai_strerror(status) << std::endl;
    return -1;
  }

  /**
   * Create a socket for the server
   */
  serv_sockfd = socket(serv_info->ai_family, serv_info->ai_socktype, serv_info->ai_protocol);
  if (serv_sockfd < 0) {
    std::cout << "ERROR CREATING SOCKET" << std::endl;
    return -1;
  }

  /**
   * Passive open: bind and listen.
   */
  status = bind(serv_sockfd, serv_info->ai_addr, serv_info->ai_addrlen);
  if (status < 0) {
    std::cout << "ERROR BINDING: " << std::endl;
    return -1;
  }
  inet_ntop(serv_info->ai_family, get_in_addr(serv_info->ai_addr), serv_ip, sizeof(serv_ip));
  serv_port = get_in_port(serv_info->ai_addr);
  std::cout << "Server is open now on " << serv_ip << ":" << serv_port << std::endl;
  freeaddrinfo(serv_info);
  status = listen(serv_sockfd, max_clients);
  if (status < 0) {
    std::cout << "ERROR LISTENING: " << std::endl;
    return -1;
  }
  std::cout << "Listening..." << std::endl;

  // new client info
  struct sockaddr_storage client_addr;
  socklen_t client_addrlen;
  char client_ip[INET6_ADDRSTRLEN];
  unsigned short int client_port;
  int client_sockfd;
  char buf[512];

  // message info
  std::map<int, std::string> usr_map;
  int nbytes;

  // file descripters
  fd_set all_fds, read_fds;
  int fdmax;

  FD_ZERO(&all_fds);
  FD_ZERO(&read_fds);
  FD_SET(serv_sockfd, &all_fds);
  fdmax = serv_sockfd;

  while(1) {
    /**
     * Select
     */
    read_fds = all_fds;
    if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
      std::cout << "ERROR SELECTING" << std::endl;
      return -1;
    }
    /**
     * Run through the existing connections lonking for data to read.
     */
    for (int i = 0; i <= fdmax; i++) {
      if (FD_ISSET(i, &read_fds)) {
        if (i == serv_sockfd) {  // handle new connections
          client_addrlen = sizeof(client_addr);
          client_sockfd = accept(serv_sockfd, (struct sockaddr *)&client_addr, &client_addrlen);
          if (client_sockfd < 0) {
            std::cout << "ERROR CONNECTING" << std::endl;
            // return -1;
          } else {
            inet_ntop(client_addr.ss_family,
                      get_in_addr((struct sockaddr *)&client_addr),
                      client_ip, sizeof(client_ip));
            client_port = get_in_port((struct sockaddr *)&client_addr);
            std::cout << "--New connection from " << client_ip << ":" << client_port
                      << " on socket " << client_sockfd << std::endl;
            FD_SET(client_sockfd, &all_fds);
            fdmax = client_sockfd > fdmax ? client_sockfd : fdmax;
          }
        } else {  // receive data from a client
          nbytes = server_recv(i, buf, sizeof(buf), &usr_map, max_clients);
          if (nbytes < 0) {  // connection error or receiving error
            if (usr_map.find(i) != usr_map.end()) {
              std::cout << "User " << usr_map.at(i) << " has left the chat room." << std::endl;
              for (int j = 0; j <= fdmax; j++) {
                if (FD_ISSET(j, &all_fds)) {
                  if (j != serv_sockfd && j != i) {
                    std::cout << "----Sending OFFLINE info to " << usr_map.at(j);
                    if ( server_send_offline(j, usr_map.at(i).c_str(),
                                            usr_map.at(i).size()) < 0) {
                      std::cout << "SENDING OFFLINE ERROR" << std::endl;
                    } else {
                      std::cout << ". Succeeded." << std::endl;
                    }
                  }
                }
              }
              usr_map.erase(i);
            }
            close(i);
            FD_CLR(i, &all_fds);
          } else if (nbytes == 0){    // receive not send
            for (int j = 0; j <= fdmax; j++) {
              if (FD_ISSET(j, &all_fds)) {
                if (j != serv_sockfd && j != i) {
                  std::cout << "----Sending ONLINE info to " << usr_map.at(j);
                  if ( server_send_online(j, usr_map.at(i).c_str(),
                                       usr_map.at(i).size()) < 0) {
                    std::cout << "SENDING ONLINE ERROR" << std::endl;
                  } else {
                    std::cout << ". Succeeded." << std::endl;
                  }
                }
              }
            }
          } else {           // send message to all other clients
            for (int j = 0; j <= fdmax; j++) {
              if (FD_ISSET(j, &all_fds)) {
                if (j != serv_sockfd && j != i) {
                  std::cout << "----Forwarding to " << usr_map.at(j);
                  if ( server_send_fwd(j, buf, nbytes, usr_map.at(i).c_str(),
                                       usr_map.at(i).size()) < 0) {
                    std::cout << "FWD ERROR" << std::endl;
                  } else {
                    std::cout << ". Succeeded." << std::endl;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Esempio n. 6
0
static void krping_cq_event_handler(struct ib_cq *cq, void *ctx)
{
	struct krping_cb *cb = ctx;
	struct ib_wc wc;
	struct ib_recv_wr *bad_wr;
	int ret;

	mtx_lock(&cb->lock);
	KASSERT(cb->cq == cq, ("bad condition"));
	if (cb->state == ERROR) {
		log(LOG_ERR,  "cq completion in ERROR state\n");
		mtx_unlock(&cb->lock);
		return;
	}
	if (!cb->wlat && !cb->rlat && !cb->bw)
		ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
	while ((ret = ib_poll_cq(cb->cq, 1, &wc)) == 1) {
		if (wc.status) {
			if (wc.status == IB_WC_WR_FLUSH_ERR) {
				DEBUG_LOG("cq flushed\n");
				continue;
			} else {
				log(LOG_CRIT, "cq completion failed status %d\n",
					wc.status);
				goto error;
			}
		}

		switch (wc.opcode) {
		case IB_WC_SEND:
			DEBUG_LOG(PFX "send completion\n");
			cb->stats.send_bytes += cb->send_sgl.length;
			cb->stats.send_msgs++;
			break;

		case IB_WC_RDMA_WRITE:
			DEBUG_LOG(PFX "rdma write completion\n");
			cb->stats.write_bytes += cb->rdma_sq_wr.sg_list->length;
			cb->stats.write_msgs++;
			cb->state = RDMA_WRITE_COMPLETE;
			wakeup(cb);
			break;

		case IB_WC_RDMA_READ:
			DEBUG_LOG(PFX "rdma read completion\n");
			cb->stats.read_bytes += cb->rdma_sq_wr.sg_list->length;
			cb->stats.read_msgs++;
			cb->state = RDMA_READ_COMPLETE;
			wakeup(cb);
			break;

		case IB_WC_RECV:
			DEBUG_LOG(PFX "recv completion\n");
			cb->stats.recv_bytes += sizeof(cb->recv_buf);
			cb->stats.recv_msgs++;
			if (cb->wlat || cb->rlat || cb->bw)
				ret = server_recv(cb, &wc);
			else
				ret = cb->server ? server_recv(cb, &wc) :
					   client_recv(cb, &wc);
			if (ret) {
				log(LOG_ERR, "recv wc error: %d\n", ret);
				goto error;
			}

			ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
			if (ret) {
				log(LOG_ERR, "post recv error: %d\n", 
				       ret);
				goto error;
			}
			wakeup(cb);
			break;

		default:
			log(LOG_ERR, "unknown!!!!! completion\n");
			goto error;
		}
	}
	if (ret) {
		log(LOG_ERR, "poll error %d\n", ret);
		goto error;
	}
	mtx_unlock(&cb->lock);
	return;
error:
	cb->state = ERROR;
	wakeup(cb);
	mtx_unlock(&cb->lock);
}
Esempio n. 7
0
void server_look(int conn_fd, char * local_path)
{

	char server_path[SIZE];
	char  filename[SIZE];
	char file_path[SIZE];
	char len[SIZE];
	char tmp[SIZE] = {"\n\n\t\t.\n\n\t\t.."};
	struct dirent * ptr;
	struct stat buf;
	DIR  * dir;
	int fd;
	char name[SIZE][SIZE];
	int i = 0;
	int num;
	char log[SIZE];
	char log_path[SIZE];

	strcpy(log_path, local_path);
	memset(log, '\0', sizeof(log));
	strcpy(log, "浏览服务器目录:");
	strcat(log, log_path);
	sys_log(log);
	stat (local_path, &buf);
	if (S_ISREG(buf.st_mode))
	{
		;
	}
	chdir(local_path);
	getcwd(file_path, sizeof(file_path));
	file_path[strlen(file_path)] = '/';
	file_path[strlen(file_path) + 1] = '\0';
	dir = opendir(file_path);
	while ((ptr = readdir(dir)) != NULL)
	{
		if (strcmp(ptr->d_name, ".") && strcmp(ptr->d_name, ".."))
		{
			strcat(tmp, "\n\n\t\t");
			strcat(tmp, ptr->d_name);
			strcpy(name[i++], ptr->d_name);
		}
	}
	strcat(tmp, "\n请选择要保存的位置\n..返回上层目录\n");
	send(conn_fd, tmp, SIZE, 0);
	recv(conn_fd, filename, SIZE, 0);
	if (strcmp(filename, "1") == 0)
	{
		recv(conn_fd, filename, SIZE, 0);
		mkdir(filename, 0777);
		chdir(filename);
		sys_log("进入新建文件夹目录");
		server_look(conn_fd, filename);
	}
	else if (strcmp (filename, "2") == 0)
	{
loop:
		num = i - 1;
		recv(conn_fd, filename, SIZE, 0);
		for (; num >= 0; num--)
		{
			if (strcmp(filename, name[num]) == 0)
			{
				send_data(conn_fd, "n\n");
				goto loop;
			}
		}
		send_data(conn_fd, "y\n");
		recv(conn_fd, len, SIZE, 0);
		server_recv(conn_fd, filename, len);

	}	
	else if (strncmp(filename, "..", 2) == 0)
	{
		server_look(conn_fd, "..");
	}
	else 
	{
		server_look(conn_fd, filename);
	}
}
/* the loop that waits for a response while in the uncertain state */
void tx_worker_uncertain(worker_state_t* wstate) 
{
    int bytes = 0;
    int running = true;
    struct sockaddr_in recv_addr;
    message_t msg;

    /* loop and wait for reply */
    while(wstate->uncertain) 
    {
        printf("Waiting for decision from transaction manager...\n");

        /* wait for reply */
        bytes = server_recv(wstate->server, &msg, &recv_addr); 
        if (bytes <= 0) {
            /* ask what the f**k happened again */
            message_t ask_msg;
            message_init(&ask_msg, wstate->vclock);
            ask_msg.type = ASK;
            ask_msg.tid = wstate->transaction;

            server_send_to(wstate->server, wstate->tm_host, wstate->tm_port, &ask_msg);
            continue;
        }

        /* update vector clock */
        vclock_update(wstate->node_id, wstate->vclock, msg.vclock);
        vclock_increment(wstate->node_id, wstate->vclock);
        txlog_write_clock(wstate->txlog, wstate->vclock);

        /* we got a reply! */
        switch(msg.type) 
        {
            case COMMIT: {
                /* log commit etc */
                shitviz_append(wstate->node_id, "Commit", wstate->vclock);

                txlog_entry_t entry;
                txentry_init(&entry, LOG_COMMIT, wstate->transaction, wstate->vclock);
                txlog_append(wstate->txlog, &entry);

                wstate->uncertain = false;
                return;
            }

            case ABORT: {
                /* log abort */
                shitviz_append(wstate->node_id, "Abort", wstate->vclock);

                /* roll back */
                update_rollback(wstate);

                wstate->uncertain = false;
                return;
            }

            case PREPARE_TO_COMMIT: 
                shitviz_append(wstate->node_id, "Unexpected PREPARE in uncertain state", wstate->vclock);
                break;

            default:
                shitviz_append(wstate->node_id, "Unknown command in uncertain state", wstate->vclock);
                break;
        }
    }
}
/* main transaction thread recieve loop */
void* tx_worker_thread(void* params) 
{
    worker_state_t* wstate = (worker_state_t*)params;

    printf("Started TM thread: (%s:%d)\n", wstate->tm_host, wstate->tm_port);

    int bytes = 0;
    int running = true;
    struct sockaddr_in recv_addr;
    message_t msg;

    /* if uncertain, wait */
    if (wstate->uncertain) {
        tx_worker_uncertain(wstate);
        /* we're not actually gonna go into the transaction loop,
         * we just need an update on the current transaction */
        running = false;
    }

    while(running) 
    {
        bytes = server_recv(wstate->server, &msg, &recv_addr); 
        if (bytes <= 0)
            continue;

        /* delay function */
        if (wstate->delay != 0 && wstate->delay != -1000) {
            int sleep_time = abs(wstate->delay);
            sleep(sleep_time);
        }

        /* update vector clock */
        vclock_update(wstate->node_id, wstate->vclock, msg.vclock);
        vclock_increment(wstate->node_id, wstate->vclock);
        txlog_write_clock(wstate->txlog, wstate->vclock);
        
        switch(msg.type)
        {
            case PREPARE_TO_COMMIT: {
                message_t vote;
                message_init(&vote, wstate->vclock);
                vote.tid = wstate->transaction;

                /* vote commit? */
                if (wstate->do_commit) {
                    vote.type = PREPARED;
                    shitviz_append(wstate->node_id, "Voting COMMIT", wstate->vclock);

                    /* log prepared */
                    txlog_entry_t entry;
                    txentry_init(&entry, LOG_PREPARED, wstate->transaction, wstate->vclock);
                    txlog_append(wstate->txlog, &entry);

                    /* negative delay crash */
                    if (wstate->delay < 0) exit(1);

                    server_send_to(wstate->server, wstate->tm_host, wstate->tm_port, &vote);

                    /* we're uncertain! force wait for a reply */
                    wstate->uncertain = true;
                    tx_worker_uncertain(wstate);

                    /* this transaction is finished as soon as we get a reply */
                    running = false;
                    break;
                }

                /* vote abort? */
                if (wstate->do_abort) {
                    vote.type = ABORT;
                    shitviz_append(wstate->node_id, "Voting ABORT", wstate->vclock);

                    /* reset state */
                    wstate->do_abort = false;
                    wstate->do_commit = true;

                    /* negative delay crash */
                    if (wstate->delay < 0) exit(1);

                    server_send_to(wstate->server, wstate->tm_host, wstate->tm_port, &vote);

                    /* we're uncertain! force wait for a reply */
                    wstate->uncertain = true;
                    tx_worker_uncertain(wstate);

                    /* this transaction is finished as soon as we get a reply */
                    running = false;
                    break;
                }

                shitviz_append(wstate->node_id, "Dunno what to do for PREPARE :(", wstate->vclock);
                break;
            }

            case TX_ERROR: {
                char err_buff[512];
                sprintf(err_buff, "Manager Error: %s", msg.strdata);
                shitviz_append(wstate->node_id, err_buff, wstate->vclock);

                /* if value=1 then exit the transaction thread */
                if (msg.value) 
                    running = false;
                break;
            }

            case COMMIT: {
                /* log commit etc */
                shitviz_append(wstate->node_id, "Commit", wstate->vclock);

                txlog_entry_t entry;
                txentry_init(&entry, LOG_COMMIT, wstate->transaction, wstate->vclock);
                txlog_append(wstate->txlog, &entry);

                running = false;
                break;
            }

            case ABORT: {
                /* log abort */
                shitviz_append(wstate->node_id, "Abort", wstate->vclock);

                /* roll back */
                update_rollback(wstate);

                running = false;
                break;
            }

            default:
                shitviz_append(wstate->node_id, "Unknown command", wstate->vclock);
                break;
        }
    }
    
    /* clean up */
    printf("Exiting transaction thread\n");
    server_shutdown(&wstate->server);
    wstate->is_active = false;
    return NULL;
}