1 2 /* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) NGINX, Inc. 5 */ 6 7 #ifndef _NXT_SENDBUF_H_INCLUDED_ 8 #define _NXT_SENDBUF_H_INCLUDED_ 9 10 11 /* 12 * The sendbuf interface is intended to send a buffer chain to a connection. 13 * It uses sendfile interface if available. Otherwise it can send only 14 * memory buffers, so file buffers must be read in memory in advance. 15 * 16 * The sendbuf interface sets c->socket.write_ready to appropriate state 17 * and returns: 18 * 19 * N > 0 if sendbuf sent N bytes. 20 * 21 * 0 if sendbuf was interrupted (EINTR and so on), 22 * or sendbuf sent previously buffered data, 23 * or single sync buffer has been encountered. 24 * In all these cases sendbuf is ready to continue 25 * operation, unless c->socket.write_ready is cleared. 26 * 27 * NXT_AGAIN if sendbuf did not send any bytes. 28 * 29 * NXT_ERROR if there was erorr. 30 * 31 * The sendbuf limit is size_t type since size_t is large enough and many 32 * sendfile implementations do not support anyway sending more than size_t 33 * at once. The limit support is located at the sendbuf level otherwise 34 * an additional limited chain must be created on each sendbuf call. 35 */ 36 37 38 typedef struct { 39 nxt_buf_t *buf; 40 void *tls; 41 nxt_socket_t socket; 42 nxt_err_t error; 43 nxt_off_t sent; 44 size_t size; 45 size_t limit; 46 47 uint8_t ready; /* 1 bit */ 48 uint8_t once; /* 1 bit */ 49 uint8_t sync; /* 1 bit */ 50 uint8_t last; /* 1 bit */ 51 } nxt_sendbuf_t; 52 53 54 typedef struct { 55 nxt_buf_t *buf; 56 nxt_iobuf_t *iobuf; 57 nxt_uint_t niov; 58 59 uint32_t nmax; 60 uint8_t sync; /* 1 bit */ 61 uint8_t last; /* 1 bit */ 62 uint8_t limit_reached; 63 uint8_t nmax_reached; 64 65 size_t size; 66 size_t limit; 67 } nxt_sendbuf_coalesce_t; 68 69 70 #if (NXT_HAVE_LINUX_SENDFILE) 71 #define NXT_HAVE_SENDFILE 1 72 ssize_t nxt_linux_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, 73 size_t limit); 74 #endif 75 76 #if (NXT_HAVE_FREEBSD_SENDFILE) 77 #define NXT_HAVE_SENDFILE 1 78 ssize_t nxt_freebsd_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, 79 size_t limit); 80 #endif 81 82 #if (NXT_HAVE_SOLARIS_SENDFILEV) 83 #define NXT_HAVE_SENDFILE 1 84 ssize_t nxt_solaris_event_conn_io_sendfilev(nxt_conn_t *c, nxt_buf_t *b, 85 size_t limit); 86 #endif 87 88 #if (NXT_HAVE_MACOSX_SENDFILE) 89 #define NXT_HAVE_SENDFILE 1 90 ssize_t nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, 91 size_t limit); 92 #endif 93 94 #if (NXT_HAVE_AIX_SEND_FILE) 95 #define NXT_HAVE_SENDFILE 1 96 ssize_t nxt_aix_event_conn_io_send_file(nxt_conn_t *c, nxt_buf_t *b, 97 size_t limit); 98 #endif 99 100 #if (NXT_HAVE_HPUX_SENDFILE) 101 #define NXT_HAVE_SENDFILE 1 102 ssize_t nxt_hpux_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, 103 size_t limit); 104 #endif 105 106 ssize_t nxt_event_conn_io_sendbuf(nxt_conn_t *c, nxt_buf_t *b, 107 size_t limit); 108 109 110 nxt_uint_t nxt_sendbuf_mem_coalesce0(nxt_task_t *task, nxt_sendbuf_t *sb, 111 struct iovec *iov, nxt_uint_t niov_max); 112 nxt_uint_t nxt_sendbuf_mem_coalesce(nxt_task_t *task, 113 nxt_sendbuf_coalesce_t *sb); 114 size_t nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb); 115 116 /* 117 * Auxiliary nxt_sendbuf_copy_coalesce() interface copies small memory 118 * buffers into internal buffer before output. It is intended for 119 * SSL/TLS libraries which lack vector I/O interface yet add noticeable 120 * overhead to each SSL/TLS record. 121 */ 122 ssize_t nxt_sendbuf_copy_coalesce(nxt_conn_t *c, nxt_buf_mem_t *bm, 123 nxt_buf_t *b, size_t limit); 124 125 nxt_buf_t *nxt_sendbuf_update(nxt_buf_t *b, size_t sent); 126 nxt_buf_t *nxt_sendbuf_completion(nxt_task_t *task, nxt_work_queue_t *wq, 127 nxt_buf_t *b); 128 void nxt_sendbuf_drain(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b); 129 130 131 #endif /* _NXT_SENDBUF_H_INCLUDED_ */ 132