1/*
2 * Copyright 1990 Network Computing Devices
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Network Computing Devices not be
9 * used in advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.  Network Computing
11 * Devices makes no representations about the suitability of this software
12 * for any purpose.  It is provided "as is" without express or implied
13 * warranty.
14 *
15 * NETWORK COMPUTING DEVICES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
17 * IN NO EVENT SHALL NETWORK COMPUTING DEVICES BE LIABLE FOR ANY SPECIAL,
18 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
21 * OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Author:  	Dave Lemke, Network Computing Devices, Inc
24 */
25
26#ifndef	_FSIO_H_
27#define	_FSIO_H_
28
29#ifdef DEBUG
30#define	REQUEST_LOG_SIZE	100
31#endif
32
33typedef struct _fs_fpe_alternate {
34    char       *name;
35    Bool        subset;
36}           FSFpeAltRec, *FSFpeAltPtr;
37
38
39/* Per client access contexts */
40typedef struct _fs_client_data {
41    pointer		    client;
42    struct _fs_client_data  *next;
43    XID			    acid;
44    int			    auth_generation;
45} FSClientRec, *FSClientPtr;
46
47#define FS_RECONNECT_POLL	1000
48#define FS_RECONNECT_WAIT	5000
49#define FS_GIVEUP_WAIT		20000
50#define FS_REQUEST_TIMEOUT	20000
51#define FS_OPEN_TIMEOUT		30000
52#define FS_REOPEN_TIMEOUT	10000
53#define FS_FLUSH_POLL		1000
54
55typedef struct _fs_buf {
56    char    *buf;		/* data */
57    long    size;		/* sizeof data */
58    long    insert;		/* where to insert new data */
59    long    remove;		/* where to remove old data */
60} FSBufRec, *FSBufPtr;
61
62#define FS_BUF_INC  1024
63#define FS_BUF_MAX  32768
64
65#define FS_PENDING_WRITE	0x01	    /* some write data is queued */
66#define FS_BROKEN_WRITE		0x02	    /* writes are broken */
67#define FS_BROKEN_CONNECTION	0x04	    /* connection is broken */
68#define FS_PENDING_REPLY	0x08	    /* waiting for a reply */
69#define FS_GIVE_UP		0x10	    /* font server declared useless */
70#define FS_COMPLETE_REPLY	0x20	    /* complete reply ready */
71#define FS_RECONNECTING		0x40
72
73#define FS_CONN_UNCONNECTED	0
74#define FS_CONN_CONNECTING	1
75#define FS_CONN_CONNECTED	2
76#define FS_CONN_SENT_PREFIX	3
77#define FS_CONN_RECV_INIT	4
78#define FS_CONN_SENT_CAT    	5
79#define FS_CONN_RUNNING		6
80
81/* FS specific font FontPathElement data */
82typedef struct _fs_fpe_data {
83    FSFpePtr	next;		/* list of all active fs fpes */
84    int         fs_fd;		/* < 0 when not running */
85    int		fs_conn_state;	/* connection state */
86    int         current_seq;
87    char       *servername;
88    Bool	has_catalogues;
89
90    int         generation;
91    int         numAlts;
92    int		alternate;	/* which alternate is in use +1 */
93    int		fsMajorVersion; /* font server major version number */
94    FSFpeAltPtr alts;
95
96    FSClientPtr	clients;
97    XID		curacid;
98#ifdef DEBUG
99    int         reqindex;
100    struct {
101	int	opcode;
102	int	sequence;
103    } reqbuffer[REQUEST_LOG_SIZE];
104#endif
105    FSBufRec	outBuf;		/* request queue */
106    FSBufRec	inBuf;		/* reply queue */
107    long	inNeed;		/* amount needed for reply */
108
109    CARD32	blockState;
110    CARD32	blockedReplyTime;	/* time to abort blocked read */
111    CARD32	brokenWriteTime;	/* time to retry broken write */
112    CARD32	blockedConnectTime;	/* time to abort blocked connect */
113    CARD32	brokenConnectionTime;	/* time to retry broken connection */
114
115    FSBlockDataPtr  blockedRequests;
116
117    struct _XtransConnInfo *trans_conn; /* transport connection object */
118}           FSFpeRec;
119
120#define fs_outspace(conn)   ((conn)->outBuf.size - (conn)->outBuf.insert)
121#define fs_outqueued(conn)  ((conn)->outBuf.insert - (conn)->outBuf.remove)
122#define fs_inqueued(conn)   ((conn)->inBuf.insert - (conn)->inBuf.remove)
123#define fs_needsflush(conn) (fs_outqueued(conn) != 0)
124#define fs_needsfill(conn)  (fs_inqueued(conn) < (conn)->inNeed)
125#define fs_needsconnect(conn)	((conn)->fs_fd < 0)
126#define fs_data_read(conn)   ((conn)->inBuf.insert - (conn)->inBuf.remove)
127
128#define FSIO_READY  1
129#define FSIO_BLOCK  0
130#define FSIO_ERROR  -1
131
132extern Bool _fs_reopen_server ( FSFpePtr conn );
133extern int _fs_write ( FSFpePtr conn, const char *data, long size );
134extern int _fs_write_pad ( FSFpePtr conn, const char *data, long len );
135extern int _fs_wait_for_readable ( FSFpePtr conn, int ms );
136extern long _fs_pad_length (long len);
137
138extern void _fs_connection_died ( FSFpePtr conn );
139
140extern int  _fs_flush (FSFpePtr conn);
141extern void _fs_mark_block (FSFpePtr conn, CARD32 mask);
142extern void _fs_unmark_block (FSFpePtr conn, CARD32 mask);
143extern void _fs_done_read (FSFpePtr conn, long size);
144extern void _fs_io_reinit (FSFpePtr conn);
145extern int  _fs_start_read (FSFpePtr conn, long size, char **buf);
146extern Bool _fs_io_init (FSFpePtr conn);
147extern void _fs_io_fini (FSFpePtr conn);
148extern int  _fs_poll_connect (XtransConnInfo trans_conn, int timeout);
149extern XtransConnInfo	_fs_connect(char *servername, int *ret);
150
151/* check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX
152 * systems are broken and return EWOULDBLOCK when they should return EAGAIN
153 */
154#ifdef WIN32
155#define ETEST() (WSAGetLastError() == WSAEWOULDBLOCK)
156#else
157#if defined(EAGAIN) && defined(EWOULDBLOCK)
158#define ETEST() (errno == EAGAIN || errno == EWOULDBLOCK)
159#else
160#ifdef EAGAIN
161#define ETEST() (errno == EAGAIN)
162#else
163#define ETEST() (errno == EWOULDBLOCK)
164#endif
165#endif
166#endif
167#ifdef WIN32
168#define ECHECK(err) (WSAGetLastError() == err)
169#define ESET(val) WSASetLastError(val)
170#else
171#ifdef ISC
172#define ECHECK(err) ((errno == err) || ETEST())
173#else
174#define ECHECK(err) (errno == err)
175#endif
176#define ESET(val) errno = val
177#endif
178
179#endif				/* _FSIO_H_ */
180