1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Purpose: GSocket include file (system independent)
7 * Log: Revision 1.1 1999/07/22 17:51:47 GL
8 * Log: Added GSocket for Unix (only GTK for the moment)
9 * Log: Updated wxSocket to use GSocket API
10 * Log: Added a progress bar to client.cpp
11 * Log: Added CopyTo to wxMemoryOutputStream to copy the internal buffer to a specified buffer.
12 * Log: Various changes/fixes to the high-level protocols FTP/HTTP
13 * Log: Various Unicode fixes
14 * Log: Removed sckint.*
16 * Log: Revision 1.2 1999/07/18 15:54:28 guilhem
17 * Log: Copyright, etc.
19 * -------------------------------------------------------------------------
24 #include <sys/types.h>
26 #if !defined(__cplusplus)
40 typedef struct _GSocket GSocket
;
41 typedef struct _GAddress GAddress
;
74 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
75 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
76 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
77 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
,
80 typedef int GSocketEventFlags
;
82 typedef void (*GSocketFallback
)(GSocket
*socket
, GSocketEvent event
,
89 /* Constructors / Destructors */
91 GSocket
*GSocket_new();
92 void GSocket_destroy(GSocket
*socket
);
94 /* This will disable all IO calls to this socket but errors are still available */
95 void GSocket_Shutdown(GSocket
*socket
);
97 /* Address handling */
99 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
100 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
101 GAddress
*GSocket_GetLocal(GSocket
*socket
);
102 GAddress
*GSocket_GetPeer(GSocket
*socket
);
104 /* Non-oriented connections handlers */
106 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
108 /* Server specific parts */
111 GSocket_SetServer() setup the socket as a server. It uses the "Local" field
112 of GSocket. "Local" must be set by GSocket_SetLocal() before
113 GSocket_SetServer() is called. In the other case, it returns GSOCK_INVADDR.
115 GSocketError
GSocket_SetServer(GSocket
*socket
);
118 GSocket_WaitConnection() waits for an incoming client connection.
120 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
122 /* Client specific parts */
125 GSocket_Connect() establishes a client connection to a server using the "Peer"
126 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
127 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
129 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
133 /* Like recv(), send(), ... */
135 NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
136 connection address is stored in the "Local" ("Peer") field.
138 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
139 int GSocket_Write(GSocket
*socket
, const char *buffer
,
141 bool GSocket_DataAvailable(GSocket
*socket
);
146 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
147 if we don't want to wait.
149 void GSocket_SetBlocking(GSocket
*socket
, bool block
);
152 GSocket_GetError() returns the last error occured on the socket stream.
155 GSocketError
GSocket_GetError(GSocket
*socket
);
160 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
161 INPUT: The function is called when there is at least a byte in the
163 OUTPUT: The function is called when the system is sure the next write call
165 CONNECTION: Two cases is possible:
166 Client socket -> the connection is established
167 Server socket -> a client request a connection
168 LOST: the connection is lost
170 SetFallback accepts a combination of these flags so a same callback can
171 receive different events.
173 An event is generated only once and its state is reseted when the relative
174 IO call is requested.
175 For example: INPUT -> GSocket_Read()
176 CONNECTION -> GSocket_Accept()
178 void GSocket_SetFallback(GSocket
*socket
, GSocketEventFlags event
,
179 GSocketFallback fallback
, char *cdata
);
182 UnsetFallback will disables all fallbacks specified by "event".
183 NOTE: event may be a combination of flags
185 void GSocket_UnsetFallback(GSocket
*socket
, GSocketEventFlags event
);
189 GAddress
*GAddress_new();
190 GAddress
*GAddress_copy(GAddress
*address
);
191 void GAddress_destroy(GAddress
*address
);
193 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
194 GAddressType
GAddress_GetFamily(GAddress
*address
);
197 The use of any of the next functions will set the address family to the adapted
198 one. For example if you use GAddress_INET_SetHostName, address family will be AF_INET
202 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
203 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
204 unsigned long hostaddr
);
205 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
);
206 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
208 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
210 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
211 unsigned short GAddress_INET_GetPort(GAddress
*address
);
213 /* TODO: Define specific parts (INET6, UNIX) */
215 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
216 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
219 * System specific functions
222 /* On systems needing an event id */
223 void GSocket_SetEventID(GSocket
*socket
, unsigned long evt_id
);
225 /* On systems which don't have background refresh */
226 void GSocket_DoEvent(unsigned long evt_id
);