src/sock_test.h
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 21:44:34 +0200
changeset 43 42f5dc680930
parent 42 13cfc41f76a7
child 46 0c13bca53ae1
permissions -rw-r--r--
improve irc_conn test
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#ifndef SOCK_TEST_H
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#define SOCK_TEST_H
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
 * @file
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
 *
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
 * Dummy sock_stream implemention for local testing.
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
#include "sock_internal.h"
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    10
#include <stdbool.h>
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
 * IO vector
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
struct io_vec {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    /** The buffer */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    char *buf;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    /** Buffer size */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    size_t len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
 * Vectored IO-buffer
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
struct io_buf {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    /** The array of buffer-vectors, {NULL}-terminated */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    struct io_vec *vecs;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    /** The number of io_vecs */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    size_t count;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    33
    /** Current read/write vector */
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    34
    struct io_vec *read_vec, *write_vec;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    /** Offset into current vector */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    size_t off;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
 * The per-socket state
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
struct sock_test {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    /** The base struct for sock_stream_* functions */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    struct sock_stream base;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    /** The send/recieve buffers */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    struct io_buf send_buf, recv_buf;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    49
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    50
    /** non-blocking mode? */
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    51
    bool nonblocking;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    52
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    53
    /** No more data is going to be added, return EOF once all the rest is consumed */
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    54
    bool eof;
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    55
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    56
    /** event flags */
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    57
    int ev_mask;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
 * Get a sock_stream pointer from a sock_tcp pointer
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
#define SOCK_TEST_BASE(sock_ptr) (&(sock_ptr)->base)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
/**
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    66
 * Get the sock_stream.err pointer from a sock_tcp pointer
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    67
 */
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    68
#define SOCK_TEST_ERR(sock_ptr) SOCK_ERR(SOCK_TEST_BASE(sock_ptr))
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    69
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    70
/**
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
 * A dummy stream socket intended for testing purposes.
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
struct sock_test* sock_test_create (void);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
/**
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    76
 * Destroy the sock buffer, releasing any resource we allocated ourself
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    77
 */
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    78
void sock_test_destroy (struct sock_test *sock);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    79
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    80
/**
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
 * Set the recieve buffer contents.
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
 *
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    83
 * The vectors themselves are copied, but the data they contain is not.
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    84
 *
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    85
 * If the EOF flag is given, it indicates that no more data will be added, otherwise the eof status is unchanged.
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
 */
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    87
void sock_test_set_recv_buffer (struct sock_test *sock, struct io_vec *vecs, size_t count, bool eof);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    88
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    89
/**
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    90
 * Add some data to the recieve buffer.
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    91
 *
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    92
 * If events are enabled, they are triggered.
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    93
 */
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    94
void sock_test_add_recv_vec (struct sock_test *sock, struct io_vec vec);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
/**
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
    97
 * Add a string to the recieve buffer using sock_test_add_recv_vec()
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
    98
 */
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
    99
void sock_test_add_recv_str (struct sock_test *sock, const char *str);
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   100
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   101
/**
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   102
 * Get the send buffer contents as a single string, free() after use if you care about that.
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   103
 *
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   104
 * Clears the send buffer, so this doesn't return the same data twice.
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
void sock_test_get_send_data (struct sock_test *sock, char **buf, size_t *len);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
#endif