]>
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
;
62 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
63 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
64 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
65 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
68 typedef int GSocketEventFlags
;
70 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
77 /* Global initialisers */
79 /* GSocket_Init() must be called at the beginning */
81 /* GSocket_Cleanup() must be called at the ending */
82 void GSocket_Cleanup();
84 /* Constructors / Destructors */
86 GSocket
*GSocket_new();
87 void GSocket_destroy(GSocket
*socket
);
89 /* This will disable all IO calls to this socket but errors are still available */
90 void GSocket_Shutdown(GSocket
*socket
);
92 /* Address handling */
94 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
95 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
96 GAddress
*GSocket_GetLocal(GSocket
*socket
);
97 GAddress
*GSocket_GetPeer(GSocket
*socket
);
99 /* Non-oriented connections handlers */
101 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
103 /* Server specific parts */
106 GSocket_SetServer() setups the socket as a server. It uses the "Local" field
107 of GSocket. "Local" must be set by GSocket_SetLocal() before
108 GSocket_SetServer() is called. In the other case, it returns GSOCK_INVADDR.
110 GSocketError
GSocket_SetServer(GSocket
*socket
);
113 GSocket_WaitConnection() waits for an incoming client connection.
115 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
117 /* Client specific parts */
120 GSocket_Connect() establishes a client connection to a server using the "Peer"
121 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
122 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
124 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
128 /* Like recv(), send(), ... */
130 NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
131 connection address is stored in the "Local" ("Peer") field.
133 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
134 int GSocket_Write(GSocket
*socket
, const char *buffer
,
136 bool GSocket_DataAvailable(GSocket
*socket
);
138 /* Flags/Parameters */
141 GSocket_SetTimeout() sets the timeout for reading and writing IO call. Time
142 is expressed in milliseconds.
144 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
147 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
148 if we don't want to wait.
150 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
);
153 GSocket_GetError() returns the last error occured on the socket stream.
156 GSocketError
GSocket_GetError(GSocket
*socket
);
161 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
162 INPUT: The function is called when there is at least a byte in the
164 OUTPUT: The function is called when the system is sure the next write call
166 CONNECTION: Two cases is possible:
167 Client socket -> the connection is established
168 Server socket -> a client request a connection
169 LOST: the connection is lost
171 SetCallback accepts a combination of these flags so a same callback can
172 receive different events.
174 An event is generated only once and its state is reseted when the relative
175 IO call is requested.
176 For example: INPUT -> GSocket_Read()
177 CONNECTION -> GSocket_Accept()
179 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
180 GSocketCallback fallback
, char *cdata
);
183 UnsetCallback will disables all fallbacks specified by "event".
184 NOTE: event may be a combination of flags
186 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
);
190 GAddress
*GAddress_new();
191 GAddress
*GAddress_copy(GAddress
*address
);
192 void GAddress_destroy(GAddress
*address
);
194 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
195 GAddressType
GAddress_GetFamily(GAddress
*address
);
198 The use of any of the next functions will set the address family to the adapted
199 one. For example if you use GAddress_INET_SetHostName, address family will be AF_INET
203 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
204 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
205 unsigned long hostaddr
);
206 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
207 const char *protocol
);
208 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
210 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
212 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
213 unsigned short GAddress_INET_GetPort(GAddress
*address
);
215 /* TODO: Define specific parts (INET6, UNIX) */
217 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
218 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
221 * System specific functions
224 /* On systems needing an event id */
225 void GSocket_SetEventID(GSocket
*socket
, unsigned long evt_id
);
227 /* On systems which don't have background refresh */
228 void GSocket_DoEvent(unsigned long evt_id
);