@@ -123,7 +123,7 @@ verify_request::~verify_request(void)
123123
124124shard_connection::shard_connection (unsigned int id, connections_manager* conns_man, benchmark_config* config,
125125 struct event_base * event_base, abstract_protocol* abs_protocol) :
126- m_sockfd(- 1 ), m_address(NULL ), m_port(NULL ), m_unix_sockaddr(NULL ),
126+ m_address(NULL ), m_port(NULL ), m_unix_sockaddr(NULL ),
127127 m_bev(NULL ), m_pending_resp(0 ), m_connection_state(conn_disconnected),
128128 m_authentication(auth_done), m_db_selection(select_done), m_cluster_slots(slots_done) {
129129 m_id = id;
@@ -148,11 +148,6 @@ shard_connection::shard_connection(unsigned int id, connections_manager* conns_m
148148}
149149
150150shard_connection::~shard_connection () {
151- if (m_sockfd != -1 ) {
152- close (m_sockfd);
153- m_sockfd = -1 ;
154- }
155-
156151 if (m_address != NULL ) {
157152 free (m_address);
158153 m_address = NULL ;
@@ -184,7 +179,7 @@ shard_connection::~shard_connection() {
184179 }
185180}
186181
187- void shard_connection::setup_event () {
182+ void shard_connection::setup_event (int sockfd ) {
188183 if (m_bev) {
189184 bufferevent_free (m_bev);
190185 }
@@ -193,10 +188,10 @@ void shard_connection::setup_event() {
193188 if (m_config->openssl_ctx ) {
194189 SSL *ctx = SSL_new (m_config->openssl_ctx );
195190 m_bev = bufferevent_openssl_socket_new (m_event_base,
196- m_sockfd , ctx, BUFFEREVENT_SSL_CONNECTING, 0 );
191+ sockfd , ctx, BUFFEREVENT_SSL_CONNECTING, BEV_OPT_CLOSE_ON_FREE );
197192 } else {
198193#endif
199- m_bev = bufferevent_socket_new (m_event_base, m_sockfd, 0 );
194+ m_bev = bufferevent_socket_new (m_event_base, sockfd, BEV_OPT_CLOSE_ON_FREE );
200195#ifdef USE_TLS
201196 }
202197#endif
@@ -209,47 +204,42 @@ void shard_connection::setup_event() {
209204
210205int shard_connection::setup_socket (struct connect_info * addr) {
211206 int flags;
212-
213- // clean up existing socket
214- if (m_sockfd != -1 )
215- close (m_sockfd);
207+ int sockfd;
216208
217209 if (m_unix_sockaddr != NULL ) {
218- m_sockfd = socket (AF_UNIX, SOCK_STREAM, 0 );
219- if (m_sockfd < 0 ) {
220- return -errno ;
210+ sockfd = socket (AF_UNIX, SOCK_STREAM, 0 );
211+ if (sockfd < 0 ) {
212+ return -1 ;
221213 }
222214 } else {
223215 // initialize socket
224- m_sockfd = socket (addr->ci_family , addr->ci_socktype , addr->ci_protocol );
225- if (m_sockfd < 0 ) {
226- return -errno ;
216+ sockfd = socket (addr->ci_family , addr->ci_socktype , addr->ci_protocol );
217+ if (sockfd < 0 ) {
218+ return -1 ;
227219 }
228220
229221 // configure socket behavior
230222 struct linger ling = {0 , 0 };
231223 int flags = 1 ;
232- int error = setsockopt (m_sockfd , SOL_SOCKET, SO_KEEPALIVE, (void *) &flags, sizeof (flags));
224+ int error = setsockopt (sockfd , SOL_SOCKET, SO_KEEPALIVE, (void *) &flags, sizeof (flags));
233225 assert (error == 0 );
234226
235- error = setsockopt (m_sockfd , SOL_SOCKET, SO_LINGER, (void *) &ling, sizeof (ling));
227+ error = setsockopt (sockfd , SOL_SOCKET, SO_LINGER, (void *) &ling, sizeof (ling));
236228 assert (error == 0 );
237229
238- error = setsockopt (m_sockfd , IPPROTO_TCP, TCP_NODELAY, (void *) &flags, sizeof (flags));
230+ error = setsockopt (sockfd , IPPROTO_TCP, TCP_NODELAY, (void *) &flags, sizeof (flags));
239231 assert (error == 0 );
240232 }
241233
242234 // set non-blocking behavior
243235 flags = 1 ;
244- if ((flags = fcntl (m_sockfd, F_GETFL, 0 )) < 0 ||
245- fcntl (m_sockfd, F_SETFL, flags | O_NONBLOCK) < 0 ) {
246- benchmark_error_log (" connect: failed to set non-blocking flag.\n " );
247- close (m_sockfd);
248- m_sockfd = -1 ;
236+ if ((flags = fcntl (sockfd, F_GETFL, 0 )) < 0 ||
237+ fcntl (sockfd, F_SETFL, flags | O_NONBLOCK) < 0 ) {
238+ close (sockfd);
249239 return -1 ;
250240 }
251241
252- return 0 ;
242+ return sockfd ;
253243}
254244
255245int shard_connection::connect (struct connect_info * addr) {
@@ -258,10 +248,14 @@ int shard_connection::connect(struct connect_info* addr) {
258248 m_db_selection = m_config->select_db ? select_none : select_done;
259249
260250 // setup socket
261- setup_socket (addr);
251+ int sockfd = setup_socket (addr);
252+ if (sockfd < 0 ) {
253+ fprintf (stderr, " Failed to setup socket: %s" , strerror (errno));
254+ return -1 ;
255+ }
262256
263257 // set up bufferevent
264- setup_event ();
258+ setup_event (sockfd );
265259
266260 // set readable id
267261 set_readable_id ();
@@ -282,11 +276,6 @@ int shard_connection::connect(struct connect_info* addr) {
282276}
283277
284278void shard_connection::disconnect () {
285- if (m_sockfd != -1 ) {
286- close (m_sockfd);
287- m_sockfd = -1 ;
288- }
289-
290279 if (m_bev) {
291280 bufferevent_free (m_bev);
292281 }
0 commit comments