Skip to content

Commit

Permalink
Add text mode to yrmcds struct.
Browse files Browse the repository at this point in the history
New error codes:

* YRMCDS_IN_BINARY

    The connection has sent a binary command; therefore
    putting it into text mode is impossible.

* YRMCDS_BAD_KEY

    Key contains white spaces or control characters.
  • Loading branch information
ymmt2005 committed Mar 16, 2016
1 parent 26bf445 commit d56d1be
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
4 changes: 3 additions & 1 deletion connect.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) 2013 Cybozu.
// (C) 2013, 2016 Cybozu.

#include "yrmcds.h"

Expand Down Expand Up @@ -160,6 +160,8 @@ yrmcds_error yrmcds_connect(yrmcds* c, const char* node, uint16_t port) {
c->last_size = 0;
c->decompressed = NULL;
c->invalid = 0;
c->text_mode = 0;
c->rserial = 0;
return YRMCDS_OK;
}

Expand Down
4 changes: 4 additions & 0 deletions strerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const char* yrmcds_strerror(yrmcds_error e) {
return "Received malformed packet";
case YRMCDS_NOT_IMPLEMENTED:
return "Not implemented";
case YRMCDS_IN_BINARY:
return "Connection is fixed for binary protocol";
case YRMCDS_BAD_KEY:
return "Bad key";
default:
return "Unknown error";
};
Expand Down
32 changes: 32 additions & 0 deletions text_mode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// (C) 2016 Cybozu.

#include "yrmcds.h"

#include <errno.h>

yrmcds_error yrmcds_text_mode(yrmcds* c) {
if( c == NULL )
return YRMCDS_BAD_ARGUMENT;

#ifndef LIBYRMCDS_NO_INTERNAL_LOCK
int e = pthread_mutex_lock(&c->lock);
if( e != 0 ) {
errno = e;
return YRMCDS_SYSTEM_ERROR;
}
#endif // ! LIBYRMCDS_NO_INTERNAL_LOCK

yrmcds_error ret = YRMCDS_OK;
if( c->serial != 0 ) {
ret = YRMCDS_IN_BINARY;
goto OUT;
}

c->text_mode = 1;

OUT:
#ifndef LIBYRMCDS_NO_INTERNAL_LOCK
pthread_mutex_unlock(&c->lock);
#endif
return ret;
}
23 changes: 22 additions & 1 deletion yrmcds.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @file yrmcds.h
* libyrmcds public API.
* (C) 2013-2015 Cybozu.
* (C) 2013-2016 Cybozu.
*/

#pragma once
Expand Down Expand Up @@ -37,6 +37,10 @@ typedef struct {
size_t last_size; ///< size of the last response.
char* decompressed; ///< decompressed data.
int invalid; ///< invalid flag.

/* for text mode */
int text_mode; ///< text mode flag.
uint32_t rserial; ///< serial emulation.
} yrmcds;


Expand Down Expand Up @@ -146,6 +150,8 @@ typedef enum {
YRMCDS_COMPRESS_FAILED, ///< LZ4 compression failed.
YRMCDS_PROTOCOL_ERROR, ///< received malformed packet.
YRMCDS_NOT_IMPLEMENTED, ///< the function is not available.
YRMCDS_IN_BINARY, ///< connection is fixed for binary protocol.
YRMCDS_BAD_KEY, ///< bad key.
} yrmcds_error;


Expand Down Expand Up @@ -208,6 +214,21 @@ yrmcds_error yrmcds_close(yrmcds* c);
yrmcds_error yrmcds_shutdown(yrmcds* c);


/**
* Turn on text protocol mode.
* @param c A pointer to ::yrmcds.
* @return 0 if succeeded. Other values indicate an error.
*
* This function puts the connection into text protocol mode.
* \p c should be a newly connected object; if any (binary) request
* has been sent, this function will return an error.
*
* Text protocol mode has overheads and limitations; most notably,
* \p quiet option for command sending functions cannot be enabled.
*/
yrmcds_error yrmcds_text_mode(yrmcds* c);


/**
* Enable/disable (de)compression for large objects.
* @param c A pointer to ::yrmcds.
Expand Down

0 comments on commit d56d1be

Please sign in to comment.