Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __HTTP2_CLIENT_SESSION_H__
00025 #define __HTTP2_CLIENT_SESSION_H__
00026
00027 #include "HTTP2.h"
00028 #include "ProxyClientSession.h"
00029 #include "Http2ConnectionState.h"
00030
00031
00032
00033
00034
00035
00036
00037 #define HTTP2_SESSION_EVENT_INIT (HTTP2_SESSION_EVENTS_START + 1)
00038 #define HTTP2_SESSION_EVENT_FINI (HTTP2_SESSION_EVENTS_START + 2)
00039 #define HTTP2_SESSION_EVENT_RECV (HTTP2_SESSION_EVENTS_START + 3)
00040 #define HTTP2_SESSION_EVENT_XMIT (HTTP2_SESSION_EVENTS_START + 4)
00041
00042 static size_t const HTTP2_HEADER_BUFFER_SIZE_INDEX = CLIENT_CONNECTION_FIRST_READ_BUFFER_SIZE_INDEX;
00043
00044 class Http2Frame
00045 {
00046 public:
00047 Http2Frame(const Http2FrameHeader& h, IOBufferReader * r) {
00048 this->hdr.cooked = h;
00049 this->ioreader = r;
00050 }
00051
00052 Http2Frame(Http2FrameType type, Http2StreamId streamid, uint8_t flags = 0) {
00053 Http2FrameHeader hdr = { 0, (uint8_t)type, flags, streamid };
00054 http2_write_frame_header(hdr, make_iovec(this->hdr.raw));
00055 }
00056
00057 IOBufferReader * reader() const {
00058 return ioreader;
00059 }
00060
00061 const Http2FrameHeader& header() const {
00062 return this->hdr.cooked;
00063 }
00064
00065
00066
00067 void alloc(int index) {
00068 this->ioblock = new_IOBufferBlock();
00069 this->ioblock->alloc(index);
00070 memcpy(this->ioblock->start(), this->hdr.raw, sizeof(this->hdr.raw));
00071 this->ioblock->fill(sizeof(this->hdr.raw));
00072
00073 http2_parse_frame_header(make_iovec(this->ioblock->start(), HTTP2_FRAME_HEADER_LEN), this->hdr.cooked);
00074 }
00075
00076
00077 IOVec write() {
00078 return make_iovec(this->ioblock->end(), this->ioblock->write_avail());
00079 }
00080
00081
00082 void finalize(size_t nbytes) {
00083 if (this->ioblock) {
00084 ink_assert((int64_t)nbytes <= this->ioblock->write_avail());
00085 this->ioblock->fill(nbytes);
00086
00087 this->hdr.cooked.length = this->ioblock->size() - HTTP2_FRAME_HEADER_LEN;
00088 http2_write_frame_header(this->hdr.cooked, make_iovec(this->ioblock->start(), HTTP2_FRAME_HEADER_LEN));
00089 }
00090 }
00091
00092 void xmit(MIOBuffer * iobuffer) {
00093 if (ioblock) {
00094 iobuffer->append_block(this->ioblock);
00095 } else {
00096 iobuffer->write(this->hdr.raw, sizeof(this->hdr.raw));
00097 }
00098 }
00099
00100 private:
00101 Http2Frame(Http2Frame &);
00102 Http2Frame& operator=(const Http2Frame &);
00103
00104 Ptr<IOBufferBlock> ioblock;
00105 IOBufferReader * ioreader;
00106
00107 union {
00108 Http2FrameHeader cooked;
00109 uint8_t raw[HTTP2_FRAME_HEADER_LEN];
00110 } hdr;
00111 };
00112
00113 class Http2ClientSession : public ProxyClientSession
00114 {
00115 public:
00116 Http2ClientSession();
00117
00118 typedef int (Http2ClientSession::*SessionHandler)(int, void *);
00119
00120
00121 void start();
00122 void destroy();
00123 void new_connection(NetVConnection * new_vc, MIOBuffer * iobuf, IOBufferReader * reader, bool backdoor);
00124
00125
00126 VIO * do_io_read(Continuation * c, int64_t nbytes = INT64_MAX, MIOBuffer * buf = 0);
00127 VIO * do_io_write(Continuation * c = NULL, int64_t nbytes = INT64_MAX, IOBufferReader * buf = 0, bool owner = false);
00128 void do_io_close(int lerrno = -1);
00129 void do_io_shutdown(ShutdownHowTo_t howto);
00130 void reenable(VIO * vio);
00131
00132 int64_t connection_id() const {
00133 return this->con_id;
00134 }
00135
00136 private:
00137
00138 Http2ClientSession(Http2ClientSession &);
00139 Http2ClientSession& operator=(const Http2ClientSession &);
00140
00141 int main_event_handler(int, void *);
00142
00143 int state_read_connection_preface(int, void *);
00144 int state_start_frame_read(int, void *);
00145 int state_complete_frame_read(int, void *);
00146
00147 int64_t con_id;
00148 SessionHandler session_handler;
00149 NetVConnection * client_vc;
00150 MIOBuffer * read_buffer;
00151 IOBufferReader * sm_reader;
00152 MIOBuffer * write_buffer;
00153 IOBufferReader * sm_writer;
00154 Http2FrameHeader current_hdr;
00155 Http2ConnectionState connection_state;
00156 };
00157
00158 extern ClassAllocator<Http2ClientSession> http2ClientSessionAllocator;
00159
00160 #endif // __HTTP2_CLIENT_SESSION_H__