Changeset 686
- Timestamp:
- 12/19/07 08:34:45 (1 year ago)
- Files:
-
- branches/binary/server/memcached.c (modified) (23 diffs)
- branches/binary/server/memcached.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/binary/server/memcached.c
r674 r686 828 828 } 829 829 830 static void add_bin_header(conn *c, int err, int body_len) {830 static void add_bin_header(conn *c, int err, int hdr_len, int body_len) { 831 831 int i=0; 832 832 uint32_t res_header[BIN_PKT_HDR_WORDS]; … … 846 846 res_header[0] = BIN_RES_MAGIC << 24; 847 847 res_header[0] |= ((0xff & c->cmd) << 16); 848 res_header[0] |= (err << 8); 849 850 res_header[1] = c->opaque; 848 res_header[0] |= err & 0xffff; 849 850 res_header[1] = hdr_len << 24; 851 /* TODO: Support datatype */ 851 852 res_header[2] = body_len; 853 res_header[3] = c->opaque; 852 854 853 855 if(settings.verbose > 1) { 854 fprintf(stderr, "Writing bin response: %08x %08x %08x \n",855 res_header[0], res_header[1], res_header[2] );856 fprintf(stderr, "Writing bin response: %08x %08x %08x %08x\n", 857 res_header[0], res_header[1], res_header[2], res_header[3]); 856 858 } 857 859 … … 893 895 fprintf(stderr, "Writing an error: %s\n", errstr); 894 896 } 895 add_bin_header(c, err, strlen(errstr));897 add_bin_header(c, err, 0, strlen(errstr)); 896 898 add_iov(c, errstr, strlen(errstr)); 897 899 … … 906 908 907 909 /* Form and send a response to a command over the binary protocol */ 908 static void write_bin_response(conn *c, void *d, int dlen) {909 add_bin_header(c, 0, dlen);910 static void write_bin_response(conn *c, void *d, int hlen, int dlen) { 911 add_bin_header(c, 0, hlen, dlen); 910 912 if(dlen > 0) { 911 913 add_iov(c, d, dlen); … … 981 983 memset(responseBuf, ' ', 32); 982 984 responseBuf[32]=0x00; 983 add_delta(it, true, delta, responseBuf);985 add_delta(it, c->cmd == CMD_INCR, delta, responseBuf); 984 986 res = strlen(responseBuf); 985 987 986 988 assert(res > 0); 987 write_bin_response(c, responseBuf, res);989 write_bin_response(c, responseBuf, BIN_INCR_HDR_LEN, res); 988 990 item_remove(it); /* release our reference */ 989 991 } else { … … 999 1001 1000 1002 if(store_item(it, NREAD_SET)) { 1001 write_bin_response(c, responseBuf, res);1003 write_bin_response(c, responseBuf, BIN_INCR_HDR_LEN, res); 1002 1004 } else { 1003 1005 write_bin_error(c, ERR_NOT_STORED, 0); … … 1028 1030 case 1: 1029 1031 /* Stored */ 1030 write_bin_response(c, NULL, 0);1032 write_bin_response(c, NULL, BIN_SET_HDR_LEN, 0); 1031 1033 break; 1032 1034 case 2: … … 1057 1059 if (it) { 1058 1060 int *flags; 1061 uint64_t* identifier; 1059 1062 1060 1063 assert(c->rsize >= MIN_BIN_PKT_LENGTH + 4); … … 1066 1069 1067 1070 /* the length has two unnecessary bytes, and then we write four more */ 1068 add_bin_header(c, 0, it->nbytes - 2 + 4 + (c->cmd == CMD_GETS?8:0));1071 add_bin_header(c, 0, GET_RES_HDR_LEN, it->nbytes - 2 + GET_RES_HDR_LEN); 1069 1072 /* Flags */ 1070 1073 add_iov(c, flags, 4); 1071 /* if it's a gets, add the identifier */ 1072 if(c->cmd == CMD_GETS) { 1073 uint64_t* identifier; 1074 identifier=(uint64_t*)(c->wbuf + MIN_BIN_PKT_LENGTH + 4); 1075 *identifier=swap64((uint32_t)it->cas_id); 1076 add_iov(c, identifier, 8); 1077 } 1074 identifier=(uint64_t*)(c->wbuf + MIN_BIN_PKT_LENGTH + 4); 1075 *identifier=swap64((uint32_t)it->cas_id); 1076 add_iov(c, identifier, 8); 1078 1077 /* bytes minus the CRLF */ 1079 1078 add_iov(c, ITEM_data(it), it->nbytes - 2); … … 1101 1100 switch(c->cmd) { 1102 1101 case CMD_VERSION: 1103 write_bin_response(c, VERSION, strlen(VERSION));1102 write_bin_response(c, VERSION, 0, strlen(VERSION)); 1104 1103 break; 1105 1104 case CMD_FLUSH: … … 1108 1107 settings.oldest_live = current_time - 1; 1109 1108 item_flush_expired(); 1110 write_bin_response(c, NULL, 0 );1109 write_bin_response(c, NULL, 0, 0); 1111 1110 break; 1112 1111 case CMD_NOOP: 1113 write_bin_response(c, NULL, 0 );1112 write_bin_response(c, NULL, 0, 0); 1114 1113 break; 1115 1114 case CMD_SET: … … 1120 1119 bin_read_key(c, bin_reading_set_header, BIN_SET_HDR_LEN); 1121 1120 break; 1122 case CMD_CAS:1123 bin_read_key(c, bin_reading_cas_header, BIN_CAS_HDR_LEN);1124 break;1125 case CMD_GETS:1126 1121 case CMD_GETQ: 1127 1122 case CMD_GET: … … 1132 1127 break; 1133 1128 case CMD_INCR: 1129 case CMD_DECR: 1134 1130 bin_read_key(c, bin_reading_incr_header, BIN_INCR_HDR_LEN); 1135 1131 break; … … 1139 1135 } 1140 1136 1141 static void process_bin_update(conn *c , bool cas) {1137 static void process_bin_update(conn *c) { 1142 1138 char *key; 1143 1139 int nkey; … … 1147 1143 item *it; 1148 1144 int comm; 1149 int hdrlen= cas ? BIN_CAS_HDR_LEN :BIN_SET_HDR_LEN;1145 int hdrlen=BIN_SET_HDR_LEN; 1150 1146 1151 1147 assert(c != NULL); … … 1183 1179 1184 1180 it = item_alloc(key, nkey, flags, realtime(exptime), vlen+2); 1185 if(cas) {1186 it->cas_id = (uint64_t)swap64(*((int64_t*)(c->rbuf + 8)));1187 }1188 1181 1189 1182 if (it == 0) { … … 1198 1191 } 1199 1192 1193 it->cas_id = (uint64_t)swap64(*((int64_t*)(c->rbuf + 8))); 1194 1200 1195 switch(c->cmd) { 1201 1196 case CMD_ADD: 1202 1197 c->item_comm = NREAD_ADD; 1203 1198 break; 1204 case CMD_CAS:1205 c->item_comm = NREAD_CAS;1206 break;1207 1199 case CMD_SET: 1208 1200 c->item_comm = NREAD_SET; … … 1213 1205 default: 1214 1206 assert(0); 1207 } 1208 1209 if(it->cas_id != 0) { 1210 c->item_comm = NREAD_CAS; 1215 1211 } 1216 1212 … … 1263 1259 item_unlink(it); 1264 1260 item_remove(it); /* release our reference */ 1265 write_bin_response(c, NULL, 0 );1261 write_bin_response(c, NULL, 0, 0); 1266 1262 } else { 1267 1263 /* XXX: This is really lame, but defer_delete returns a string */ 1268 1264 char *res=defer_delete(it, exptime); 1269 1265 if(res[0] == 'D') { 1270 write_bin_response(c, NULL, 0 );1266 write_bin_response(c, NULL, 0, 0); 1271 1267 } else { 1272 1268 write_bin_error(c, ERR_OUT_OF_MEMORY, 0); … … 1290 1286 } 1291 1287 if(settings.verbose) { 1292 fprintf(stderr, "Read binary protocol data: %08x %08x %08x\n", 1293 c->bin_header[0], c->bin_header[1], c->bin_header[2]); 1288 fprintf(stderr, "Read binary protocol data: %08x %08x %08x %08x\n", 1289 c->bin_header[0], c->bin_header[1], c->bin_header[2], 1290 c->bin_header[3]); 1294 1291 } 1295 1292 if((c->bin_header[0] >> 24) != BIN_REQ_MAGIC) { … … 1310 1307 1311 1308 c->cmd = (c->bin_header[0] >> 16) & 0xff; 1312 c->keylen = (c->bin_header[0] >> 8) & 0xff;1313 c->opaque = c->bin_header[ 1];1309 c->keylen = c->bin_header[0] & 0xffff; 1310 c->opaque = c->bin_header[3]; 1314 1311 if(settings.verbose > 1) { 1315 1312 fprintf(stderr, … … 1321 1318 switch(c->substate) { 1322 1319 case bin_reading_set_header: 1323 process_bin_update(c, false); 1324 break; 1325 case bin_reading_cas_header: 1326 process_bin_update(c, true); 1320 process_bin_update(c); 1327 1321 break; 1328 1322 case bin_read_set_value: … … 1399 1393 if(settings.verbose > 1) { 1400 1394 fprintf(stderr, "CAS: failure: expected %llu, got %llu\n", 1401 old_it->cas_id, it->cas_id);1395 old_it->cas_id, it->cas_id); 1402 1396 } 1403 1397 stored = 2; branches/binary/server/memcached.h
r674 r686 40 40 41 41 /* Binary protocol stuff */ 42 #define MIN_BIN_PKT_LENGTH 12 43 /* flags:32, expiration:32 */ 44 #define BIN_SET_HDR_LEN 8 45 /* Same as set, but with another 64 bits for the CAS identifier */ 46 #define BIN_CAS_HDR_LEN (BIN_SET_HDR_LEN + 8) 42 #define MIN_BIN_PKT_LENGTH 16 43 /* flags:32, expiration:32, cas:64 */ 44 #define BIN_SET_HDR_LEN 16 47 45 /* incr:64, initial:64, expiration:32 */ 48 46 #define BIN_INCR_HDR_LEN 20 47 /* flags:32, cas:64 */ 48 #define GET_RES_HDR_LEN (4+8) 49 49 /* timeout:32 */ 50 50 #define BIN_DEL_HDR_LEN 4 51 51 #define BIN_PKT_HDR_WORDS (MIN_BIN_PKT_LENGTH/sizeof(uint32_t)) 52 52 53 #define BIN_REQ_MAGIC 0x 0f54 #define BIN_RES_MAGIC 0x f053 #define BIN_REQ_MAGIC 0x80 54 #define BIN_RES_MAGIC 0x80 55 55 56 56 #define CMD_GET 0 … … 60 60 #define CMD_DELETE 4 61 61 #define CMD_INCR 5 62 #define CMD_QUIT 6 63 #define CMD_FLUSH 7 64 #define CMD_GETQ 8 65 #define CMD_NOOP 9 66 #define CMD_VERSION 10 67 68 #define CMD_GETS 50 69 #define CMD_CAS 51 62 #define CMD_DECR 6 63 #define CMD_QUIT 7 64 #define CMD_FLUSH 8 65 #define CMD_GETQ 9 66 #define CMD_NOOP 10 67 #define CMD_VERSION 11 70 68 71 69 #define ERR_UNKNOWN_CMD 0x81
