From 811e8afcd6a9c7f01f1b6bdf6ecd2e767f6407f2 Mon Sep 17 00:00:00 2001 From: zhengchao Date: Tue, 20 Nov 2018 16:42:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3stream=5Fwrite=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=BF=9E=E6=8E=A5=E5=85=B3=E9=97=AD=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=B8=AD=E6=B2=A1=E6=9C=89=E9=80=9A=E7=9F=A5write=5Fctx?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82=20*=20=E5=8E=9F=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=A4=84=E7=90=86stream=5Fwrite=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=97=AD=E4=BA=8B=E4=BB=B6=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=B0=86write=5Fctx=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E6=8C=87=E9=92=88=E7=BD=AE=E4=BD=8D=E3=80=82?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=B8=8A=E5=B1=82=E7=BB=A7=E7=BB=ADfrag=5Fwr?= =?UTF-8?q?ite=E6=97=B6=EF=BC=8C=E6=B2=A1=E6=9C=89=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=BA=95=E5=B1=82=E8=BF=9E=E6=8E=A5=E3=80=82=E7=8E=B0?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform/src/tcp_stream.cpp | 43 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/platform/src/tcp_stream.cpp b/platform/src/tcp_stream.cpp index 4c3f52f..3e9e170 100644 --- a/platform/src/tcp_stream.cpp +++ b/platform/src/tcp_stream.cpp @@ -208,6 +208,11 @@ struct tfe_stream_write_ctx * tfe_stream_write_frag_start(const struct tfe_strea int tfe_stream_write_frag(struct tfe_stream_write_ctx * w_ctx, const unsigned char * data, size_t size) { + if(w_ctx->_stream == NULL) + { + return -EPIPE; + } + struct tfe_conn_private * this_conn = __this_conn(w_ctx->_stream, w_ctx->dir); int ret = 0; if (this_conn != NULL) @@ -224,8 +229,13 @@ int tfe_stream_write_frag(struct tfe_stream_write_ctx * w_ctx, const unsigned ch void tfe_stream_write_frag_end(struct tfe_stream_write_ctx * w_ctx) { - struct tfe_conn_private * this_conn = __this_conn(w_ctx->_stream, w_ctx->dir); - struct tfe_conn_private * peer_conn = __peer_conn(w_ctx->_stream, w_ctx->dir); + struct tfe_conn_private * this_conn; + struct tfe_conn_private * peer_conn; + + /* The connection terminated before this function call */ + if (w_ctx->_stream == NULL) goto __out; + this_conn = __this_conn(w_ctx->_stream, w_ctx->dir); + peer_conn = __peer_conn(w_ctx->_stream, w_ctx->dir); if (this_conn != NULL) { @@ -233,20 +243,18 @@ void tfe_stream_write_frag_end(struct tfe_stream_write_ctx * w_ctx) bufferevent_enable(peer_conn->bev, EV_READ); } - if (w_ctx->_stream != NULL) + if (w_ctx->dir == CONN_DIR_DOWNSTREAM) { - if (w_ctx->dir == CONN_DIR_DOWNSTREAM) - { - assert(w_ctx->_stream->w_ctx_downstream == w_ctx); - w_ctx->_stream->w_ctx_downstream = NULL; - } - else - { - assert(w_ctx->_stream->w_ctx_upstream == w_ctx); - w_ctx->_stream->w_ctx_upstream = NULL; - } + assert(w_ctx->_stream->w_ctx_downstream == w_ctx); + w_ctx->_stream->w_ctx_downstream = NULL; + } + else + { + assert(w_ctx->_stream->w_ctx_upstream == w_ctx); + w_ctx->_stream->w_ctx_upstream = NULL; } +__out: free(w_ctx); } @@ -616,6 +624,7 @@ static void __stream_bev_eventcb(struct bufferevent * bev, short events, void * struct tfe_conn_private ** ref_peer_conn{}; struct ssl_stream ** ref_this_ssl_stream{}; struct ssl_stream ** ref_peer_ssl_stream{}; + struct tfe_stream_write_ctx ** ref_this_write_ctx{}; const char * __str_dir = NULL; if (__bev_dir(_stream, bev) == CONN_DIR_UPSTREAM) @@ -624,6 +633,7 @@ static void __stream_bev_eventcb(struct bufferevent * bev, short events, void * ref_peer_conn = &_stream->conn_downstream; ref_this_ssl_stream = &_stream->ssl_upstream; ref_peer_ssl_stream = &_stream->ssl_downstream; + ref_this_write_ctx = &_stream->w_ctx_upstream; } if (__bev_dir(_stream, bev) == CONN_DIR_DOWNSTREAM) @@ -632,6 +642,7 @@ static void __stream_bev_eventcb(struct bufferevent * bev, short events, void * ref_peer_conn = &_stream->conn_upstream; ref_this_ssl_stream = &_stream->ssl_downstream; ref_peer_ssl_stream = &_stream->ssl_upstream; + ref_this_write_ctx = &_stream->w_ctx_downstream; } if (events & BEV_EVENT_ERROR || events & BEV_EVENT_EOF) @@ -665,6 +676,12 @@ __close_connection: if (*ref_this_conn != NULL) { + /* There is a frag writter setted, need to clear the reference of stream in the writter to indicate the connection is closed */ + if(*ref_this_write_ctx != NULL) + { + (*ref_this_write_ctx)->_stream = NULL; + } + __conn_private_destory_with_ssl(ev_base, *ref_this_conn, *ref_this_ssl_stream); *ref_this_conn = NULL; *ref_this_ssl_stream = NULL;