-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCP_client.cpp
176 lines (144 loc) · 5.25 KB
/
TCP_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "player.h"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <boost/regex.hpp>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h> // "close"
// create initial request in ICY protocol
std::string create_request(std::string path, bool md) {
return "GET " + path + " HTTP/1.0\r\n" +
"Icy-MetaData:" + (md ? '1' : '0') + "\r\n" +
"\r\n";
}
// erases not needed header data
std::string eliminate_initial_response_header(std::string msg) {
const boost::regex header {"ICY.*(\\n|\\r\\n)icy-notice1:.*(\\n|\\r\\n)icy-notice2:.*(\\n|\\r\\n)"};
const std::string replacer = "";
return regex_replace(msg, header, replacer);
}
std::string eliminate_metadata(std::string msg) {
const boost::regex metadata {"icy-name:.*(?:\\n|\\r\\n)(.*(?:\\n|\\r\\n))*icy-br:\\d*(\\n|\\r\\n)(\\n|\\r\\n)"};
const std::string replacer = "";
return regex_replace(msg, metadata, replacer);
}
bool parse_the_metaint(std::string msg) {
const boost::regex reg_ex {"icy-metaint:([1-9]\\d*)"};
boost::smatch matches;
if (regex_search(msg, matches, reg_ex)) {
std::string md_int_as_str = std::string(matches[1].first, matches[1].second);
md_int = std::stoi(md_int_as_str);
std::cerr << "Received md_int is: " << md_int << std::endl;
return true;
}
return false;
}
// true if ok (there was metadata, and its parsed), false otherwise
bool parse_the_title(std::string s) {
size_t pos = s.find(TITLE_STR);
if (pos != std::string::npos) {
pos += TITLE_STR.size();
last_received_title = "";
while (s[pos] != ';') {
last_received_title += s[pos];
pos++;
}
last_received_title.pop_back();
std::cerr << "TITLE: " << last_received_title << std::endl;
return true;
}
return false;
}
void parse_tcp_message(std::string msg) {
static int byte_count = 0; // counts bytes till next metadata
if (!is_md_int_fetched)
if (parse_the_metaint(msg))
is_md_int_fetched = true;
parse_the_title(msg);
std::string msg2 = eliminate_initial_response_header(msg);
std::string msg3;
if (is_md_in_data)
msg3 = eliminate_metadata(msg2);
else
msg3 = msg2;
std::string final_msg = "";
std::string left_msg = "";
//std::cerr << "Byte count: " << byte_count << ", size after cutting: " << msg3.size() << ", size before cutting: " << msg.size() << std::endl;
if (is_md_in_data) {
if (byte_count == md_int) {
// we should have received meta-data
int md_len = int(msg[0]) * 16 + 1;
for (int i = md_len; i < (int)msg3.size(); ++i)
final_msg += msg3[i];
byte_count -= md_int;
std::cerr << "\nGG " << int(msg[0]) << " " << msg.size() << " " << final_msg.size() << " " << byte_count << std::endl;
}
else if (byte_count + (int)msg3.size() < md_int)
final_msg = msg3;
else {
// we cut metadata in the middle
int len = md_int - byte_count;
final_msg = std::string(msg3.begin(), msg3.begin() + len);
left_msg = std::string(msg3.begin() + len, msg3.end());
}
byte_count += final_msg.size();
}
else
final_msg = msg3;
if (is_output_to_file)
output_to_file_stream << final_msg;
else
write(STDOUT_FILENO, final_msg.c_str(), final_msg.size());
if (left_msg.size() > 0)
parse_tcp_message(left_msg);
}
int setup_tcp_client(std::string host, std::string path,
int servPort, bool md) {
struct addrinfo hints, *res;
int sockfd;
int s;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // either ip4 or 6
hints.ai_socktype = SOCK_STREAM; // tcp
// TODO: sprawdzic czy warto/trzeba dodac AI_PASSIVE w hints.flags?
if ((s = getaddrinfo(host.c_str(), std::to_string(servPort).c_str(), &hints, &res)) != 0) {
std::cerr << "Getaddrinfo error in TCP client: " << gai_strerror(s) << std::endl;
return -1;
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0) {
freeaddrinfo(res);
std::cerr << "Socket error during TCP client setup." << std::endl;
return -1;
}
s = connect(sockfd, res->ai_addr, res->ai_addrlen);
if (s < 0) {
freeaddrinfo(res);
close(sockfd);
std::cerr << "Connect error during TCP client setup." << std::endl;
return -1;
}
freeaddrinfo(res);
std::string request = create_request(path, md);
std::cerr << "Request to server:\n" << request << std::endl;
if (send(sockfd, request.c_str(), request.size(), 0) < 0) {
close(sockfd);
std::cerr << "Send error in TCP client setup." << std::endl;
return -1;
}
char buf[MAX_BUF_SIZE];
memset(buf, 0, MAX_BUF_SIZE);
s = recv(sockfd, buf, MAX_BUF_SIZE, 0);
parse_tcp_message(std::string(buf, s));
// sprawdzac -1 czy cos?
return sockfd;
}
void process_tcp_event(int fd, bool is_player_paused) {
char buf[MAX_BUF_SIZE];
memset(buf, 0, MAX_BUF_SIZE);
int len = recv(fd, buf, MAX_BUF_SIZE, 0);
parse_tcp_message(std::string(buf, len));
}