Пример #1
0
 int ObMergeServer::do_request(common::ObPacket* base_packet)
 {
   int ret = OB_SUCCESS;
   ObPacket* ob_packet = base_packet;
   int32_t packet_code = ob_packet->get_packet_code();
   int32_t version = ob_packet->get_api_version();
   int32_t channel_id = ob_packet->get_channel_id();
   ObDataBuffer* in_buffer = ob_packet->get_buffer();
   ThreadSpecificBuffer::Buffer* thread_buffer = response_buffer_.get_buffer();
   easy_request_t* request = ob_packet->get_request();
   if (NULL == request || NULL == request->ms || NULL == request->ms->c)
   {
     TBSYS_LOG(ERROR, "req or req->ms or req->ms->c is NUll should not reach this");
   }
   else if (NULL == in_buffer || NULL == thread_buffer)
   {
     TBSYS_LOG(ERROR, "in_buffer = %p or out_buffer=%p cannot be NULL.",
         in_buffer, thread_buffer);
   }
   else
   {
     FILL_TRACE_LOG("start handle client=%s request packet wait=%ld",
                    get_peer_ip(base_packet->get_request()),
                    tbsys::CTimeUtil::getTime() - ob_packet->get_receive_ts());
     thread_buffer->reset();
     ObDataBuffer out_buffer(thread_buffer->current(), thread_buffer->remain());
     //TODO read thread stuff multi thread
     ret = service_.do_request(ob_packet->get_receive_ts(), packet_code, version,
         channel_id, request, *in_buffer, out_buffer,
         ob_packet->get_source_timeout() - (tbsys::CTimeUtil::getTime() - ob_packet->get_receive_ts()));
   }
   return ret;
 }
Пример #2
0
struct pkt_info_t init_and_connect(char *host, char *port)
{

	struct pkt_info_t ret;

	memset(&ret, 0x00, sizeof(struct pkt_info_t));

	char           *device = get_device();

	printf("Got interface: %s\n", device);

	ret.fd = init_socket(device);
	printf("Got socket\n");

	ret.p_src_ip = get_local_ip(device);
	printf("Got local IP: %s\n", ret.p_src_ip);

	ret.p_dst_ip = get_peer_ip(host, port);

	register int    i;

	for (i = 0; i < ret.p_dst_ip[0]->total; i++)
		printf("Got peer IP #%d: %s\n", i, ret.p_dst_ip[i]->d_ip);

	ret.p_src_port = ((arc4random() % 32768) + 32768);
	printf("Got local port: %d\n", ret.p_src_port);

	ret.p_dst_port = atoi(port);
	printf("Got peer port: %d\n", ret.p_dst_port);

	struct ether_header *eh =
		uni_get_mac(ret.fd, ret.p_dst_ip[0]->d_ip, port);

	memcpy(ret.p_src_mac, eh->ether_shost, ETH_ALEN);
	memcpy(ret.p_dst_mac, eh->ether_dhost, ETH_ALEN);

	printf("Got local MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
	       eh->ether_shost[0], eh->ether_shost[1],
	       eh->ether_shost[2], eh->ether_shost[3],
	       eh->ether_shost[4], eh->ether_shost[5]);

	printf("Got peer MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
	       eh->ether_dhost[0], eh->ether_dhost[1],
	       eh->ether_dhost[2], eh->ether_dhost[3],
	       eh->ether_dhost[4], eh->ether_dhost[5]);

	return ret;

}
Пример #3
0
static void finish_recv_heart_beat(struct aeEventLoop *eventLoop, int sockfd, void *clientData, int mask)
{
	tracker_client_t *pClient;
	in_addr_t ip;
	printf("finish_recv_heart_beat\n");
	fflush(stdout);
	
	pClient = (tracker_client_t *)clientData;
		
	if(get_peer_ip(sockfd,&ip) < 0)
	{
		clean_tracker_client(pClient);
		return ;
	}
	
	insert_heart_beat(pClient->heart_beat,ip,pClient->return_heart_beat);

	tracker_nio_write(&pClient->return_heart_beat->head,sizeof(pClient->return_heart_beat->head),\
			finish_send_return_heart_beat_head,heart_beat_stop,"finish_recv_heart_beat");
	
	return ;
}
Пример #4
0
const char* socket_stream::get_peer(bool full /* = false */) const
{
	if (stream_ == NULL)
		return dummy_;

	// xxx: acl_vstream 中没有对此地址赋值
	char* ptr = ACL_VSTREAM_PEER(stream_);
	if (ptr == NULL || *ptr == 0)
	{
		char  buf[64];
		if (acl_getpeername(ACL_VSTREAM_SOCK(stream_),
			buf, sizeof(buf)) == -1)
		{
			return dummy_;
		}
		acl_vstream_set_peer(stream_, buf);
	}

	if (full)
		return ACL_VSTREAM_PEER(stream_);
	else
		return get_peer_ip();
}
Пример #5
0
int main(int argc, char *argv[]){


	int port; //port on which server runs
	port = SERVER_FAMOUS_PORT;
	if (argc <= 1){
		printf("group id required.\n");
		exit(1);
	}


	int client_addr_len; // var to store len of the address of client. 
	char *buffer, buffer1[256];
	char *service = argv[1]; // this will now be the group id to connect to OR start(Change variables later!)

	buffer = (char *)malloc(sizeof(char)*100);

	struct sockaddr_in server_addr;

	// creating a socket
	sfd = socket(AF_INET, SOCK_STREAM, 0);
	print_error(sfd, "error opening socket");




	// port = atoi(argv[1]);
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(port);
	//Computer networks are big endian. This means that when little endian computers are going to pass integers over the network (IP addresses for example), they need to convert them to network byte order. Likewise, when the receive integer values over the network, they need to convert them back to their own native representation.
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	// establishing the connection to server.
	int c = connect(sfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
	get_my_ip(sfd);
	get_peer_ip(sfd);
	print_error(c, "could not connect to server.");

	printf("%s\n", "connected to main server");


	// reading from stdin and sending to server.
	size_t write_size;
	int read_size;

	int s = send(sfd, service, sizeof(service), 0);
	print_error(s, "Failed to send initial message to server");
	int r = recv(sfd, buffer1, 256, 0);
	print_error(r, "Failed to receive a reply from the server");
	printf("Server:%s\n", buffer1);

	int service_port = atoi(buffer1);
	if(service_port == 0){
		printf("Service does not exist. \n");
		exit(1);
	}


	memset(buffer, 0, strlen(buffer));
	memset(buffer1, 0, strlen(buffer1));

	close(sfd);
	sleep(5);
	int nsfd = socket(AF_INET, SOCK_STREAM, 0);
	print_error(nsfd, "error opening socket");

	server_addr.sin_port = htons(service_port);
	c = connect(nsfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
	print_error(c, "could not connect to service.");
	printf("%s\n", "Connected to service");
	service_sfd = nsfd;
	pthread_t t;
	pthread_create(&t, NULL, reading_thread, (void*)0);


	while(1){
		printf("Enter message to send to group \n");
		ssize_t in  = getline(&buffer, &write_size, stdin);
		// fgets(buffer,256,stdin);
		// gets(buffer);
		// scanf("%s",buffer);
		print_error(in, "Failed to read from stdin");
		if(strcmp(buffer, "exit") == 0)
			exit(1);

		// printf("Sending:[%s]\n", buffer);
		fflush(stdout);
		int s = send(nsfd, buffer, strlen(buffer), 0);
		print_error(s, "Failed to send message to server");

		//receive a reply from the server
		// int r = recv(nsfd, buffer1, 256,0);
		// print_error(r, "Failed to receive a reply from the server");
		// printf("Server:%s", buffer1);
		memset(buffer, 0, 256);
		memset(buffer1, 0, 256);
	}


	printf("Client disconnected\n");
	fflush(stdout);
	return 0;

}
Пример #6
0
int rf_epoll_wait(epoll_handle_accept *accept_handler,epoll_handle_read_event *read_handler,epoll_handle_write_event *write_handler){
	int nfds;
	int i;
	
	rf_client *rfc;

	nfds = epoll_wait(kdpfd, events, MAX_EVENTS, EPOLL_TIMEOUT);
	if(nfds == -1) {
		if( errno != EINTR ) {
			cclog(2,"ERROR. {%s}\n", strerror(errno));
			return RF_ERR_EPOLL;
		}

		return RF_OK;
	} else if( 0 == nfds) {
		cclog(9,"epoll timeout!");
		return RF_OK;
	}

	int ret = -1;
	int fd;
	for( i = 0 ; i < nfds ; i++ ) {
		if( listen_fd == events[i].data.fd ) {
			accept_handler(listen_fd);
			continue;
		}

		fd = events[i].data.fd;
		rfc = &fd_table[fd];
		if(events[i].events & (EPOLLIN|EPOLLHUP|EPOLLERR)){
			ret = read_handler(fd);
			switch(ret){
				case RF_OK:
					cclog(5,"process success! fd(%d)",fd);
					break;
				case RF_ERR_PEER_CLOSE:
					cclog(2,"close peer socket! close fd(%d),ip(%s)",fd,get_peer_ip(fd));
				//add for async report dir refresh result
					if(rfc->type == RF_CLIENT_REPORT)
					{
						rf_set_session_report_status(fd, RF_SESSION_CLOSE);
					}
				//add for async report dir refresh result

					disconnect_fd(fd);
					break;
				case RF_CLOSE_REPORT:
					if(rfc->type == RF_CLIENT_REPORT)
					{
						cclog(2, "refreshd close report fd: [%d], peer_ip: [%s]", fd, get_peer_ip(fd));
						rf_set_session_report_status(fd, RF_SESSION_SUCCESS);
					}

					disconnect_fd(fd);
					break;
				default:
				//add for async report dir refresh result
					if(rfc->type == RF_CLIENT_REPORT)
					{
						rf_set_session_report_status(fd, RF_SESSION_FAILED);
					}
				//add for async report dir refresh result
					cclog(2,"proccess error! close fd(%d),ip(%s),ret(%d)",fd,get_peer_ip(fd),ret);
					disconnect_fd(fd);
					break;
			}
		}

		if(events[i].events & (EPOLLOUT|EPOLLHUP|EPOLLERR)){
			ret = write_handler(fd);
			switch(ret){
				case RF_OK:
					cclog(5,"process success! fd(%d),ip(%s)",fd,get_peer_ip(fd));
					break;
				case RF_ERR_PEER_CLOSE:
					cclog(2,"close peer socket! close fd(%d),ip(%s)",fd,get_peer_ip(fd));
				//add for async report dir refresh result
					if(rfc->type == RF_CLIENT_REPORT)
					{
						rf_set_session_report_status(fd, RF_SESSION_SUCCESS);
					}
				//add for async report dir refresh result

					disconnect_fd(fd);
					break;
				default:
					//add for async report dir refresh result
                                        rfc = &fd_table[fd];
                                        if(rfc->type == RF_CLIENT_REPORT)
                                        {
						rf_set_session_report_status(fd, RF_SESSION_FAILED);
                                        }
                                	//add for async report dir refresh result
					cclog(2,"proccess error! close fd(%d),ip(%s)",fd,get_peer_ip(fd));
					disconnect_fd(fd);
					break;
			}
		}

	}

	return RF_OK;
}