Skip to content

Commit

Permalink
refactor http2 client
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed May 8, 2019
1 parent b33074a commit f64874c
Show file tree
Hide file tree
Showing 5 changed files with 660 additions and 700 deletions.
22 changes: 16 additions & 6 deletions examples/coroutine/http2_client.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
<?php
use Swoole\Coroutine as co;

//const TEST = array('get', 'post', 'pipeline');
const TEST = array('pipeline');
const TEST = array('get', 'post', 'pipeline');
//const TEST = array('pipeline');
//const TEST = array('get',);

CO::set(['trace_flags' => SWOOLE_TRACE_HTTP2,
// 'log_level' => SWOOLE_LOG_TRACE,



]);



co::create(function () use ($fp)
{
$cli = new co\Http2\Client('127.0.0.1', 9518);

$cli->set([ 'timeout' => 1]);
$cli->set([ 'timeout' => 1, 'package_max_length' => 1024*1024*8]);
var_dump($cli->connect());

if (in_array('get', TEST))
{
$req = new co\Http2\Request;
$req = new Swoole\Http2\Request;
$req->path = "/index.html";
$req->headers = [
'host' => "localhost",
Expand All @@ -30,7 +40,7 @@

if (in_array('post', TEST))
{
$req2 = new co\Http2\Request;
$req2 = new Swoole\Http2\Request;
$req2->path = "/index.php";
$req2->headers = [
'host' => "localhost",
Expand All @@ -47,7 +57,7 @@

if (in_array('pipeline', TEST))
{
$req3 = new co\Http2\Request;
$req3 = new Swoole\Http2\Request;
$req3->path = "/index.php";
$req3->headers = [
'host' => "localhost",
Expand Down
4 changes: 2 additions & 2 deletions include/coroutine_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class Socket
{
if (unlikely(!read_buffer))
{
read_buffer = swString_new(SW_BUFFER_SIZE_STD);
read_buffer = swString_new(SW_BUFFER_SIZE_BIG);
}
return read_buffer;
}
Expand All @@ -234,7 +234,7 @@ class Socket
{
if (unlikely(!write_buffer))
{
write_buffer = swString_new(SW_BUFFER_SIZE_STD);
write_buffer = swString_new(SW_BUFFER_SIZE_BIG);
}
return write_buffer;
}
Expand Down
57 changes: 2 additions & 55 deletions swoole_http_v2_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef SWOOLE_HTTP_V2_CLIENT_H_
#define SWOOLE_HTTP_V2_CLIENT_H_

#include "php_swoole.h"
#include "php_swoole_cxx.h"
#include "swoole_http.h"

#include "http.h"
Expand Down Expand Up @@ -55,10 +55,7 @@ typedef struct
double timeout;
zval *object;

int read_cid;
// int write_cid; // useless temporarily
uint8_t iowait;
swClient *client;
swoole::coroutine::Socket *client;

nghttp2_hd_inflater *inflater;
nghttp2_hd_deflater *deflater;
Expand Down Expand Up @@ -87,55 +84,5 @@ static sw_inline void http2_client_init_gzip_stream(http2_client_stream *stream)
}
#endif

static sw_inline void http2_client_send_setting(swClient *cli, swHttp2_settings *settings)
{
uint16_t id = 0;
uint32_t value = 0;

char frame[SW_HTTP2_FRAME_HEADER_SIZE + 18];
memset(frame, 0, sizeof(frame));
swHttp2_set_frame_header(frame, SW_HTTP2_TYPE_SETTINGS, 18, 0, 0);

char *p = frame + SW_HTTP2_FRAME_HEADER_SIZE;
/**
* MAX_CONCURRENT_STREAMS
*/
id = htons(SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
memcpy(p, &id, sizeof(id));
p += 2;
value = htonl(settings->max_concurrent_streams);
memcpy(p, &value, sizeof(value));
p += 4;
/**
* MAX_FRAME_SIZE
*/
id = htons(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE);
memcpy(p, &id, sizeof(id));
p += 2;
value = htonl(settings->max_frame_size);
memcpy(p, &value, sizeof(value));
p += 4;
/**
* INIT_WINDOW_SIZE
*/
id = htons(SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE);
memcpy(p, &id, sizeof(id));
p += 2;
value = htonl(settings->window_size);
memcpy(p, &value, sizeof(value));
p += 4;

swTraceLog(SW_TRACE_HTTP2, "[" SW_ECHO_GREEN "]\t[length=%d]", swHttp2_get_type(SW_HTTP2_TYPE_SETTINGS), 18);
cli->send(cli, frame, SW_HTTP2_FRAME_HEADER_SIZE + 18, 0);
}

static sw_inline void http2_client_send_window_update(swClient *cli, int stream_id, uint32_t size)
{
char frame[SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_WINDOW_UPDATE_SIZE];
swTraceLog(SW_TRACE_HTTP2, "[" SW_ECHO_YELLOW "] stream_id=%d, size=%d", "WINDOW_UPDATE", stream_id, size);
*(uint32_t*) ((char *)frame + SW_HTTP2_FRAME_HEADER_SIZE) = htonl(size);
swHttp2_set_frame_header(frame, SW_HTTP2_TYPE_WINDOW_UPDATE, SW_HTTP2_WINDOW_UPDATE_SIZE, 0, stream_id);
cli->send(cli, frame, SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_WINDOW_UPDATE_SIZE, 0);
}

#endif
Loading

0 comments on commit f64874c

Please sign in to comment.