Last active
April 19, 2016 16:24
-
-
Save userid/720664bdbc28caa3174e69e4a7e88e54 to your computer and use it in GitHub Desktop.
douyutv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <sys/socket.h> | |
| #include <netinet/in.h> | |
| #include <arpa/inet.h> | |
| #include <netdb.h> | |
| #include <sys/types.h> | |
| #define BUFFER_SIZE 1024 | |
| #define DANMU_PORT 8601 | |
| #define DANMU_IP "danmu.douyutv.com" | |
| #define USERNAME "username" | |
| #define PASSWORD "passwd" | |
| struct MsgInfo { | |
| int len; | |
| int code; | |
| int magic; | |
| char content[BUFFER_SIZE]; | |
| }; | |
| int sock_conn(int port, const char* addr) { | |
| int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
| struct addrinfo hints, *servinfo, *p; | |
| char sport[5]={0}; | |
| sprintf(sport,"%d", port); | |
| memset(&hints, 0, sizeof(hints)); | |
| hints.ai_family = AF_UNSPEC; | |
| hints.ai_socktype = SOCK_STREAM; | |
| if (getaddrinfo(addr, sport, &hints, &servinfo) != 0) { | |
| fprintf(stderr, "getaddrinfo error\n" ); | |
| exit(1); | |
| } | |
| if (connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen) < 0) { | |
| fprintf(stderr, "connect socket failed, port:%d, addr:%s\n", port, addr); | |
| printf("%d\n",sockfd);exit(1); | |
| return -1; | |
| } | |
| return sockfd; | |
| } | |
| int sock_close(int sockfd) { | |
| close(sockfd); | |
| return 0; | |
| } | |
| int process_danmu(int msg_len, MsgInfo* msg) { | |
| if (msg_len <= 12 || msg->len <= 8) { | |
| printf("msg_len:%d\n", msg_len); | |
| return -1; | |
| } | |
| char* p = strstr(msg->content, "txt@="); | |
| if (!p) return -1; | |
| char* content = p + sizeof("txt@=")-1; | |
| p = strchr(content, '/'); | |
| if (!p) return -1; | |
| *p++ = 0; | |
| p = strstr(msg->content, "nn@="); | |
| char* snick = p + sizeof("nn@=")-1; | |
| p = strchr(snick, '/'); | |
| if (!p) return -1; | |
| *p++ = 0; | |
| printf("%s:\t%s\n", snick, content); | |
| return 0; | |
| } | |
| int douyu_danmu(int sockfd, const char* username, | |
| const char* passwd, int room_id, int group_id) { | |
| MsgInfo msg; | |
| int ct_len = snprintf(msg.content, sizeof(msg.content), | |
| "type@=loginreq/username@%s=/password@=%s/roomid@=%d/ct@=2/", | |
| username, passwd, room_id); | |
| msg.len = ct_len + 1 + sizeof(msg.code) + sizeof(msg.magic); | |
| msg.code = msg.len; | |
| msg.magic = 0x2b1; | |
| send(sockfd, &msg, msg.len+sizeof(msg.len), 0); | |
| recv(sockfd, &msg, sizeof(msg), 0); | |
| printf("recv:%s\n", msg.content); | |
| ct_len = snprintf(msg.content, sizeof(msg.content), | |
| "type@=joingroup/rid@=%d/gid@=%d/", room_id, group_id); | |
| msg.len = ct_len + 1 + sizeof(msg.code) + sizeof(msg.magic); | |
| msg.code = msg.len; | |
| msg.magic = 0x2b1; | |
| send(sockfd, &msg, msg.len+sizeof(msg.len), 0); | |
| int ret = 0; | |
| while (true) { | |
| ret = recv(sockfd, &msg, sizeof(msg), 0); | |
| if (ret <=0 ){ | |
| sleep(1); | |
| } | |
| if (process_danmu(ret, &msg) == -1) { | |
| send(sockfd, &msg, 0, 0); | |
| } | |
| } | |
| return 0; | |
| } | |
| int main(int argc, char** argv) { | |
| if (argc < 2) { | |
| printf("usage: danmu room_id \n"); | |
| return 0; | |
| } | |
| const char* username = argc == 4 ? argv[2] : USERNAME; | |
| const char* passwd = argc == 4 ? argv[3] : PASSWORD; | |
| int room_id = atoi(argv[1]); | |
| int group_id = -9999; | |
| int sockfd = sock_conn(DANMU_PORT, DANMU_IP); | |
| douyu_danmu(sockfd, username, passwd, room_id, group_id); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment