コード例 #1
0
ファイル: universal.c プロジェクト: caizhifu/gearman
gearman_return_t gearman_echo(gearman_universal_st *universal,
                              const void *workload,
                              size_t workload_size)
{
  gearman_connection_st *con;
  gearman_packet_st packet;
  gearman_return_t ret;
  bool orig_block_universal;

  ret= gearman_packet_create_args(universal, &packet, GEARMAN_MAGIC_REQUEST,
                                  GEARMAN_COMMAND_ECHO_REQ,
                                  &workload, &workload_size, 1);
  if (ret != GEARMAN_SUCCESS)
  {
    return ret;
  }

  _push_blocking(universal, &orig_block_universal);

  for (con= universal->con_list; con != NULL; con= con->next)
  {
    gearman_packet_st *packet_ptr;

    ret= gearman_connection_send(con, &packet, true);
    if (ret != GEARMAN_SUCCESS)
    {
      goto exit;
    }

    packet_ptr= gearman_connection_recv(con, &(con->packet), &ret, true);
    if (ret != GEARMAN_SUCCESS)
    {
      goto exit;
    }

    assert(packet_ptr);

    if (con->packet.data_size != workload_size ||
        memcmp(workload, con->packet.data, workload_size))
    {
      gearman_packet_free(&(con->packet));
      gearman_universal_set_error(universal, "gearman_echo", "corruption during echo");

      ret= GEARMAN_ECHO_DATA_CORRUPTION;
      goto exit;
    }

    gearman_packet_free(&(con->packet));
  }

  ret= GEARMAN_SUCCESS;

exit:
  gearman_packet_free(&packet);
  _pop_non_blocking(universal, orig_block_universal);

  return ret;
}
コード例 #2
0
ファイル: gearman.c プロジェクト: Xobicvap/gearman-server
void gearman_free(gearman_st *gearman)
{
  gearman_con_st *con;
  gearman_job_st *job;
  gearman_task_st *task;
  gearman_packet_st *packet;

  for (con= gearman->con_list; con != NULL; con= gearman->con_list)
    gearman_con_free(con);

  for (job= gearman->job_list; job != NULL; job= gearman->job_list)
    gearman_job_free(job);

  for (task= gearman->task_list; task != NULL; task= gearman->task_list)
    gearman_task_free(task);

  for (packet= gearman->packet_list; packet != NULL;
       packet= gearman->packet_list)
  {
    gearman_packet_free(packet);
  }

  if (gearman->pfds != NULL)
    free(gearman->pfds);

  if (gearman->options & GEARMAN_ALLOCATED)
    free(gearman);
}
コード例 #3
0
ファイル: base.hpp プロジェクト: FataliBud/gearmand
  virtual ~_worker_function_st()
  {
    if (options.packet_in_use)
    {
      gearman_packet_free(&_packet);
    }

    delete [] _function_name;
  }
コード例 #4
0
ファイル: packet.c プロジェクト: caizhifu/gearman
void gearman_server_io_packet_remove(gearman_server_con_st *con)
{
  gearman_server_packet_st *server_packet= con->io_packet_list;

  gearman_packet_free(&(server_packet->packet));

  (void) pthread_mutex_lock(&con->thread->lock);
  GEARMAN_FIFO_DEL(con->io_packet, server_packet,);
  (void) pthread_mutex_unlock(&con->thread->lock);

  gearman_server_packet_free(server_packet, con->thread, true);
}
コード例 #5
0
ファイル: thread.c プロジェクト: caizhifu/gearman
static void *_proc(void *data)
{
  gearman_server_st *server= (gearman_server_st *)data;
  gearman_server_thread_st *thread;
  gearman_server_con_st *con;
  gearman_server_packet_st *packet;

  while (1)
  {
    (void) pthread_mutex_lock(&(server->proc_lock));
    while (server->proc_wakeup == false)
    {
      if (server->proc_shutdown)
      {
        (void) pthread_mutex_unlock(&(server->proc_lock));
        return NULL;
      }

      (void) pthread_cond_wait(&(server->proc_cond), &(server->proc_lock));
    }
    server->proc_wakeup= false;
    (void) pthread_mutex_unlock(&(server->proc_lock));

    for (thread= server->thread_list; thread != NULL; thread= thread->next)
    {
      while ((con= gearman_server_con_proc_next(thread)) != NULL)
      {
        if (con->is_dead)
        {
          gearman_server_con_free_workers(con);

          while (con->client_list != NULL)
            gearman_server_client_free(con->client_list);

          con->proc_removed= true;
          gearman_server_con_io_add(con);
          continue;
        }

        while (1)
        {
          packet= gearman_server_proc_packet_remove(con);
          if (packet == NULL)
            break;

          con->ret= gearman_server_run_command(con, &(packet->packet));
          gearman_packet_free(&(packet->packet));
          gearman_server_packet_free(packet, con->thread, false);
        }
      }
    }
  }
}
コード例 #6
0
ファイル: thread.c プロジェクト: caizhifu/gearman
gearman_return_t _thread_packet_read(gearman_server_con_st *con)
{
  gearman_return_t ret;

  while (1)
  {
    if (con->packet == NULL)
    {
      con->packet= gearman_server_packet_create(con->thread, true);
      if (con->packet == NULL)
        return GEARMAN_MEMORY_ALLOCATION_FAILURE;
    }

    (void)gearman_connection_recv(&(con->con), &(con->packet->packet), &ret, true);
    if (ret != GEARMAN_SUCCESS)
    {
      if (ret == GEARMAN_IO_WAIT)
        break;

      gearman_server_packet_free(con->packet, con->thread, true);
      con->packet= NULL;
      return ret;
    }

    gearman_log_debug(con->thread->gearman, "%15s:%5s Received  %s",
                      con->host == NULL ? "-" : con->host,
                      con->port == NULL ? "-" : con->port,
                      gearman_command_info_list[con->packet->packet.command].name);

    /* We read a complete packet. */
    if (con->thread->server->flags.threaded)
    {
      /* Multi-threaded, queue for the processing thread to run. */
      gearman_server_proc_packet_add(con, con->packet);
      con->packet= NULL;
    }
    else
    {
      /* Single threaded, run the command here. */
      ret= gearman_server_run_command(con, &(con->packet->packet));
      gearman_packet_free(&(con->packet->packet));
      gearman_server_packet_free(con->packet, con->thread, true);
      con->packet= NULL;
      if (ret != GEARMAN_SUCCESS)
        return ret;
    }
  }

  return GEARMAN_SUCCESS;
}
コード例 #7
0
ファイル: universal.c プロジェクト: caizhifu/gearman
void gearman_free_all_packets(gearman_universal_st *universal)
{
  while (universal->packet_list != NULL)
    gearman_packet_free(universal->packet_list);
}
コード例 #8
0
ファイル: packet.c プロジェクト: caizhifu/gearman
gearman_return_t gearman_server_io_packet_add(gearman_server_con_st *con,
                                              bool take_data,
                                              enum gearman_magic_t magic,
                                              gearman_command_t command,
                                              const void *arg, ...)
{
  gearman_server_packet_st *server_packet;
  va_list ap;
  size_t arg_size;
  gearman_return_t ret;

  server_packet= gearman_server_packet_create(con->thread, false);
  if (server_packet == NULL)
    return GEARMAN_MEMORY_ALLOCATION_FAILURE;

  if (gearman_packet_create(con->thread->gearman,
                            &(server_packet->packet)) == NULL)
  {
    gearman_server_packet_free(server_packet, con->thread, false);
    return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  }

  server_packet->packet.magic= magic;
  server_packet->packet.command= command;

  va_start(ap, arg);

  while (arg != NULL)
  {
    arg_size = va_arg(ap, size_t);

    ret= gearman_packet_create_arg(&(server_packet->packet), arg, arg_size);
    if (ret != GEARMAN_SUCCESS)
    {
      va_end(ap);
      gearman_packet_free(&(server_packet->packet));
      gearman_server_packet_free(server_packet, con->thread, false);
      return ret;
    }

    arg = va_arg(ap, void *);
  }

  va_end(ap);

  ret= gearman_packet_pack_header(&(server_packet->packet));
  if (ret != GEARMAN_SUCCESS)
  {
    gearman_packet_free(&(server_packet->packet));
    gearman_server_packet_free(server_packet, con->thread, false);
    return ret;
  }

  if (take_data)
    server_packet->packet.options.free_data= true;

  (void) pthread_mutex_lock(&con->thread->lock);
  GEARMAN_FIFO_ADD(con->io_packet, server_packet,);
  (void) pthread_mutex_unlock(&con->thread->lock);

  gearman_server_con_io_add(con);

  return GEARMAN_SUCCESS;
}