static void tunnel_connected_cb (GObject *object, GAsyncResult *result, gpointer user_data) { Tunnel *tunnel = user_data; GError *error = NULL; tunnel->server.iostream = (GIOStream *) g_socket_client_connect_to_host_finish (G_SOCKET_CLIENT (object), result, &error); if (!tunnel->server.iostream) { soup_message_set_status (tunnel->msg, SOUP_STATUS_BAD_GATEWAY); soup_message_set_response (tunnel->msg, "text/plain", SOUP_MEMORY_COPY, error->message, strlen (error->message)); g_error_free (error); soup_server_unpause_message (tunnel->self, tunnel->msg); tunnel_close (tunnel); return; } tunnel->server.istream = g_io_stream_get_input_stream (tunnel->server.iostream); tunnel->server.ostream = g_io_stream_get_output_stream (tunnel->server.iostream); soup_message_set_status (tunnel->msg, SOUP_STATUS_OK); soup_server_unpause_message (tunnel->self, tunnel->msg); g_signal_connect (tunnel->msg, "finished", G_CALLBACK (start_tunnel), tunnel); }
void tunnel_destory(Tunnel * tunnel ) { if ( tunnel_is_connected( tunnel ) || tunnel->in_fd != INVALID_SOCKET ) tunnel_close( tunnel ); if ( tunnel->server_socket != INVALID_SOCKET ) closesocket( tunnel->server_socket ); free( tunnel ); }
static void tunnel_wrote_cb (GObject *object, GAsyncResult *result, gpointer user_data) { Tunnel *tunnel = user_data; TunnelEnd *write_end, *read_end; GError *error = NULL; gssize nwrote; nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (object), result, &error); if (nwrote <= 0) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { g_error_free (error); return; } else if (error) { g_print ("Tunnel write failed: %s\n", error->message); g_error_free (error); } tunnel_close (tunnel); return; } if (object == (GObject *)tunnel->client.ostream) { write_end = &tunnel->client; read_end = &tunnel->server; } else { write_end = &tunnel->server; read_end = &tunnel->client; } write_end->nwrote += nwrote; if (write_end->nwrote < read_end->nread) { g_output_stream_write_async (write_end->ostream, read_end->buffer + write_end->nwrote, read_end->nread - write_end->nwrote, G_PRIORITY_DEFAULT, tunnel->cancellable, tunnel_wrote_cb, tunnel); } else { g_input_stream_read_async (read_end->istream, read_end->buffer, BUFSIZE, G_PRIORITY_DEFAULT, tunnel->cancellable, tunnel_read_cb, tunnel); } }
static void tunnel_read_cb (GObject *object, GAsyncResult *result, gpointer user_data) { Tunnel *tunnel = user_data; TunnelEnd *read_end, *write_end; GError *error = NULL; gssize nread; nread = g_input_stream_read_finish (G_INPUT_STREAM (object), result, &error); if (nread <= 0) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { g_error_free (error); return; } else if (error) { g_print ("Tunnel read failed: %s\n", error->message); g_error_free (error); } tunnel_close (tunnel); return; } if (object == (GObject *)tunnel->client.istream) { read_end = &tunnel->client; write_end = &tunnel->server; } else { read_end = &tunnel->server; write_end = &tunnel->client; } read_end->nread = nread; write_end->nwrote = 0; g_output_stream_write_async (write_end->ostream, read_end->buffer, read_end->nread, G_PRIORITY_DEFAULT, tunnel->cancellable, tunnel_wrote_cb, tunnel); }