| 329 | } |
| 330 | |
| 331 | bool redis_pubsub::get_message(string& channel, string& msg, |
| 332 | string* message_type /* = NULL */, string* pattern /* = NULL */, |
| 333 | int timeout /* = -1 */) |
| 334 | { |
| 335 | clear_request(); |
| 336 | int rw_timeout = -1; |
| 337 | const redis_result* result = run(0, timeout >= 0 ? &timeout : &rw_timeout); |
| 338 | if (result == NULL) { |
| 339 | return false; |
| 340 | } |
| 341 | if (result->get_type() != REDIS_RESULT_ARRAY) { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | size_t size = result->get_size(); |
| 346 | if (size < 3) { |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | const redis_result* obj = result->get_child(0); |
| 351 | if (obj == NULL || obj->get_type() != REDIS_RESULT_STRING) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | string tmp; |
| 356 | obj->argv_to_string(tmp); |
| 357 | if (message_type) { |
| 358 | *message_type = tmp; |
| 359 | } |
| 360 | if (tmp.equal("message", true)) { |
| 361 | if (pattern) { |
| 362 | pattern->clear(); |
| 363 | } |
| 364 | |
| 365 | obj = result->get_child(1); |
| 366 | if (obj == NULL || obj->get_type() != REDIS_RESULT_STRING) { |
| 367 | return false; |
| 368 | } else { |
| 369 | obj->argv_to_string(channel); |
| 370 | } |
| 371 | |
| 372 | obj = result->get_child(2); |
| 373 | if (obj == NULL || obj->get_type() != REDIS_RESULT_STRING) { |
| 374 | return false; |
| 375 | } else { |
| 376 | obj->argv_to_string(msg); |
| 377 | } |
| 378 | |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | if (!tmp.equal("pmessage", false)) { |
| 383 | logger_error("unknown message type: %s", tmp.c_str()); |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | if (size < 4) { |
| 388 | logger_error("invalid size: %d, message type: %s", |