Skip to content

Commit 04d51ab

Browse files
authored
Merge pull request #2 from tlexy/develop
Develop
2 parents 5dcc72a + f6010a4 commit 04d51ab

File tree

7 files changed

+96
-5
lines changed

7 files changed

+96
-5
lines changed

rclientpp/core/async_socket_client.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ namespace rcpp {
1111
//FD_ZERO(&_efds);
1212
}
1313

14-
AsyncSocketClient::AsyncSocketClient(int sockfd)
15-
{}
14+
/*AsyncSocketClient::AsyncSocketClient(int sockfd)
15+
{}*/
16+
17+
AsyncSocketClient::~AsyncSocketClient()
18+
{
19+
close();
20+
}
1621

1722
int AsyncSocketClient::sockfd() const
1823
{

rclientpp/core/async_socket_client.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace rcpp {
1616
{
1717
public:
1818
AsyncSocketClient();
19-
AsyncSocketClient(int sockfd);
19+
//AsyncSocketClient(int sockfd);
2020

2121
int sockfd() const;
2222

@@ -30,6 +30,8 @@ namespace rcpp {
3030

3131
void close();
3232

33+
~AsyncSocketClient();
34+
3335
private:
3436
int _sockfd{ -1 };
3537
fd_set _rfds;

rclientpp/core/rclient.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ int RClient::_read_timeout = 30000;
1919
RClient::RClient(const std::string& ipstr, int port)
2020
:_ipstr(ipstr),
2121
_port(port),
22-
_bufptr(std::make_shared<RClientBuffer>(1024)),
22+
_bufptr(std::make_shared<RClientBuffer>(1024*1024)),
2323
_map_parser(std::make_shared<MapParser>()),
2424
_array_parser(std::make_shared<ArrayParser>()),
2525
_aclient(std::make_shared<AsyncSocketClient>())
2626
{
27-
_strerr.resize(128, '\0');
27+
_strerr.resize(256, '\0');
2828
}
2929

3030
int RClient::connect(const std::string& username, const std::string& auth, int timeoutms)
3131
{
32+
_user = username;
33+
_pass = auth;
3234
bool flag = _aclient->connect(_ipstr.c_str(), _port, timeoutms);
3335
if (!flag)
3436
{
@@ -52,6 +54,7 @@ int RClient::connect(const std::string& username, const std::string& auth, int t
5254

5355
int RClient::connect(const std::string& auth, int timeoutms)
5456
{
57+
_pass = auth;
5558
bool flag = _aclient->connect(_ipstr.c_str(), _port, timeoutms);
5659
if (!flag)
5760
{
@@ -91,6 +94,20 @@ int RClient::connect(int timeoutms)
9194
return _sock_param();
9295
}
9396

97+
int RClient::reconnect(int timeoutms)
98+
{
99+
_aclient = std::make_shared<AsyncSocketClient>();
100+
if (_user.size() == 0 && _pass.size() == 0)
101+
{
102+
return connect(timeoutms);
103+
}
104+
if (_pass.size() > 0 && _user.size() > 0)
105+
{
106+
return connect(_user, _pass, timeoutms);
107+
}
108+
return connect(_pass, timeoutms);
109+
}
110+
94111
int RClient::_sock_param()
95112
{
96113
/*int ret = sockets::SetRecvTimeout(_sockfd, _read_timeout);
@@ -166,6 +183,11 @@ std::string RClient::strerror()
166183
return _strerr;
167184
}
168185

186+
void RClient::set_error_str(const std::string& str)
187+
{
188+
_strerr = str;
189+
}
190+
169191
void RClient::set_read_timeout(int millisec)
170192
{
171193
_read_timeout = millisec;
@@ -407,6 +429,16 @@ std::shared_ptr<BaseValue> RClient::get_results(int& ret_code)
407429
//ret_code = PARSE_FORMAT_ERROR;
408430
}
409431
}
432+
else if (text[0] == '>')
433+
{
434+
_bufptr->has_read(1);
435+
result = std::make_shared<RedisComplexValue>(ParserType::Push);
436+
ret = _array_parser->parse(_bufptr, result);
437+
if (ret != 0)
438+
{
439+
//ret_code = PARSE_FORMAT_ERROR;
440+
}
441+
}
410442
else
411443
{
412444
return nullptr;

rclientpp/core/rclient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class RClient
2020
int connect(const std::string& auth, int timeoutms = 10000);
2121
int connect(int timeoutms = 10000);
2222

23+
int reconnect(int timeoutms = 500);
24+
2325
int use_resp3();
2426
int use_resp2();
2527

@@ -38,6 +40,8 @@ class RClient
3840

3941
std::string strerror();
4042

43+
void set_error_str(const std::string&);
44+
4145
private:
4246
int do_connect(const std::string& cmd);
4347

@@ -51,6 +55,9 @@ class RClient
5155

5256
std::string _ipstr;
5357
int _port;
58+
std::string _user;
59+
std::string _pass;
60+
5461
int _err_code;
5562
static int _read_timeout;
5663
std::string _strerr;

rclientpp/core/rclient_def.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ enum class ParserType
134134
BlobError,
135135
Verbatim,
136136
BigNumber,
137+
Push,
137138

138139
};
139140

@@ -144,6 +145,9 @@ class BaseValue
144145
:type_(type)
145146
{}
146147

148+
virtual int64_t get_number() = 0;
149+
virtual std::string get_string() = 0;
150+
147151
ParserType value_type() const
148152
{
149153
return type_;
@@ -172,6 +176,15 @@ class BaseValue
172176
return false;
173177
}
174178

179+
bool is_number() const
180+
{
181+
if (type_ == ParserType::Number)
182+
{
183+
return true;
184+
}
185+
return false;
186+
}
187+
175188
bool is_digit() const
176189
{
177190
if (type_ == ParserType::Number
@@ -233,6 +246,16 @@ class RedisValue : public BaseValue
233246
{
234247
}
235248

249+
virtual int64_t get_number()
250+
{
251+
return u.int_val_;
252+
}
253+
254+
virtual std::string get_string()
255+
{
256+
return str_val_;
257+
}
258+
236259
virtual bool is_ok()
237260
{
238261
if (value_type() == ParserType::SimpleString)
@@ -262,6 +285,15 @@ class RedisComplexValue : public BaseValue
262285
:BaseValue(type)
263286
{
264287
}
288+
virtual int64_t get_number()
289+
{
290+
return 0;
291+
}
292+
293+
virtual std::string get_string()
294+
{
295+
return std::string("");
296+
}
265297
std::list<std::shared_ptr<BaseValue>> results;
266298
int count{0};
267299
};

rclientpp/ds/rd.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ void Rd::set_connection(std::shared_ptr<RClient> connection)
1313
_client = connection;
1414
}
1515

16+
void Rd::set_err_str(const std::string& str)
17+
{
18+
_client->set_error_str(str);
19+
}
20+
21+
std::string Rd::get_err_str()
22+
{
23+
return _client->strerror();
24+
}
25+
1626
std::shared_ptr<BaseValue> Rd::redis_command(const char* cmd, int len, int& ret_code)
1727
{
1828
int ret = _client->command(cmd, len);

rclientpp/ds/rd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class Rd
2323
bool get_boolean(const std::string& cmd);
2424
//int get_integer(const std::string& cmd);
2525

26+
void set_err_str(const std::string&);
27+
std::string get_err_str();
28+
2629
protected:
2730
std::shared_ptr<RClient> _client;
2831
std::string _crlf;

0 commit comments

Comments
 (0)