]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Purpose: GSocket include file (system independent)
6 * -------------------------------------------------------------------------
11 #include <sys/types.h>
13 #if !defined(__cplusplus)
27 typedef struct _GSocket GSocket
;
28 typedef struct _GAddress GAddress
;
61 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
62 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
63 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
64 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
67 typedef int GSocketEventFlags
;
69 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
76 /* Global initialisers */
78 /* GSocket_Init() must be called at the beginning */
80 /* GSocket_Cleanup() must be called at the ending */
81 void GSocket_Cleanup();
83 /* Constructors / Destructors */
85 GSocket
*GSocket_new();
86 void GSocket_destroy(GSocket
*socket
);
88 /* This will disable all IO calls to this socket but errors are still available */
89 void GSocket_Shutdown(GSocket
*socket
);
91 /* Address handling */
93 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
94 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
95 GAddress
*GSocket_GetLocal(GSocket
*socket
);
96 GAddress
*GSocket_GetPeer(GSocket
*socket
);
98 /* Non-oriented connections handlers */
100 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
102 /* Server specific parts */
105 GSocket_SetServer() setups the socket as a server. It uses the "Local" field
106 of GSocket. "Local" must be set by GSocket_SetLocal() before
107 GSocket_SetServer() is called. In the other case, it returns GSOCK_INVADDR.
109 GSocketError
GSocket_SetServer(GSocket
*socket
);
112 GSocket_WaitConnection() waits for an incoming client connection.
114 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
116 /* Client specific parts */
119 GSocket_Connect() establishes a client connection to a server using the "Peer"
120 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
121 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
123 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
127 /* Like recv(), send(), ... */
129 NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
130 connection address is stored in the "Local" ("Peer") field.
132 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
133 int GSocket_Write(GSocket
*socket
, const char *buffer
,
135 bool GSocket_DataAvailable(GSocket
*socket
);
137 /* Flags/Parameters */
140 GSocket_SetTimeout() sets the timeout for reading and writing IO call. Time
141 is expressed in milliseconds.
143 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
146 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
147 if we don't want to wait.
149 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_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 SetCallback 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_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
179 GSocketCallback fallback
, char *cdata
);
182 UnsetCallback will disables all fallbacks specified by "event".
183 NOTE: event may be a combination of flags
185 void GSocket_UnsetCallback(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 const char *protocol
);
207 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
209 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
211 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
212 unsigned short GAddress_INET_GetPort(GAddress
*address
);
214 /* TODO: Define specific parts (INET6, UNIX) */
216 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
217 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
220 * System specific functions
223 /* On systems needing an event id */
224 void GSocket_SetEventID(GSocket
*socket
, unsigned long evt_id
);
226 /* On systems which don't have background refresh */
227 void GSocket_DoEvent(unsigned long evt_id
);