99
1010namespace swss
1111{
12- DBConnector::~DBConnector ()
13- {
14- close (m_conn->fd );
15- if (m_conn->connection_type == REDIS_CONN_TCP)
16- free (m_conn->tcp .host );
17- else
18- free (m_conn->unix_sock .path );
19- free (m_conn);
20- }
21-
2212 DBConnector::DBConnector (int dbId, const std::string &hostname, int port, unsigned int timeout) :
2313 m_dbId (dbId)
2414 {
25- m_conn = (redisContext *)calloc (1 , sizeof (redisContext));
26- m_conn->connection_type = REDIS_CONN_TCP;
27- m_conn->tcp .host = strdup (hostname.c_str ());
28- m_conn->tcp .port = port;
29- m_conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
15+ auto conn = (redisContext *)calloc (1 , sizeof (redisContext));
16+ conn->connection_type = REDIS_CONN_TCP;
17+ conn->tcp .host = strdup (hostname.c_str ());
18+ conn->tcp .port = port;
19+ conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
20+ setContext (conn);
3021 }
3122
3223 DBConnector::DBConnector (int dbId, const std::string &unixPath, unsigned int timeout) :
3324 m_dbId (dbId)
3425 {
35- m_conn = (redisContext *)calloc (1 , sizeof (redisContext));
36- m_conn->connection_type = REDIS_CONN_UNIX;
37- m_conn->unix_sock .path = strdup (unixPath.c_str ());
38- m_conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
26+ auto conn = (redisContext *)calloc (1 , sizeof (redisContext));
27+ conn->connection_type = REDIS_CONN_UNIX;
28+ conn->unix_sock .path = strdup (unixPath.c_str ());
29+ conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
30+ setContext (conn);
3931 }
4032
4133 DBConnector::DBConnector (const std::string& dbName, unsigned int timeout, bool isTcpConn)
@@ -46,18 +38,20 @@ namespace swss
4638 m_dbId = swss::SonicDBConfig::getDbId (dbName);
4739 if (isTcpConn)
4840 {
49- m_conn = (redisContext *)calloc (1 , sizeof (redisContext));
50- m_conn->connection_type = REDIS_CONN_TCP;
51- m_conn->tcp .host = strdup (swss::SonicDBConfig::getDbHostname (dbName).c_str ());
52- m_conn->tcp .port = swss::SonicDBConfig::getDbPort (dbName);
53- m_conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
41+ auto conn = (redisContext *)calloc (1 , sizeof (redisContext));
42+ conn->connection_type = REDIS_CONN_TCP;
43+ conn->tcp .host = strdup (swss::SonicDBConfig::getDbHostname (dbName).c_str ());
44+ conn->tcp .port = swss::SonicDBConfig::getDbPort (dbName);
45+ conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
46+ setContext (conn);
5447 }
5548 else
5649 {
57- m_conn = (redisContext *)calloc (1 , sizeof (redisContext));
58- m_conn->connection_type = REDIS_CONN_UNIX;
59- m_conn->unix_sock .path = strdup (swss::SonicDBConfig::getDbSock (dbName).c_str ());
60- m_conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
50+ auto conn = (redisContext *)calloc (1 , sizeof (redisContext));
51+ conn->connection_type = REDIS_CONN_UNIX;
52+ conn->unix_sock .path = strdup (swss::SonicDBConfig::getDbSock (dbName).c_str ());
53+ conn->fd = socket (AF_UNIX, SOCK_DGRAM, 0 );
54+ setContext (conn);
6155 }
6256 }
6357
0 commit comments