Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532616
  • 博文数量: 137
  • 博客积分: 3170
  • 博客等级: 中校
  • 技术积分: 1455
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-17 11:47
文章分类

全部博文(137)

文章存档

2015年(2)

2013年(1)

2012年(6)

2011年(5)

2010年(62)

2009年(61)

我的朋友

分类: LINUX

2015-11-06 17:43:12

点击(此处)折叠或打开

  1. #include <lua.h>
  2. #include <lauxlib.h>
  3. #include <lualib.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "skynet_malloc.h"
  7. #include <arpa/inet.h>
  8. #include <time.h>

  9. #include "cJSON.h"
  10. //#define __DEBUG__L_

  11. static int dbmsg(const char* fmt,...) {
  12. #ifdef __DEBUG__L_
  13.     va_list ap;
  14.     va_start(ap, fmt);
  15.     fprintf(stderr, "[LOG]");
  16.     vfprintf(stderr, fmt, ap);
  17.     va_end(ap);
  18. #endif    
  19.     return 0;
  20. }

  21. static int checkdata(cJSON *json){
  22.     cJSON *jtmp, *jtmp1;

  23.     jtmp = cJSON_GetObjectItem(json, "sender");
  24.     if(jtmp == NULL || jtmp->type != cJSON_Number){
  25.         return 1;
  26.     }

  27.     jtmp = cJSON_GetObjectItem(json, "event");
  28.     if(jtmp == NULL || jtmp->type != cJSON_Number){
  29.         return 2;
  30.     }

  31.     jtmp1= cJSON_GetObjectItem(json, "receiver");
  32.     if(jtmp1== NULL || jtmp1->type != cJSON_Object){
  33.         return 3;
  34.     }

  35.     jtmp = cJSON_GetObjectItem(jtmp1, "type");
  36.     if(jtmp == NULL || jtmp->type != cJSON_Number){
  37.         return 4;
  38.     }

  39.     jtmp = cJSON_GetObjectItem(jtmp1, "id");
  40.     if(jtmp == NULL || jtmp->type != cJSON_Number){
  41.         return 5;
  42.     }

  43.     jtmp = cJSON_GetObjectItem(json, "data");
  44.     if(jtmp == NULL){
  45.         return 6;
  46.     }

  47.     return 0;
  48. }

  49. static int encode(lua_State *L){
  50.     if (lua_isnoneornil(L,1)) {
  51.         return 0;
  52.     }

  53.     size_t sz;
  54.     const char * jsonbuf = luaL_checklstring(L,1, &sz);

  55.     cJSON *json = NULL;

  56.     json = cJSON_Parse(jsonbuf);
  57.     if(!json){
  58.         cJSON_Delete(json);
  59.         return luaL_error(L, "json 格式错误 %s", jsonbuf);
  60.     }
  61.     sz = checkdata(json);
  62.     if(sz != 0){
  63.         cJSON_Delete(json);
  64.         return luaL_error(L, "json 数据有问题 %d", sz);
  65.     }


  66.   int version = luaL_checkinteger(L,3);
  67.     dbmsg("%d==>", version);

  68.     cJSON *p;

  69.     char sendbuf[8192]; // 这个长度不够用 怎么办,使用 lua 数组?

  70.     int pos=0;
  71.     int ts;
  72.     int32_t big_int =htonl(version<<24);
  73.     if(version == 1){
  74.         memcpy(sendbuf+pos*sizeof(int32_t), &big_int, sizeof(int32_t));
  75.         pos++;
  76.     }

  77.     p = cJSON_GetObjectItem(json, "sender");
  78.     big_int = htonl(p->valueint);
  79.     memcpy(sendbuf+pos*sizeof(int32_t), &big_int, sizeof(int32_t));
  80.     pos++;
  81.     
  82.     p = cJSON_GetObjectItem(json, "receiver");
  83.     p = cJSON_GetObjectItem(p, "id");
  84.     big_int = htonl(p->valueint);
  85.     memcpy(sendbuf+pos*sizeof(int32_t), &big_int, sizeof(int32_t));
  86.     pos++;

  87.     p = cJSON_GetObjectItem(json, "receiver");
  88.     p = cJSON_GetObjectItem(p, "type");
  89.     big_int = htonl(p->valueint);
  90.     memcpy(sendbuf+pos*sizeof(int32_t), &big_int, sizeof(int32_t));
  91.     pos++;

  92.     p = cJSON_GetObjectItem(json, "event");
  93.     big_int = htonl(p->valueint);
  94.     memcpy(sendbuf+pos*sizeof(int32_t), &big_int, sizeof(int32_t));
  95.     pos++;

  96.     p = cJSON_GetObjectItem(json, "timestamp");
  97.     if(p == NULL){
  98.         ts = time(NULL);
  99.     }else{
  100.         ts = p->valueint;
  101.     }

  102.     if(version == 1){
  103.         big_int = htonl(ts);
  104.         memcpy(sendbuf+pos*sizeof(int32_t), &big_int, sizeof(int32_t));
  105.         pos++;
  106.     }

  107.     sz = pos*sizeof(int32_t);

  108.     p = cJSON_GetObjectItem(json, "data");
  109.     char *ptr = cJSON_PrintUnformatted(p);

  110.     int32_t dlen = (ptr==NULL?0:strlen(ptr));
  111.     
  112.     int i;
  113.     for(i=0; i<(dlen-4); i+=sizeof(int32_t)){
  114.         big_int = htonl(*(int32_t*)(ptr+i)) ^ 0xa5a5a5a5;
  115.         memcpy(sendbuf+sz, &big_int, sizeof(int32_t));
  116.         sz += sizeof(int32_t);
  117.     }

  118.     for(;i<dlen;i++){
  119.         char a = *(ptr+i)^0xa5;
  120.         memcpy(sendbuf+sz, &a, sizeof(char));
  121.         sz += 1;
  122.     }

  123.     free(ptr);
  124.     
  125.     lua_pushlstring(L,sendbuf, sz);
  126.     lua_pushnumber(L, ts);
  127.             
  128.     cJSON_Delete(json);

  129.     return 2;
  130. }

  131. static int decode(lua_State *L){

  132.     if (lua_isnoneornil(L,1)) {
  133.     return 0;
  134.   }
  135.   char * data = lua_touserdata(L,1);
  136.   int sz = luaL_checkinteger(L,2);

  137.     dbmsg("%s %d\n", data+20, sz);

  138.     int32_t ackid = ntohl(*(int32_t*)(data));

  139.     int version = ackid >> 24;
  140.     ackid = ackid & 0x00ffffff;

  141.     int32_t senderid = ntohl(*(int32_t*)(data+sizeof(int32_t)));
  142.     int32_t receiverid = ntohl(*(int32_t*)(data+2*sizeof(int32_t)));
  143.     int32_t receivertype = ntohl(*(int32_t*)(data+3*sizeof(int32_t)));
  144.     int32_t event = ntohl(*(int32_t*)(data+4*sizeof(int32_t)));
  145.     int32_t respts = ntohl(*(int32_t*)(data+5*sizeof(int32_t)));

  146.     int32_t timestamp =0;
  147.     if(version == 1){
  148.         timestamp = time(NULL);
  149. //        timestamp = ntohl(*(int32_t*)(data+5*sizeof(int32_t)));
  150.     }


  151. dbmsg("srte:%d %d %d %d\n", senderid, receiverid, receivertype, event);

  152.     char json[8192]={0};

  153.     strcpy(json, "{\"sender\":");

  154.   sprintf(json+strlen(json), "%d,", senderid);
  155.   strcat(json, "\"receiver\":{\"id\":");
  156.   sprintf(json+strlen(json), "%d,", receiverid);
  157.   strcat(json, "\"type\":");
  158.   sprintf(json+strlen(json), "%d},", receivertype);
  159.   strcat(json, "\"event\":");
  160.   sprintf(json+strlen(json), "%d,", event);

  161.     if(version == 1){
  162.         strcat(json, "\"timestamp\":");
  163.         sprintf(json+strlen(json), "%u,", timestamp);
  164.     }

  165.   strcat(json,"\"data\":");
  166.  // memcpy(json+strlen(json), data+5*sizeof(int32_t), sz-5*sizeof(int32_t));

  167.     int i = 0;
  168.     int len = sz;

  169.     dbmsg("==>%d\n", version);
  170.     if(version == 0){
  171.         i = 5*sizeof(int32_t);
  172.     }else if(version == 1){
  173.         i = 6*sizeof(int32_t);
  174.     }

  175.   for (; i<(len-4); i+=sizeof(int32_t) ){
  176.         dbmsg("%X\n", *(int32_t*)(data+i));
  177.     int32_t big_int = ntohl(*(int32_t*)(data+i)) ^ 0xa5a5a5a5;
  178.     dbmsg("%X %s\n", big_int, (char*)&big_int);
  179.         memcpy(json+strlen(json), &big_int, sizeof(int32_t));
  180.   }

  181.   for(;i<len && i<(sizeof(json)-10); i++){
  182.     char a = *(data+i) ^ 0xa5;
  183.     memcpy(json+strlen(json), &a, 1);
  184.   }
  185.   strcat(json,"}");

  186.     if (i>=(sizeof(json)-10)){
  187.         event = -1;    
  188.     }
  189.     
  190. //    lua_newtable(L);
  191.   if(receivertype == 1){
  192.     lua_pushlstring(L,"group", 5);
  193.   }else {
  194.     lua_pushlstring(L,"send", 4);
  195.   }
  196.     if(event == 92){
  197.         lua_pushlstring(L, "response", 8);
  198.         memset(json, 0, sizeof(json));
  199.         sprintf(json, "%u", respts);
  200.     }

  201. /*
  202.     if (event == 90){
  203.         lua_pushlstring(L,"login", 5);

  204.     }else if(event == 91){
  205.         lua_pushlstring(L,"heartbeat", 9);

  206.     }else if(event == 92){
  207.         lua_pushlstring(L, "response", 8);
  208.         memset(json, 0, sizeof(json));
  209.         sprintf(json, "%u", respts);
  210.     }else if(receivertype == 1){
  211.         lua_pushlstring(L,"group", 5);    
  212.     }else if(event >= 1000 && event <= 4000){
  213.         lua_pushlstring(L,"send", 4);    
  214.     }else{
  215.         lua_pushlstring(L, "error", 5);
  216.     }
  217.  */

  218.     lua_pushnumber(L, ackid);

  219.     dbmsg("1:%d\n", senderid);
  220.     lua_pushnumber(L, senderid);
  221.     dbmsg("2:%d\n", receiverid);
  222.     lua_pushnumber(L, receiverid);
  223.     
  224.     dbmsg("3:%s\n", json);
  225.   lua_pushlstring(L,json, strlen(json));

  226.     dbmsg("4:%d\n", version);

  227.   lua_pushnumber(L, version);

  228.   lua_pushnumber(L, event);
  229. //    lua_rawseti(L, -2, 1);
  230. //
  231. // 这里不必 skynet_free , 因为数据是 skynet_redirect 过来的

  232.   return 7;
  233. }


  234. static const struct luaL_Reg chatencode[] = {

  235.   {"encode" , encode},
  236.   {"decode" , decode},

  237.   {NULL, NULL}

  238. };

  239. int luaopen_chatencode(lua_State *L){

  240.   luaL_newlib(L, chatencode);

  241.     return 1;
  242. }


阅读(4733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~