]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/gsocket.h
7190696fd081ba9b4b623d10e0005f51853533fe
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket)
4 * Purpose: GSocket include file (system independent)
6 * -------------------------------------------------------------------------
15 #include <sys/types.h>
17 #if !defined(__cplusplus)
29 typedef struct _GSocket GSocket
;
30 typedef struct _GAddress GAddress
;
65 GSOCK_INPUT_FLAG
= 1 << GSOCK_INPUT
,
66 GSOCK_OUTPUT_FLAG
= 1 << GSOCK_OUTPUT
,
67 GSOCK_CONNECTION_FLAG
= 1 << GSOCK_CONNECTION
,
68 GSOCK_LOST_FLAG
= 1 << GSOCK_LOST
71 typedef int GSocketEventFlags
;
73 typedef void (*GSocketCallback
)(GSocket
*socket
, GSocketEvent event
,
80 /* Global initialisers */
82 /* GSocket_Init() must be called at the beginning */
84 /* GSocket_Cleanup() must be called at the ending */
85 void GSocket_Cleanup();
87 /* Constructors / Destructors */
89 GSocket
*GSocket_new();
90 void GSocket_destroy(GSocket
*socket
);
92 /* This will disable all IO calls to this socket but errors are still available */
93 void GSocket_Shutdown(GSocket
*socket
);
95 /* Address handling */
97 GSocketError
GSocket_SetLocal(GSocket
*socket
, GAddress
*address
);
98 GSocketError
GSocket_SetPeer(GSocket
*socket
, GAddress
*address
);
99 GAddress
*GSocket_GetLocal(GSocket
*socket
);
100 GAddress
*GSocket_GetPeer(GSocket
*socket
);
102 /* Non-oriented connections handlers */
104 GSocketError
GSocket_SetNonOriented(GSocket
*socket
);
106 /* Server specific parts */
109 GSocket_SetServer() setups the socket as a server. It uses the "Local" field
110 of GSocket. "Local" must be set by GSocket_SetLocal() before
111 GSocket_SetServer() is called. In the other case, it returns GSOCK_INVADDR.
113 GSocketError
GSocket_SetServer(GSocket
*socket
);
116 GSocket_WaitConnection() waits for an incoming client connection.
118 GSocket
*GSocket_WaitConnection(GSocket
*socket
);
120 /* Client specific parts */
123 GSocket_Connect() establishes a client connection to a server using the "Peer"
124 field of GSocket. "Peer" must be set by GSocket_SetPeer() before
125 GSocket_Connect() is called. In the other case, it returns GSOCK_INVADDR.
127 GSocketError
GSocket_Connect(GSocket
*socket
, GSocketStream stream
);
131 /* Like recv(), send(), ... */
133 NOTE: In case we read from a non-oriented connection, the incoming (outgoing)
134 connection address is stored in the "Local" ("Peer") field.
136 int GSocket_Read(GSocket
*socket
, char *buffer
, int size
);
137 int GSocket_Write(GSocket
*socket
, const char *buffer
,
139 bool GSocket_DataAvailable(GSocket
*socket
);
141 /* Flags/Parameters */
144 GSocket_SetTimeout() sets the timeout for reading and writing IO call. Time
145 is expressed in milliseconds.
147 void GSocket_SetTimeout(GSocket
*socket
, unsigned long millisec
);
150 GSocket_SetBlocking() puts the socket in non-blocking mode. This is useful
151 if we don't want to wait.
153 void GSocket_SetNonBlocking(GSocket
*socket
, bool non_block
);
156 GSocket_GetError() returns the last error occured on the socket stream.
159 GSocketError
GSocket_GetError(GSocket
*socket
);
164 Only one fallback is possible for each event (INPUT, OUTPUT, CONNECTION, LOST)
165 INPUT: The function is called when there is at least a byte in the
167 OUTPUT: The function is called when the system is sure the next write call
169 CONNECTION: Two cases is possible:
170 Client socket -> the connection is established
171 Server socket -> a client request a connection
172 LOST: the connection is lost
174 SetCallback accepts a combination of these flags so a same callback can
175 receive different events.
177 An event is generated only once and its state is reseted when the relative
178 IO call is requested.
179 For example: INPUT -> GSocket_Read()
180 CONNECTION -> GSocket_Accept()
182 void GSocket_SetCallback(GSocket
*socket
, GSocketEventFlags event
,
183 GSocketCallback fallback
, char *cdata
);
186 UnsetCallback will disables all fallbacks specified by "event".
187 NOTE: event may be a combination of flags
189 void GSocket_UnsetCallback(GSocket
*socket
, GSocketEventFlags event
);
193 GAddress
*GAddress_new();
194 GAddress
*GAddress_copy(GAddress
*address
);
195 void GAddress_destroy(GAddress
*address
);
197 void GAddress_SetFamily(GAddress
*address
, GAddressType type
);
198 GAddressType
GAddress_GetFamily(GAddress
*address
);
201 The use of any of the next functions will set the address family to the adapted
202 one. For example if you use GAddress_INET_SetHostName, address family will be AF_INET
206 GSocketError
GAddress_INET_SetHostName(GAddress
*address
, const char *hostname
);
207 GSocketError
GAddress_INET_SetHostAddress(GAddress
*address
,
208 unsigned long hostaddr
);
209 GSocketError
GAddress_INET_SetPortName(GAddress
*address
, const char *port
,
210 const char *protocol
);
211 GSocketError
GAddress_INET_SetPort(GAddress
*address
, unsigned short port
);
213 GSocketError
GAddress_INET_GetHostName(GAddress
*address
, char *hostname
,
215 unsigned long GAddress_INET_GetHostAddress(GAddress
*address
);
216 unsigned short GAddress_INET_GetPort(GAddress
*address
);
218 /* TODO: Define specific parts (INET6, UNIX) */
220 GSocketError
GAddress_UNIX_SetPath(GAddress
*address
, const char *path
);
221 GSocketError
GAddress_UNIX_GetPath(GAddress
*address
, char *path
, size_t sbuf
);
224 * System specific functions
227 /* On systems needing an event id */
228 void GSocket_SetEventID(GSocket
*socket
, unsigned long evt_id
);
230 /* On systems which don't have background refresh */
231 void GSocket_DoEvent(unsigned long evt_id
);
235 #endif /* __cplusplus */