plibsys
psocket.h
Go to the documentation of this file.
1 /*
2  * The MIT License
3  *
4  * Copyright (C) 2010-2016 Alexander Saprykin <saprykin.spb@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * 'Software'), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
148 #if !defined (PLIBSYS_H_INSIDE) && !defined (PLIBSYS_COMPILATION)
149 # error "Header files shouldn't be included directly, consider using <plibsys.h> instead."
150 #endif
151 
152 #ifndef PLIBSYS_HEADER_PSOCKET_H
153 #define PLIBSYS_HEADER_PSOCKET_H
154 
155 #include <pmacros.h>
156 #include <psocketaddress.h>
157 #include <perror.h>
158 
160 
162 typedef enum PSocketProtocol_ {
169 
171 typedef enum PSocketType_ {
176 } PSocketType;
177 
179 typedef enum PSocketDirection_ {
183 
185 typedef enum PSocketIOCondition_ {
189 
191 typedef struct PSocket_ PSocket;
192 
209  PError **error);
210 
229  PSocketType type,
230  PSocketProtocol protocol,
231  PError **error);
232 
240 P_LIB_API pint p_socket_get_fd (const PSocket *socket);
241 
256 
265 
275 
296 
310 
329 
350 
364  PError **error);
365 
382  PError **error);
383 
396 
409 
433  PError **error);
434 
445  pboolean keepalive);
446 
460  pboolean blocking);
461 
476  pint backlog);
477 
488  pint timeout);
489 
533 P_LIB_API pboolean p_socket_bind (const PSocket *socket,
534  PSocketAddress *address,
535  pboolean allow_reuse,
536  PError **error);
537 
567  PSocketAddress *address,
568  PError **error);
569 
589  PError **error);
590 
603 P_LIB_API PSocket * p_socket_accept (const PSocket *socket,
604  PError **error);
605 
626  pchar *buffer,
627  psize buflen,
628  PError **error);
629 
651  PSocketAddress **address,
652  pchar *buffer,
653  psize buflen,
654  PError **error);
655 
672 P_LIB_API pssize p_socket_send (const PSocket *socket,
673  const pchar *buffer,
674  psize buflen,
675  PError **error);
676 
696  PSocketAddress *address,
697  const pchar *buffer,
698  psize buflen,
699  PError **error);
700 
714  PError **error);
715 
732  pboolean shutdown_read,
733  pboolean shutdown_write,
734  PError **error);
735 
742 P_LIB_API void p_socket_free (PSocket *socket);
743 
755  PSocketDirection dir,
756  psize size,
757  PError **error);
758 
774  PSocketIOCondition condition,
775  PError **error);
776 
778 
779 #endif /* PLIBSYS_HEADER_PSOCKET_H */
enum PSocketType_ PSocketType
Socket types.
P_LIB_API pboolean p_socket_connect(PSocket *socket, PSocketAddress *address, PError **error)
Connects a socket to a given remote address.
PSocketType_
Socket types.
Definition: psocket.h:171
#define P_END_DECLS
Closes .h file declarations to be exported as C functions, should be always used after P_BEGIN_DECLS...
Definition: pmacros.h:299
P_LIB_API pboolean p_socket_bind(const PSocket *socket, PSocketAddress *address, pboolean allow_reuse, PError **error)
Binds a socket to a given local address.
struct PSocketAddress_ PSocketAddress
Socket address opaque structure.
Definition: psocketaddress.h:85
Connection-less, reliable, datagram passing (i.e.
Definition: psocket.h:175
P_LIB_API pssize p_socket_send(const PSocket *socket, const pchar *buffer, psize buflen, PError **error)
Sends data through a given socket.
Send direction.
Definition: psocket.h:180
P_LIB_API pboolean p_socket_set_buffer_size(const PSocket *socket, PSocketDirection dir, psize size, PError **error)
Sets the socket buffer size for a given data transfer direction.
P_LIB_API pboolean p_socket_get_keepalive(const PSocket *socket)
Checks whether the SO_KEEPALIVE flag is enabled.
int pint
Type for an int.
Definition: ptypes.h:120
char pchar
Type for a char.
Definition: ptypes.h:116
P_LIB_API PSocketAddress * p_socket_get_remote_address(const PSocket *socket, PError **error)
Gets a socket remote endpoint address.
P_LIB_API pboolean p_socket_is_closed(const PSocket *socket)
Checks whether a socket is closed.
P_BEGIN_DECLS enum PSocketFamily_ PSocketFamily
Socket address family.
PSocketIOCondition_
Socket IO waiting (polling) conditions.
Definition: psocket.h:185
P_LIB_API void p_socket_set_timeout(PSocket *socket, pint timeout)
Sets a socket timeout value for blocking I/O operations.
P_LIB_API pint p_socket_get_timeout(const PSocket *socket)
Gets a socket timeout for blocking I/O operations.
P_LIB_API pboolean p_socket_shutdown(PSocket *socket, pboolean shutdown_read, pboolean shutdown_write, PError **error)
Shutdowns a full-duplex socket data transfer link.
P_LIB_API pboolean p_socket_close(PSocket *socket, PError **error)
Closes a socket.
typedefP_BEGIN_DECLS struct PError_ PError
Opaque data structure for an error object.
Definition: perror.h:68
unsigned int psize
Type for a platform independent size_t.
Definition: ptypes.h:224
Miscellaneous macros.
P_LIB_API pint p_socket_get_listen_backlog(const PSocket *socket)
Gets a socket listen backlog parameter.
P_LIB_API void p_socket_set_keepalive(PSocket *socket, pboolean keepalive)
Sets the socket SO_KEEPALIVE flag.
P_LIB_API pssize p_socket_send_to(const PSocket *socket, PSocketAddress *address, const pchar *buffer, psize buflen, PError **error)
Sends data through a given socket to a given address.
Unknown type.
Definition: psocket.h:172
struct PSocket_ PSocket
Socket opaque structure.
Definition: psocket.h:191
P_LIB_API pboolean p_socket_is_connected(const PSocket *socket)
Checks whether a socket is connected.
PSocketDirection_
Socket direction for data operations.
Definition: psocket.h:179
signed int pboolean
Type for a bool.
Definition: ptypes.h:114
P_LIB_API void p_socket_set_blocking(PSocket *socket, pboolean blocking)
Sets a socket blocking mode.
Default protocol.
Definition: psocket.h:164
P_LIB_API pboolean p_socket_listen(PSocket *socket, PError **error)
Puts a socket into a listening state.
Ready to read.
Definition: psocket.h:186
P_LIB_API void p_socket_set_listen_backlog(PSocket *socket, pint backlog)
Sets a socket listen backlog parameter.
P_LIB_API PSocketAddress * p_socket_get_local_address(const PSocket *socket, PError **error)
Gets a socket local (bound) address.
P_LIB_API void p_socket_free(PSocket *socket)
Closes a socket (if not closed yet) and frees its resources.
Error report system.
P_LIB_API pboolean p_socket_io_condition_wait(const PSocket *socket, PSocketIOCondition condition, PError **error)
Waits for a specified I/O condition on socket.
P_LIB_API PSocketType p_socket_get_type(const PSocket *socket)
Gets a socket type.
P_BEGIN_DECLS enum PSocketProtocol_ PSocketProtocol
Socket protocols specified by the IANA.
P_LIB_API pint p_socket_get_fd(const PSocket *socket)
Gets an underlying file descriptor of a socket.
SCTP protocol.
Definition: psocket.h:167
enum PSocketIOCondition_ PSocketIOCondition
Socket IO waiting (polling) conditions.
P_LIB_API pssize p_socket_receive(const PSocket *socket, pchar *buffer, psize buflen, PError **error)
Receives data from a given socket.
P_LIB_API PSocket * p_socket_accept(const PSocket *socket, PError **error)
Accepts a socket incoming connection.
P_LIB_API PSocketProtocol p_socket_get_protocol(const PSocket *socket)
Gets a socket data transfer protocol.
TCP protocol.
Definition: psocket.h:165
P_LIB_API pboolean p_socket_check_connect_result(PSocket *socket, PError **error)
Checks a connection state after calling p_socket_connect().
P_LIB_API pssize p_socket_receive_from(const PSocket *socket, PSocketAddress **address, pchar *buffer, psize buflen, PError **error)
Receives data from a given socket and saves a remote address.
#define P_LIB_API
Exports a symbol from a shared library.
Definition: pmacros.h:138
P_LIB_API pboolean p_socket_get_blocking(PSocket *socket)
Checks whether socket is used in a blocking mode.
Connection oritented, reliable, stream of bytes (i.e.
Definition: psocket.h:173
PSocketProtocol_
Socket protocols specified by the IANA.
Definition: psocket.h:162
P_LIB_API PSocketFamily p_socket_get_family(const PSocket *socket)
Gets a socket address family.
Receive direction.
Definition: psocket.h:181
enum PSocketDirection_ PSocketDirection
Socket direction for data operations.
Socket address wrapper.
Unknown protocol.
Definition: psocket.h:163
Connection-less, unreliable, datagram passing (i.e.
Definition: psocket.h:174
UDP protocol.
Definition: psocket.h:166
P_LIB_API PSocket * p_socket_new(PSocketFamily family, PSocketType type, PSocketProtocol protocol, PError **error)
Creates a new PSocket object.
P_LIB_API PSocket * p_socket_new_from_fd(pint fd, PError **error)
Creates a new PSocket object from a file descriptor.
signed int pssize
Type for a platform independent signed size_t.
Definition: ptypes.h:223
#define P_BEGIN_DECLS
Starts .h file declarations to be exported as C functions.
Definition: pmacros.h:298
Ready to write.
Definition: psocket.h:187