• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Http2ClientSession.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   Http2ClientSession.
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
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 // Name                       Edata                 Description
00032 // HTTP2_SESSION_EVENT_INIT   Http2ClientSession *  HTTP/2 session is born
00033 // HTTP2_SESSION_EVENT_FINI   Http2ClientSession *  HTTP/2 session is ended
00034 // HTTP2_SESSION_EVENT_RECV   Http2Frame *          Received a frame
00035 // HTTP2_SESSION_EVENT_XMIT   Http2Frame *          Send this frame
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   // Allocate an IOBufferBlock for this frame. This switches us from using the in-line header
00066   // buffer, to an external buffer block.
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   // Return the writeable buffer space.
00077   IOVec write() {
00078     return make_iovec(this->ioblock->end(), this->ioblock->write_avail());
00079   }
00080 
00081   // Once the frame has been serialized, update the length.
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 &); // noncopyable
00102   Http2Frame& operator=(const Http2Frame &); // noncopyable
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   // Implement ProxyClientSession interface.
00121   void start();
00122   void destroy();
00123   void new_connection(NetVConnection * new_vc, MIOBuffer * iobuf, IOBufferReader * reader, bool backdoor);
00124 
00125   // Implement VConnection interface.
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 &); // noncopyable
00139   Http2ClientSession& operator=(const Http2ClientSession &); // noncopyable
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__

Generated by  doxygen 1.7.1