Http2Session.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef HTTP2_SESSION_H_
  2. #define HTTP2_SESSION_H_
  3. #ifdef WITH_NGHTTP2
  4. #include "HttpSession.h"
  5. #include "http2def.h"
  6. #include "grpcdef.h"
  7. #include "nghttp2/nghttp2.h"
  8. enum http2_session_state {
  9. HSS_SEND_MAGIC,
  10. HSS_SEND_SETTINGS,
  11. HSS_SEND_PING,
  12. HSS_SEND_HEADERS,
  13. HSS_SEND_DATA_FRAME_HD,
  14. HSS_SEND_DATA,
  15. HSS_SEND_DONE,
  16. HSS_WANT_RECV,
  17. HSS_RECV_SETTINGS,
  18. HSS_RECV_PING,
  19. HSS_RECV_HEADERS,
  20. HSS_RECV_DATA,
  21. };
  22. class Http2Session : public HttpSession {
  23. public:
  24. static nghttp2_session_callbacks* cbs;
  25. nghttp2_session* session;
  26. http2_session_state state;
  27. HttpMessage* submited;
  28. HttpMessage* parsed;
  29. int error;
  30. int stream_id;
  31. int stream_closed;
  32. int frame_type_when_stream_closed;
  33. // http2_frame_hd + grpc_message_hd
  34. // at least HTTP2_FRAME_HDLEN + GRPC_MESSAGE_HDLEN = 9 + 5 = 14
  35. unsigned char frame_hdbuf[32];
  36. Http2Session(http_session_type type = HTTP_CLIENT);
  37. virtual ~Http2Session();
  38. virtual int GetSendData(char** data, size_t* len);
  39. virtual int FeedRecvData(const char* data, size_t len);
  40. virtual int GetState() {
  41. return (int)state;
  42. }
  43. virtual bool WantRecv() {
  44. return state == HSS_WANT_RECV;
  45. }
  46. virtual bool WantSend() {
  47. return state != HSS_WANT_RECV;
  48. }
  49. virtual bool IsComplete() {
  50. return stream_closed && (frame_type_when_stream_closed == HTTP2_DATA || frame_type_when_stream_closed == HTTP2_HEADERS);
  51. }
  52. virtual int GetError() {
  53. return error;
  54. }
  55. virtual const char* StrError(int error) {
  56. //return nghttp2_http2_strerror(error);
  57. return nghttp2_strerror(error);
  58. }
  59. // client
  60. // SubmitRequest -> while(GetSendData) {send} -> InitResponse -> do {recv -> FeedRecvData} while(WantRecv)
  61. virtual int SubmitRequest(HttpRequest* req);
  62. virtual int InitResponse(HttpResponse* res);
  63. // server
  64. // InitRequest -> do {recv -> FeedRecvData} while(WantRecv) -> SubmitResponse -> while(GetSendData) {send}
  65. virtual int InitRequest(HttpRequest* req);
  66. virtual int SubmitResponse(HttpResponse* res);
  67. };
  68. #endif
  69. #endif // HTTP2_SESSION_H_