转自:http://blog.csdn.net/sunnydogzhou/article/details/6430485
做Ministored的时候发现一个问题,用http传输二进制的数据时,需要将二进制做一下转化,出现了以下的问题。
简单概括如下:
1) 不知道传输字节的具体长度,如传输的int类型,将int类型之间转为char以后,丢失掉了长度的信息,如数字1234567,本来只有4个字节,但是转化成文本的“1234567”是有7个字节。在int类型的时候固然好办,但是一个数组的时候,经过转化以后,在转化回来就很麻烦了。
2) 对于一些数字,二进制传输Server是没法处理的。如int 1,二进制数据是0x00000001,按字节传输的时候,client能够正常发送,但是libevent收到以后,在抛给libevent_http层是,会把数据截断,前两位0x00是字符串的停止符。
所以,只能用base64来解决这个问题。base64将二进制的内容转化成一组有意义的字符串,然后传输,server在decode。比较不错的是,base64 encode以后,数据包只会增大1/3,效率还是不错的。
下面是base64的实现,及测试代码,声明下base64的encode/decode不是我写的,来自网友的实现,google/baidu下有很多的实现,我拿了一个看起来比较clean的code:)
- #include <stdio.h>
- #include <string.h>
- long int encode( char *src,long int src_len, char *dst)
-
- {
-
- long int i = 0, j = 0;
-
- char base64_map[65] = "BADCFEGHIJKLMNOPQRSTUVWXYZbadcfeghijklmnopqrstuvwxyz0123456789+/";
-
- for (; i < src_len - src_len % 3; i += 3) {
-
- dst[j++] = base64_map[(src[i] >> 2) & 0x3F];
-
- dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];
-
- dst[j++] = base64_map[((src[i + 1] << 2) & 0x3C) + ((src[i + 2] >> 6) & 0x3)];
-
- dst[j++] = base64_map[src[i + 2] & 0x3F];
- }
-
- if (src_len % 3 == 1) {
-
- dst[j++] = base64_map[(src[i] >> 2) & 0x3F];
-
- dst[j++] = base64_map[(src[i] << 4) & 0x30];
-
- dst[j++] = '=';
-
- dst[j++] = '=';
-
- }else if (src_len % 3 == 2) {
-
- dst[j++] = base64_map[(src[i] >> 2) & 0x3F];
-
- dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];
-
- dst[j++] = base64_map[(src[i + 1] << 2) & 0x3C];
-
- dst[j++] = '=';
-
- }
-
- dst[j] = '/0';
- printf("encode length:%ld/n",j);
- return j;
-
- }
- long int decode(char *src, long int src_len, char *dst)
-
- {
-
- long int i = 0, j = 0;
-
- unsigned char base64_decode_map[256] = {
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
-
- 255, 0, 255, 255, 255, 1, 0, 3, 2, 5, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14,
-
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 27, 26, 29,
-
- 28, 31, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
-
- 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
-
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
-
-
-
- for (; i < src_len; i += 4) {
-
- dst[j++] = base64_decode_map[src[i]] << 2 |
-
- base64_decode_map[src[i + 1]] >> 4;
-
- dst[j++] = base64_decode_map[src[i + 1]] << 4 |
-
- base64_decode_map[src[i + 2]] >> 2;
-
- dst[j++] = base64_decode_map[src[i + 2]] << 6 |
-
- base64_decode_map[src[i + 3]];
-
- }
- dst[j] = '/0';
- printf("decode length :%ld/n",j);
- return j;
-
- }
- int main(){
- int num = 11111;
- printf("int:%ld/n",sizeof(int));
- char num_char[100];
- char num_char_b64[200];
- memset(num_char,'/0',100);
- memcpy(num_char,&num,4);
- printf("content_to_be_encoded:%s/n",num_char);
- int len = encode(num_char,4,num_char_b64);
- printf("content_encoded:%s/n",num_char_b64);
- int len2= decode(num_char_b64,len,num_char);
- printf("%s/n",num_char);
- memcpy(&num,num_char,4);
- printf("int:%d/n",num);
- }
搞定了编码以后,明天估计网络框架跟ministore就能联调了,ministored有望!