static void test(struct socket_server *ss) { pthread_t pid; pthread_create(&pid, NULL, _poll, ss); int i = 0; int connid = socket_server_connect(ss, 400, "127.0.0.1", 8888); getc(stdin); socket_server_start(ss, 401, connid); char* tmp[3] = { "hello", "world", "wong" }; int cnt = ARRLEN(tmp); char* contents[ARRLEN(tmp)]; for (i = 0; i < cnt; ++i) { contents[i] = malloc(strlen(tmp[i]) + 1); memcpy(contents[i], tmp[i], strlen(tmp[i])); } for (i = 0; i < cnt; ++i) { socket_server_send(ss, connid, contents[i], strlen(contents[i])); getc(stdin); } printf("done\n"); pthread_join(pid, NULL); }
int server_socket_send(struct server_context *ctx, int id, void *buffer, int sz) { int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz); if (wsz < 0) { server_free(buffer); return -1; } else if (wsz > 1024 * 1024) { int kb4 = wsz / 1024 / 4; if (kb4 % 256 == 0) { server_error(ctx, "%d Mb bytes on socket %d need to send out", (int)(wsz / (1024 * 1024)), id); } } return 0; }
static unsigned __stdcall test(void* p) { int i; for (i=0; i<MAX_CLIENTS; ++i) { clients[i] = socket_server_connect(ss, 100+i, "127.0.0.1", 8888); printf("client id=%d\n", clients[i]); } int j; for (i=0; i<MAX_MSGS; ++i) { for (j=0; j<MAX_CLIENTS; ++j) { socket_server_send(ss, clients[i], data, DEFAULT_MSG_SIZE); } } WaitForSingleObject(hExitEvent, 30000); return 0; }
static unsigned __stdcall test(void* p) { int i; for (i=0; i<MAX_CLIENTS; ++i) { clients[i] = socket_server_connect(ss, 100+i, "127.0.0.1", 8888); } int j; for (i=0; i<MAX_MSGS; ++i) { for (j=0; j<MAX_CLIENTS; ++j) { socket_server_send(ss, clients[i], data, 5000); Sleep(1); } } return 0; }
int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) { int64_t wsz = socket_server_send(SOCKET_SERVER, id, buffer, sz); return check_wsz(ctx, id, buffer, wsz); }
// 协议解析结束 int end_cb(http_parser *p) { // 这里接收到数据,如果是 HTTP 协议还需要取出URL的TEL和SMS的值。 // 并将 SMS 的值转编码为 UniCode 的值。 // 格式: // SMS+TEL+#+DATA // SMS = 短信标识 // TEL = 发送的手机号码 // # = 中文短信分割符 // DATA = UniCode 大端编码 // // 例子:SMS13950065000#4F60597D char* pos; char tel[32] = { 0 }; // 手机号字符串 char sms[2048] = { 0 }; // 短信字符串 if (isURL == 1) { pos = strtok(&buf, "="); if (strcmp(pos, "tel") == 0) { pos = strtok(NULL, "&"); strncpy(tel, pos, strlen(pos)); // 获得手机号码 //printf("tel = %s\n",tel); } pos = strtok(NULL, "="); if (strcmp(pos, "sms") == 0) { pos = strtok(NULL, "\r\n"); strncpy(sms, pos, strlen(pos)); // 获得短信内容 urldecode(&sms); // URL解码 //printf("sms = %s\n",sms); } // 清空 buf memset(buf,0,2048); buf_len = 0; int n = 0; int unicode = 0; char comData[512] = { 0 }; // 清空数组 为0 strcat(comData, "SMS"); // 复制 'SMS' 到字符串中 strcat(comData, tel); // 复制 电话号码 到字符串中 strcat(comData, "#"); // 复制 '#' 到字符串中(中文需要 # 隔开,英文用:隔开 ) int len = strlen(sms); // 获得sms内容的长度 sms[len] = 0x00; sms[len+1] = 0x00; sms[len+2] = 0x00; sms[len+3] = 0x00; int alen = strlen(comData); // 获得发送内容的长度 char StrUni[8]={0}; int UniLen = 0; int i = 0; int j = 0; char Str0[4]={0}; for (; i <= len; ) { n = UTF8toUnicode(&sms[j], &unicode); // 将 UTF8 转成 Unicode 编码 memcpy(&StrUni,&unicode,4); UniLen = strlen(StrUni); // 计算转换后的字符串长度 unicode = htonl(unicode); // 大小端转换 memcpy(&StrUni,&unicode,4); //printf("unicode = %08X\n", unicode); if(n==1) // 返回1表示为ASCII码,需要在前面加0x00。 { strncat((char*) &comData[alen], (char*) &Str0[0], 1); alen += 1; } strncat((char*) &comData[alen], (char*) &StrUni[4-UniLen], UniLen); alen += UniLen; i = i + (4-UniLen); j = j + n; } // for // 发送信息到串口 //cssl_putstring(serial, comData); // 发字符串 cssl_putdata(serial, comData,alen); // 发数据 printf("alen=%d\n",alen); } // if char *tmp = malloc(64); // 分配 tmp 的内存空间 memset(tmp, 0, 64); // 清空内存空间 strcpy(tmp, "HTTP/1.1 200 ok\r\nContent-Length: 2\r\n\r\nOK"); // 复制数据到 tmp socket_server_send(ss, id, tmp, strlen(tmp)); return 0; }