Annotation of embedaddon/strongswan/src/libstrongswan/networking/streams/stream.h, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2013 Martin Willi
! 3: * Copyright (C) 2013 revosec AG
! 4: *
! 5: * This program is free software; you can redistribute it and/or modify it
! 6: * under the terms of the GNU General Public License as published by the
! 7: * Free Software Foundation; either version 2 of the License, or (at your
! 8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
! 9: *
! 10: * This program is distributed in the hope that it will be useful, but
! 11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
! 13: * for more details.
! 14: */
! 15:
! 16: /**
! 17: * @defgroup stream stream
! 18: * @{ @ingroup streams
! 19: */
! 20:
! 21: #ifndef STREAM_H_
! 22: #define STREAM_H_
! 23:
! 24: typedef struct stream_t stream_t;
! 25:
! 26: #include <library.h>
! 27:
! 28: /**
! 29: * Constructor function prototype for stream_t.
! 30: *
! 31: * @param uri URI to create a stream for
! 32: * @return stream instance, NULL on error
! 33: */
! 34: typedef stream_t*(*stream_constructor_t)(char *uri);
! 35:
! 36: /**
! 37: * Callback function prototype, called when stream is ready.
! 38: *
! 39: * It is not allowed to destroy the stream nor to call on_read()/on_write/()
! 40: * during the callback.
! 41: *
! 42: * As select() may return even if a read()/write() would actually block, it is
! 43: * recommended to use the non-blocking calls and handle return values
! 44: * appropriately.
! 45: *
! 46: * @param data data passed during callback registration
! 47: * @param stream associated stream
! 48: * @return FALSE unregisters the invoked callback, TRUE keeps it
! 49: */
! 50: typedef bool (*stream_cb_t)(void *data, stream_t *stream);
! 51:
! 52: /**
! 53: * Abstraction of a Berkeley socket using stream semantics.
! 54: */
! 55: struct stream_t {
! 56:
! 57: /**
! 58: * Read data from the stream.
! 59: *
! 60: * If "block" is FALSE and no data is available, the function returns -1
! 61: * and sets errno to EWOULDBLOCK.
! 62: *
! 63: * @param buf data buffer to read into
! 64: * @param len number of bytes to read
! 65: * @param block TRUE to use a blocking read
! 66: * @return number of bytes read, -1 on error
! 67: */
! 68: ssize_t (*read)(stream_t *this, void *buf, size_t len, bool block);
! 69:
! 70: /**
! 71: * Read data from the stream, avoiding short reads.
! 72: *
! 73: * This call is always blocking, and reads until len has been read
! 74: * completely. If the connection is closed before enough bytes could be
! 75: * returned, errno is set to ECONNRESET.
! 76: *
! 77: * @param buf data buffer to read into
! 78: * @param len number of bytes to read
! 79: * @return TRUE if len bytes read, FALSE on error
! 80: */
! 81: bool (*read_all)(stream_t *this, void *buf, size_t len);
! 82:
! 83: /**
! 84: * Register a callback to invoke when stream has data to read.
! 85: *
! 86: * @param cb callback function, NULL to unregister
! 87: * @param data data to pass to callback
! 88: */
! 89: void (*on_read)(stream_t *this, stream_cb_t cb, void *data);
! 90:
! 91: /**
! 92: * Write data to the stream.
! 93: *
! 94: * If "block" is FALSE and the write would block, the function returns -1
! 95: * and sets errno to EWOULDBLOCK.
! 96: *
! 97: * @param buf data buffer to write
! 98: * @param len number of bytes to write
! 99: * @param block TRUE to use a blocking write
! 100: * @return number of bytes written, -1 on error
! 101: */
! 102: ssize_t (*write)(stream_t *this, void *buf, size_t len, bool block);
! 103:
! 104: /**
! 105: * Write data to the stream, avoiding short writes.
! 106: *
! 107: * This call is always blocking, and writes until len bytes has been
! 108: * written.
! 109: *
! 110: * @param buf data buffer to write
! 111: * @param len number of bytes to write
! 112: * @return TRUE if len bytes written, FALSE on error
! 113: */
! 114: bool (*write_all)(stream_t *this, void *buf, size_t len);
! 115:
! 116: /**
! 117: * Register a callback to invoke when a write would not block.
! 118: *
! 119: * @param cb callback function, NULL to unregister
! 120: * @param data data to pass to callback
! 121: */
! 122: void (*on_write)(stream_t *this, stream_cb_t cb, void *data);
! 123:
! 124: /**
! 125: * Get a FILE reference for this stream.
! 126: *
! 127: * @return FILE*, must be fclose()d, NULL on error
! 128: */
! 129: FILE* (*get_file)(stream_t *this);
! 130:
! 131: /**
! 132: * Destroy a stream_t.
! 133: */
! 134: void (*destroy)(stream_t *this);
! 135: };
! 136:
! 137: /**
! 138: * Create a stream from a file descriptor.
! 139: *
! 140: * The file descriptor MUST be a socket for non-blocking operation.
! 141: *
! 142: * @param fd file descriptor to wrap into a stream_t
! 143: * @return stream instance
! 144: */
! 145: stream_t *stream_create_from_fd(int fd);
! 146:
! 147: #endif /** STREAM_H_ @}*/
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>