[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends] バケツリレー[2/2]




---- cocoa.c [2/2] ここから -------------------------
static void* child_main_thread(void* p_client_socket)
{
  int client_socket = (int)((int*)p_client_socket);
  int proxy_socket = -1;
  struct sockaddr_in sa_proxy;
  int len = sizeof(struct sockaddr_in);
  
  proxy_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if( proxy_socket == -1 )
  {
    close(client_socket);
    return;
  }

  memset((char *)&sa_proxy, 0, len);
  sa_proxy.sin_family      = AF_INET;
  sa_proxy.sin_port        = htons(proxy_port);
  sa_proxy.sin_addr.s_addr = proxy_addr;
  if( connect(proxy_socket, (struct sockaddr*)&sa_proxy, len) == 0 )
  {
    ConnectData connect_data;
    pthread_t read_taskId = 0;
    pthread_t write_taskId = 0;

    /* for DEBUG */
    pthread_mutex_lock(&count_mutex);
    count++;
    fprintf(stderr, "thread count : %d\n", count);
    pthread_mutex_unlock(&count_mutex);

    connect_data.client_socket = client_socket;
    connect_data.proxy_socket = proxy_socket;
    pthread_mutex_init(&connect_data.client_socket_mutex, NULL);
    pthread_mutex_init(&connect_data.proxy_socket_mutex, NULL);
    pthread_create(&read_taskId, NULL,
                   child_read_thread, (void*)&connect_data);
    pthread_create(&write_taskId, NULL,
                   child_write_thread, (void*)&connect_data);
    pthread_join(write_taskId, NULL);
    pthread_join(read_taskId, NULL);
    pthread_mutex_destroy(&connect_data.client_socket_mutex);
    pthread_mutex_destroy(&connect_data.proxy_socket_mutex);

    /* for DEBUG */
    pthread_mutex_lock(&count_mutex);
    count--;
    fprintf(stderr, "thread count : %d\n", count);
    pthread_mutex_unlock(&count_mutex);
  }
  else
  {
    close(client_socket);
    return;
  }
}

static void start_server()
{
  int server_socket;
  struct sockaddr_in sa_server;
  int len = sizeof(struct sockaddr_in);

  server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if( server_socket == -1 )
  {
    fprintf(stderr, "cocoa: could not get socket\n");
    return;
  }

  memset((char *)&sa_server, 0, len);
  sa_server.sin_family      = AF_INET;
  sa_server.sin_port        = htons(server_port);
  sa_server.sin_addr.s_addr = server_addr;

  if( bind(server_socket, (struct sockaddr*)&sa_server, len) == -1 )
  {
    fprintf(stderr, "cocoa: could not set socket bind\n");
    return;
  }
/*listen(server_socket, SOMAXCONN);*/
  listen(server_socket, 512);
  fprintf(stderr, "Server Socket OK!!\n");

  for(;;)
  {
    int client_socket = -1;
    struct sockaddr_in sa_client;
    pthread_t child_taskId = 0;

    client_socket = accept(server_socket,
                           (struct sockaddr*)&sa_client, &len);
    if( client_socket == -1 )
    {
      break;
    }
    /* accepted!! */
    pthread_create(&child_taskId, NULL,
                            child_main_thread, (void*)client_socket);
  }

  /* must join all child threads */
  /* not implemneted yet!! */

  close(server_socket);
}

int main(int argc, char* args[])
{
  if( argc != 5 )
  {
    fprintf(stdout, "Usage : cocoa src port dst port\n");
    fprintf(stdout,
       "Example : cocoa 192.168.10.1 8000 202.10.19.10 10080\n");
  }
  else
  {
    server_addr = inet_addr(args[1]);
    server_port = atoi(args[2]);
    proxy_addr  = inet_addr(args[3]);
    proxy_port  = atoi(args[4]);
    start_server();
  }
  return (0);
}
---- cocoa.c [2/2] ここまで -------------------------

-- 
高橋智宏