summaryrefslogtreecommitdiff
path: root/src/include/gtm/libpq-int.h
blob: 5956de8ff22b205636bbaf7e3b0eacb7f348f763 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*-------------------------------------------------------------------------
 *
 * libpq-int.h
 *	  This file contains internal definitions meant to be used only by
 *	  the frontend libpq library, not by applications that call it.
 *
 *	  An application can include this file if it wants to bypass the
 *	  official API defined by libpq-fe.h, but code that does so is much
 *	  more likely to break across PostgreSQL releases than code that uses
 *	  only the official API.
 *
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 * Portions Copyright (c) 2010 Nippon Telegraph and Telephone Corporation
 *
 * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.139 2009/01/01 17:24:03 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

#ifndef LIBPQ_INT_H
#define LIBPQ_INT_H

#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include "gtm/pqcomm.h"
#include "gtm/pqexpbuffer.h"
#include "gtm/gtm_client.h"

/*
 * GTM_Conn stores all the state data associated with a single connection
 * to a backend.
 */
struct gtm_conn
{
	/* Saved values of connection options */
	char	   *pghost;			/* the machine on which the server is running */
	char	   *pghostaddr;		/* the IPv4 address of the machine on which
								 * the server is running, in IPv4
								 * numbers-and-dots notation. Takes precedence
								 * over above. */
	char	   *pgport;			/* the server's communication port */
	char	   *connect_timeout;	/* connection timeout (numeric string) */
	char	   *coordinator_id;	/* coordinator id */
	int			is_proxy;		/* is this a connection to/from a proxy ? */

	/* Optional file to write trace info to */
	FILE	   *Pfdebug;

	/* Status indicators */
	ConnStatusType status;

	/* Connection data */
	int			sock;			/* Unix FD for socket, -1 if not connected */
	SockAddr	laddr;			/* Local address */
	SockAddr	raddr;			/* Remote address */

	/* Transient state needed while establishing connection */
	struct addrinfo *addrlist;	/* list of possible backend addresses */
	struct addrinfo *addr_cur;	/* the one currently being tried */
	int			addrlist_family;	/* needed to know how to free addrlist */

	/* Buffer for data received from backend and not yet processed */
	char	   *inBuffer;		/* currently allocated buffer */
	int			inBufSize;		/* allocated size of buffer */
	int			inStart;		/* offset to first unconsumed data in buffer */
	int			inCursor;		/* next byte to tentatively consume */
	int			inEnd;			/* offset to first position after avail data */

	/* Buffer for data not yet sent to backend */
	char	   *outBuffer;		/* currently allocated buffer */
	int			outBufSize;		/* allocated size of buffer */
	int			outCount;		/* number of chars waiting in buffer */

	/* State for constructing messages in outBuffer */
	int			outMsgStart;	/* offset to msg start (length word); if -1,
								 * msg has no length word */
	int			outMsgEnd;		/* offset to msg end (so far) */

	/* Buffer for current error message */
	PQExpBufferData errorMessage;		/* expansible string */

	/* Buffer for receiving various parts of messages */
	PQExpBufferData workBuffer; /* expansible string */

	/* Pointer to the result of last operation */
	GTM_Result	*result;
};

/* === in fe-misc.c === */

 /*
  * "Get" and "Put" routines return 0 if successful, EOF if not. Note that for
  * Get, EOF merely means the buffer is exhausted, not that there is
  * necessarily any error.
  */
extern int	gtmpqCheckOutBufferSpace(size_t bytes_needed, GTM_Conn *conn);
extern int	gtmpqCheckInBufferSpace(size_t bytes_needed, GTM_Conn *conn);
extern int	gtmpqGetc(char *result, GTM_Conn *conn);
extern int	gtmpqPutc(char c, GTM_Conn *conn);
extern int	gtmpqGets(PQExpBuffer buf, GTM_Conn *conn);
extern int	gtmpqGets_append(PQExpBuffer buf, GTM_Conn *conn);
extern int	gtmpqPuts(const char *s, GTM_Conn *conn);
extern int	gtmpqGetnchar(char *s, size_t len, GTM_Conn *conn);
extern int	gtmpqPutnchar(const char *s, size_t len, GTM_Conn *conn);
extern int	gtmpqGetInt(int *result, size_t bytes, GTM_Conn *conn);
extern int	gtmpqPutInt(int value, size_t bytes, GTM_Conn *conn);
extern int	gtmpqPutMsgStart(char msg_type, bool force_len, GTM_Conn *conn);
extern int	gtmpqPutMsgEnd(GTM_Conn *conn);
extern int	gtmpqReadData(GTM_Conn *conn);
extern int	gtmpqFlush(GTM_Conn *conn);
extern int	gtmpqWait(int forRead, int forWrite, GTM_Conn *conn);
extern int	gtmpqWaitTimed(int forRead, int forWrite, GTM_Conn *conn,
			time_t finish_time);
extern int	gtmpqReadReady(GTM_Conn *conn);
extern int	gtmpqWriteReady(GTM_Conn *conn);

/*
 * In fe-protocol.c
 */
GTM_Result * GTMPQgetResult(GTM_Conn *conn);
extern int gtmpqGetError(GTM_Conn *conn, GTM_Result *result);
void gtmpqFreeResultData(GTM_Result *result, bool is_proxy);

#define SOCK_ERRNO errno
#define SOCK_ERRNO_SET(e) (errno = (e))

#endif   /* LIBPQ_INT_H */