]>
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
;
63 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
64 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
65 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
66 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
69 typedef int GSocketEventFlags
;
71 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
78 /* Global initialisers */
80 /* GSocket_Init() must be called at the beginning */
82 /* GSocket_Cleanup() must be called at the ending */
83 void GSocket_Cleanup();
85 /* Constructors / Destructors */
87 GSocket
*GSocket_new();
88 void GSocket_destroy(GSocket
*socket
);
90 /* This will disable all IO calls to this socket but errors are still available */
91 void GSocket_Shutdown(GSocket
*socket
);
93 /* Address handling */
95 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
96 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
97 GAddress
*GSocket_GetLocal(GSocket
*socket
);
98 GAddress
*GSocket_GetPeer(GSocket
*socket
);
100 /* Non-oriented connections handlers */
102 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
104 /* Server specific parts */
107 GSocket_SetServer() setups the socket as a server. It uses the "Local" field
108 of GSocket. "Local" must be set by GSocket_SetLocal() before
109 GSocket_SetServer() is called. In the other case, it returns GSOCK_INVADDR.
111 GSocketError
GSocket_SetServer(GSocket
*socket
);
114 GSocket_WaitConnection() waits for an incoming client connection.
116 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
118 /* Client specific parts */
121 GSocket_Connect() establishes a client connection to a server using the "Peer"
122 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
123 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
125 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
129 /* Like recv(), send(), ... */
131 NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
132 connection address is stored in the "Local" ("Peer") field.
134 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
135 int GSocket_Write(GSocket
*socket
, const char *buffer
,
137 bool GSocket_DataAvailable(GSocket
*socket
);
139 /* Flags/Parameters */
142 GSocket_SetTimeout() sets the timeout for reading and writing IO call. Time
143 is expressed in milliseconds.
145 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
148 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
149 if we don't want to wait.
151 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
);
154 GSocket_GetError() returns the last error occured on the socket stream.
157 GSocketError
GSocket_GetError(GSocket
*socket
);
162 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
163 INPUT: The function is called when there is at least a byte in the
165 OUTPUT: The function is called when the system is sure the next write call
167 CONNECTION: Two cases is possible:
168 Client socket -> the connection is established
169 Server socket -> a client request a connection
170 LOST: the connection is lost
172 SetCallback accepts a combination of these flags so a same callback can
173 receive different events.
175 An event is generated only once and its state is reseted when the relative
176 IO call is requested.
177 For example: INPUT -> GSocket_Read()
178 CONNECTION -> GSocket_Accept()
180 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
181 GSocketCallback fallback
, char *cdata
);
184 UnsetCallback will disables all fallbacks specified by "event".
185 NOTE: event may be a combination of flags
187 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
);
191 GAddress
*GAddress_new();
192 GAddress
*GAddress_copy(GAddress
*address
);
193 void GAddress_destroy(GAddress
*address
);
195 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
196 GAddressType
GAddress_GetFamily(GAddress
*address
);
199 The use of any of the next functions will set the address family to the adapted
200 one. For example if you use GAddress_INET_SetHostName, address family will be AF_INET
204 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
205 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
206 unsigned long hostaddr
);
207 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
208 const char *protocol
);
209 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
211 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
213 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
214 unsigned short GAddress_INET_GetPort(GAddress
*address
);
216 /* TODO: Define specific parts (INET6, UNIX) */
218 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
219 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
222 * System specific functions
225 /* On systems needing an event id */
226 void GSocket_SetEventID(GSocket
*socket
, unsigned long evt_id
);
228 /* On systems which don't have background refresh */
229 void GSocket_DoEvent(unsigned long evt_id
);