]>
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 * -------------------------------------------------------------------------
16 #include <sys/types.h>
18 #if !defined(__cplusplus)
30 typedef struct _GSocket GSocket
;
31 typedef struct _GAddress GAddress
;
67 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
68 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
69 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
70 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
73 typedef int GSocketEventFlags
;
75 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
82 /* Global initialisers */
84 /* GSocket_Init() must be called at the beginning */
86 /* GSocket_Cleanup() must be called at the ending */
87 void GSocket_Cleanup();
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() setups 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
);
143 /* Flags/Parameters */
146 GSocket_SetTimeout() sets the timeout for reading and writing IO call. Time
147 is expressed in milliseconds.
149 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
152 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
153 if we don't want to wait.
155 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
);
158 GSocket_GetError() returns the last error occured on the socket stream.
161 GSocketError
GSocket_GetError(GSocket
*socket
);
166 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
167 INPUT: The function is called when there is at least a byte in the
169 OUTPUT: The function is called when the system is sure the next write call
171 CONNECTION: Two cases is possible:
172 Client socket -> the connection is established
173 Server socket -> a client request a connection
174 LOST: the connection is lost
176 SetCallback accepts a combination of these flags so a same callback can
177 receive different events.
179 An event is generated only once and its state is reseted when the relative
180 IO call is requested.
181 For example: INPUT -> GSocket_Read()
182 CONNECTION -> GSocket_Accept()
184 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
185 GSocketCallback fallback
, char *cdata
);
188 UnsetCallback will disables all fallbacks specified by "event".
189 NOTE: event may be a combination of flags
191 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
);
195 GAddress
*GAddress_new();
196 GAddress
*GAddress_copy(GAddress
*address
);
197 void GAddress_destroy(GAddress
*address
);
199 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
200 GAddressType
GAddress_GetFamily(GAddress
*address
);
203 The use of any of the next functions will set the address family to the adapted
204 one. For example if you use GAddress_INET_SetHostName, address family will be AF_INET
208 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
209 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
210 unsigned long hostaddr
);
211 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
212 const char *protocol
);
213 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
215 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
217 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
218 unsigned short GAddress_INET_GetPort(GAddress
*address
);
220 /* TODO: Define specific parts (INET6, UNIX) */
222 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
223 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
226 * System specific functions
229 /* On systems needing an event id */
230 void GSocket_SetEventID(GSocket
*socket
, unsigned long evt_id
);
232 /* On systems which don't have background refresh */
233 void GSocket_DoEvent(unsigned long evt_id
);
237 #endif /* __cplusplus */