summaryrefslogtreecommitdiffstats
path: root/connection.c
blob: ecbb7face554fb08873a7075ebf6765d1561c8ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "connection.h"
#include "pipeline.h"
#include <stdlib.h>

struct ptx_connection *ptx_connection_new(void)
{
  struct ptx_connection *conn = malloc(sizeof(struct ptx_connection));
  if (!conn) return NULL;

  /* initialize these in case we goto cleanup later */
  conn->cli_read = NULL;
  conn->cli_write = NULL;
  conn->srv_read = NULL;
  conn->srv_write = NULL;

  /* do initialization */

  if (!(conn->cli_read = ptx_pipeline_new(4))) goto cleanup;
  if (!(conn->cli_write = ptx_pipeline_new(4))) goto cleanup;
  if (!(conn->srv_read = ptx_pipeline_new(4))) goto cleanup;
  if (!(conn->srv_write = ptx_pipeline_new(4))) goto cleanup;

  return conn;

cleanup:
  ptx_connection_free(conn);

  return NULL;
}

void ptx_connection_free(struct ptx_connection *conn)
{
  /* NOTE: this function might be called with a partially-constructed or inconsistent object */

  if (!conn) return;

  /* do cleanup */

  ptx_pipeline_free(conn->cli_read);
  ptx_pipeline_free(conn->cli_write);
  ptx_pipeline_free(conn->srv_read);
  ptx_pipeline_free(conn->srv_write);

  free(conn);
}