]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
ecc65f37f636d7f86114a8f57fbae5474b8d1039
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Purpose: GSocket include file (system independent)
6 * -------------------------------------------------------------------------
16 #include <sys/types.h>
18 #if !defined(__cplusplus)
35 typedef struct _GSocket GSocket
;
36 typedef struct _GAddress GAddress
;
72 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
73 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
74 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
75 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
78 typedef int GSocketEventFlags
;
80 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
84 /* Global initializers */
86 /* GSocket_Init() must be called at the beginning */
88 /* GSocket_Cleanup() must be called at the ending */
89 void GSocket_Cleanup();
91 /* Constructors / Destructors */
93 GSocket
*GSocket_new();
94 void GSocket_destroy(GSocket
*socket
);
96 /* This will disable all further IO calls to this socket */
97 void GSocket_Shutdown(GSocket
*socket
);
99 /* Address handling */
101 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
102 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
103 GAddress
*GSocket_GetLocal(GSocket
*socket
);
104 GAddress
*GSocket_GetPeer(GSocket
*socket
);
106 /* Non-oriented connections handlers */
108 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
110 /* Server specific parts */
112 /* GSocket_SetServer:
113 * Sets up the socket as a server. It uses the "Local" field of GSocket.
114 * "Local" must be set by GSocket_SetLocal() before GSocket_SetServer()
115 * is called. Possible error codes are: GSOCK_INVSOCK if socket has not
116 * been initialized, GSOCK_INVADDR if the local address has not been
117 * defined and GSOCK_IOERR for other internal errors.
119 GSocketError
GSocket_SetServer(GSocket
*socket
);
121 /* GSocket_WaitConnection:
122 * Waits for an incoming client connection.
124 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
126 /* Client specific parts */
129 * Establishes a client connection to a server using the "Peer"
130 * field of GSocket. "Peer" must be set by GSocket_SetPeer() before
131 * GSocket_Connect() is called. Possible error codes are GSOCK_INVSOCK,
132 * GSOCK_INVADDR, GSOCK_TIMEDOUT, GSOCK_WOULDBLOCK and GSOCK_IOERR.
133 * If a socket is nonblocking and Connect() returns GSOCK_WOULDBLOCK,
134 * the connection request can be completed later. Use GSocket_Select()
135 * to check it, or wait for a GSOCK_CONNECTION event.
137 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
141 /* Like recv(), send(), ... */
143 /* NOTE: In case we read from a non-oriented connection, the incoming
144 * (outgoing) connection address is stored in the "Local" ("Peer")
147 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
148 int GSocket_Write(GSocket
*socket
, const char *buffer
,
152 * Polls the socket to determine its status. This function will
153 * check for the events specified in the 'flags' parameter, and
154 * it will return a mask indicating which operations can be
155 * performed. This function won't block, regardless of the
156 * mode (blocking|nonblocking) of the socket.
158 GSocketEventFlags
GSocket_Select(GSocket
*socket
, GSocketEventFlags flags
);
160 /* Flags/Parameters */
162 /* GSocket_SetTimeout:
163 * Sets the timeout for blocking calls. Time is
164 * expressed in milliseconds.
166 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
168 /* GSocket_SetNonBlocking:
169 * Sets the socket to non-blocking mode. This is useful if
170 * we don't want to wait.
172 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
);
175 * Returns the last error occured for this socket.
177 GSocketError
GSocket_GetError(GSocket
*socket
);
181 /* Only one callback is possible for each event (INPUT, OUTPUT, CONNECTION
182 * and LOST). The callbacks are called in the following situations:
184 * INPUT: There is at least one byte in the input buffer
185 * OUTPUT: The system is sure that the next write call will not block
186 * CONNECTION: Two cases are possible:
187 * Client socket -> the connection is established
188 * Server socket -> a client requests a connection
189 * LOST: The connection is lost
191 * An event is generated only once and its state is reseted when the
192 * relative IO call is requested.
193 * For example: INPUT -> GSocket_Read()
194 * CONNECTION -> GSocket_Accept()
197 /* GSocket_SetCallback:
198 * Enables the callbacks specified by 'flags'. Note that 'flags'
199 * may be a combination of flags OR'ed toghether, so the same
200 * callback function can be made to accept different events.
201 * The callback function must have the following prototype:
203 * void function(GSocket *socket, GSocketEvent event, char *cdata)
205 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags flags
,
206 GSocketCallback fallback
, char *cdata
);
208 /* GSocket_UnsetCallback:
209 * Disables all callbacks specified by 'flags', which may be a
210 * combination of flags OR'ed toghether.
212 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags flags
);
216 GAddress
*GAddress_new();
217 GAddress
*GAddress_copy(GAddress
*address
);
218 void GAddress_destroy(GAddress
*address
);
220 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
221 GAddressType
GAddress_GetFamily(GAddress
*address
);
223 /* The use of any of the next functions will set the address family to
224 * the specific one. For example if you use GAddress_INET_SetHostName,
225 * address family will be implicitly set to AF_INET.
228 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
229 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
230 unsigned long hostaddr
);
231 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
232 const char *protocol
);
233 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
235 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
237 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
238 unsigned short GAddress_INET_GetPort(GAddress
*address
);
240 /* TODO: Define specific parts (INET6, UNIX) */
242 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
243 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
247 #endif /* __cplusplus */
250 #endif /* wxUSE_SOCKETS */
252 #endif /* __GSOCKET_H */