Example #1
0
/* this is the callback function that gets called when the MARK timeout
 * elapsed. It runs in the main thread.
 */
static void
log_writer_mark_timeout(void *cookie)
{
  LogWriter *self = (LogWriter *)cookie;
  LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
  gchar hostname[256];
  gsize hostname_len = sizeof(hostname);
  LogMessage *msg;

  main_loop_assert_main_thread();

  msg = log_msg_new_mark();
  /* timeout: there was no new message on the writer or it is in periodical mode */
  resolve_sockaddr(hostname, &hostname_len, msg->saddr, self->options->use_dns, self->options->use_fqdn, self->options->use_dns_cache, self->options->normalize_hostnames);

  log_msg_set_value(msg, LM_V_HOST, hostname, strlen(hostname));

  /* set the current time in the message stamp */
  msg->timestamps[LM_TS_STAMP] = msg->timestamps[LM_TS_RECVD];

  if (!log_writer_is_msg_suppressed(self, msg))
    {
      log_queue_push_tail(self->queue, msg, &path_options);
      stats_counter_inc(self->processed_messages);
    }
  else
    {
      log_msg_drop(msg, &path_options);
    }

  /* we need to issue another MARK in all mark-mode cases that already
   * triggered this callback (dst-idle, host-idle, periodical).  The
   * original setup of the timer is at a different location:
   *   - log_writer_queue() for "*-idle" modes
   *   - log_writer_init() for periodical mode
   */
  log_writer_postpone_mark_timer(self);
}
static void rxtx_loop()
{
    int ret, send_ret;
	struct sockaddr_in  peer_addr;
    char peer_ip[32];
	uint16_t peer_port;
	while (!the_working_paras.need_exit)
	{
	    ret=fd_readable(the_working_paras.sockfd, 0, 10000);
	    if (ret<=0)
    	{
    	    if (ret<0 && !the_working_paras.no_verbose)
    	        ERR_DBG_PRINT("test fd failed");

			continue;
    	}

        ret=udp_socket_recvfrom(the_working_paras.sockfd
			,the_working_paras.buf
			,sizeof(the_working_paras.buf)
			,&peer_addr);
		INC_STAT(the_stat_data.rx_times_total);

		if (ret>0)
		{
			INC_STAT(the_stat_data.rx_pkts);
			INC_STAT_VALUE(the_stat_data.rx_bytes, ret);

			if (!the_working_paras.no_verbose)
			{
			    resolve_sockaddr(&peer_addr, peer_ip, sizeof(peer_ip), &peer_port);
			    printf("[%"PRIu64"]got %d bytes from %s:%d\n"
					,the_stat_data.rx_pkts
					,ret
					,peer_ip
					,(int)peer_port);

                if (!the_working_paras.no_prt_pkt)
            	{
					printf("the data contents is:\n");
					print_mem(the_working_paras.buf, ret);
            	}
			}
			
			if (!the_working_paras.only_recv)
			{
				if (!the_working_paras.no_verbose)
				{
				    printf("try send back %d bytes to %s:%d\n"
						,ret
						,peer_ip
						,(int)peer_port);

				}
			    send_ret=udp_socket_sendto(the_working_paras.sockfd, the_working_paras.buf, ret, &peer_addr);
				INC_STAT(the_stat_data.tx_pkts_total);
				INC_STAT_VALUE(the_stat_data.tx_bytes_total, ret);

				if (send_ret==ret)
				{
				    INC_STAT(the_stat_data.tx_pkts_succ);
			        INC_STAT_VALUE(the_stat_data.tx_bytes_succ, ret);
				}
				else if (send_ret<=0)
				{
				    INC_STAT(the_stat_data.tx_pkts_fail);
			        INC_STAT_VALUE(the_stat_data.tx_bytes_fail, ret);

					if (!the_working_paras.no_verbose)
					    ERR_DBG_PRINT("tx failed");

				}
				else
					DBG_PRINT_QUIT("abnormal event: udp packet was partly sent!");
			}

		}
		else
		{
			INC_STAT(the_stat_data.rx_times_fail);

			if (!the_working_paras.no_verbose)
			    ERR_DBG_PRINT("rx failed");
		}

		printf("\n\n");
		continue;

	}
}