@@ -33,23 +33,18 @@ namespace Socket
3333 ::gnutls_alpn_set_protocols (this ->session, protocols, sizeof (protocols) / sizeof(::gnutls_datum_t ), 0);
3434 }
3535
36- AdapterTls::AdapterTls (const ::gnutls_session_t session) noexcept : session(session)
37- {
38-
39- }
36+ AdapterTls::AdapterTls (const ::gnutls_session_t session) noexcept : session(session) {}
4037
4138 bool AdapterTls::handshake () noexcept
4239 {
4340 int ret;
4441
45- do
46- {
42+ do {
4743 ret = ::gnutls_handshake (this ->session );
4844 }
4945 while (ret < 0 && ::gnutls_error_is_fatal (ret) == 0 );
5046
51- if (ret < 0 )
52- {
47+ if (ret < 0 ) {
5348 Socket sock (this ->get_handle () );
5449
5550 sock.close ();
@@ -66,8 +61,7 @@ namespace Socket
6661 // size_t record_size = ::gnutls_record_get_max_size(this->session);
6762 size_t record_size = length;
6863
69- if (0 == record_size)
70- {
64+ if (0 == record_size) {
7165 return -1 ;
7266 }
7367
@@ -77,25 +71,21 @@ namespace Socket
7771
7872 size_t total = 0 ;
7973
80- while (total < length)
81- {
82- if (record_size > length - total)
83- {
74+ while (total < length) {
75+ if (record_size > length - total) {
8476 record_size = length - total;
8577 }
8678
8779 // const long send_size = ::gnutls_record_send(this->session, reinterpret_cast<const uint8_t *>(buf) + total, record_size);
8880
8981 long send_size = 0 ;
9082
91- do
92- {
83+ do {
9384 sock.nonblock_send_sync ();
9485 }
9586 while (GNUTLS_E_AGAIN == (send_size = ::gnutls_record_send (this ->session , reinterpret_cast <const uint8_t *>(buf) + total, record_size) ) );
9687
97- if (send_size < 0 )
98- {
88+ if (send_size < 0 ) {
9989 return send_size;
10090 }
10191
@@ -105,18 +95,15 @@ namespace Socket
10595 return static_cast <long >(total);
10696 }
10797
108- System::native_socket_type AdapterTls::get_handle () const noexcept
109- {
98+ System::native_socket_type AdapterTls::get_handle () const noexcept {
11099 return static_cast <System::native_socket_type>(::gnutls_transport_get_int (this ->session ) );
111100 }
112101
113- ::gnutls_session_t AdapterTls::get_tls_session () const noexcept
114- {
102+ ::gnutls_session_t AdapterTls::get_tls_session () const noexcept {
115103 return this ->session ;
116104 }
117105
118- Adapter *AdapterTls::copy () const noexcept
119- {
106+ Adapter *AdapterTls::copy () const noexcept {
120107 return new AdapterTls (this ->session );
121108 }
122109
@@ -125,18 +112,23 @@ namespace Socket
125112 // ::gnutls_record_set_timeout(this->session, static_cast<const unsigned int>(timeout.count() ) );
126113
127114 Socket sock (this ->get_handle () );
128- sock.nonblock_recv_sync ();
129115
130- return ::gnutls_record_recv (this ->session , buf, length);
116+ long result;
117+
118+ do {
119+ sock.nonblock_recv_sync ();
120+ result = ::gnutls_record_recv (this ->session , buf, length);
121+ }
122+ while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
123+
124+ return result;
131125 }
132126
133- long AdapterTls::nonblock_send (const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept
134- {
127+ long AdapterTls::nonblock_send (const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept {
135128 return this ->nonblock_send_all (buf, length, timeout);
136129 }
137130
138- void AdapterTls::close () noexcept
139- {
131+ void AdapterTls::close () noexcept {
140132 Socket sock (this ->get_handle () );
141133
142134 // Wait for send all data to client
@@ -148,4 +140,4 @@ namespace Socket
148140
149141 ::gnutls_deinit (this ->session);
150142 }
151- };
143+ }
0 commit comments