terom@40: #ifndef SOCK_TEST_H terom@40: #define SOCK_TEST_H terom@40: terom@40: /** terom@40: * @file terom@40: * terom@40: * Dummy sock_stream implemention for local testing. terom@40: */ terom@40: #include "sock_internal.h" terom@41: #include terom@40: terom@40: /** terom@40: * IO vector terom@40: */ terom@40: struct io_vec { terom@40: /** The buffer */ terom@40: char *buf; terom@40: terom@40: /** Buffer size */ terom@40: size_t len; terom@40: }; terom@40: terom@40: /** terom@40: * Vectored IO-buffer terom@40: */ terom@40: struct io_buf { terom@40: /** The array of buffer-vectors, {NULL}-terminated */ terom@40: struct io_vec *vecs; terom@40: terom@40: /** The number of io_vecs */ terom@40: size_t count; terom@40: terom@41: /** Current read/write vector */ terom@41: struct io_vec *read_vec, *write_vec; terom@40: terom@40: /** Offset into current vector */ terom@40: size_t off; terom@40: }; terom@40: terom@40: /** terom@40: * The per-socket state terom@40: */ terom@40: struct sock_test { terom@40: /** The base struct for sock_stream_* functions */ terom@40: struct sock_stream base; terom@40: terom@40: /** The send/recieve buffers */ terom@40: struct io_buf send_buf, recv_buf; terom@41: terom@41: /** non-blocking mode? */ terom@41: bool nonblocking; terom@41: terom@41: /** No more data is going to be added, return EOF once all the rest is consumed */ terom@41: bool eof; terom@42: terom@42: /** event flags */ terom@42: int ev_mask; terom@40: }; terom@40: terom@40: /** terom@40: * Get a sock_stream pointer from a sock_tcp pointer terom@40: */ terom@40: #define SOCK_TEST_BASE(sock_ptr) (&(sock_ptr)->base) terom@40: terom@40: /** terom@41: * Get the sock_stream.err pointer from a sock_tcp pointer terom@41: */ terom@41: #define SOCK_TEST_ERR(sock_ptr) SOCK_ERR(SOCK_TEST_BASE(sock_ptr)) terom@41: terom@41: /** terom@40: * A dummy stream socket intended for testing purposes. terom@40: */ terom@40: struct sock_test* sock_test_create (void); terom@40: terom@40: /** terom@41: * Destroy the sock buffer, releasing any resource we allocated ourself terom@41: */ terom@41: void sock_test_destroy (struct sock_test *sock); terom@41: terom@41: /** terom@40: * Set the recieve buffer contents. terom@40: * terom@41: * The vectors themselves are copied, but the data they contain is not. terom@41: * terom@41: * If the EOF flag is given, it indicates that no more data will be added, otherwise the eof status is unchanged. terom@40: */ terom@41: void sock_test_set_recv_buffer (struct sock_test *sock, struct io_vec *vecs, size_t count, bool eof); terom@41: terom@41: /** terom@42: * Add some data to the recieve buffer. terom@42: * terom@42: * If events are enabled, they are triggered. terom@41: */ terom@41: void sock_test_add_recv_vec (struct sock_test *sock, struct io_vec vec); terom@40: terom@40: /** terom@43: * Add a string to the recieve buffer using sock_test_add_recv_vec() terom@43: */ terom@43: void sock_test_add_recv_str (struct sock_test *sock, const char *str); terom@43: terom@43: /** terom@46: * Set EOF on recv, and trigger events. terom@46: */ terom@46: void sock_test_set_recv_eof (struct sock_test *sock); terom@46: terom@46: /** terom@43: * Get the send buffer contents as a single string, free() after use if you care about that. terom@43: * terom@43: * Clears the send buffer, so this doesn't return the same data twice. terom@40: */ terom@40: void sock_test_get_send_data (struct sock_test *sock, char **buf, size_t *len); terom@40: terom@40: #endif