-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
199 lines (149 loc) · 5.81 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <cstdint>
#include <atomic>
#include <thread>
#include <algorithm> // std::random_shuffle
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand
#include <numeric>
#include "lodepng.h"
//TCP foo
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/tcp.h>
#include <fcntl.h>
//hardcoded parameters
unsigned int threadNum = 20;
unsigned int screenWidth = 1920;
unsigned int screenHeight = 1080;
std::string host = "127.0.0.1";//"151.217.40.82"
//std::string host = "151.217.40.82";
//unsigned int port = 1337;//1234
unsigned int port = 1234;//1234
const char hex_lookup[] = "0123456789abcdef";
unsigned int startx = 400;
unsigned int starty = 400;
std::vector<int> sockfds;
std::vector<std::string> imgCmds;//rrggbb -> size 6
std::vector<unsigned char> image; //the raw pixels
unsigned width, height;
std::vector<std::string> iToStr(std::max(screenWidth, screenHeight) + 1);
std::vector<std::string> iToHex(256);
void connectSock(unsigned int tid) {
int numbytes;
struct hostent* he;
struct sockaddr_in their_addr; /* connector's address information */
if((he = gethostbyname(host.c_str())) == NULL) /* get the host info */
herror("gethostbyname");
if((sockfds[tid] = socket(AF_INET, SOCK_STREAM, 0)) == -1)
perror("socket");
their_addr.sin_family = AF_INET; /* host byte order */
their_addr.sin_port = htons(port); /* short, network byte order */
their_addr.sin_addr = *((struct in_addr*)he->h_addr);
bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */
int one = 1;
setsockopt(sockfds[tid], IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
long save_fd = fcntl(sockfds[tid], F_GETFL);
save_fd |= O_NONBLOCK;
fcntl(sockfds[tid], F_SETFL, save_fd);
if(connect(sockfds[tid], (struct sockaddr*)&their_addr, \
sizeof(struct sockaddr)) == -1)
perror("connect");
}
void sendToSocket(unsigned int tid, const char* msg, size_t len) { //unsigned int x, unsigned int y, colorNum c) {
int ret = -1;
unsigned int sent = 0;
do {
ret = send(sockfds[tid], msg + sent, len - sent, MSG_NOSIGNAL | MSG_DONTWAIT);
if(ret == -1) {
if(errno == ECONNRESET || errno == EPIPE)
connectSock(tid);
else if(errno != EAGAIN) {
perror("send");
break;
}
ret = 0;
}
sent += ret;
}
while(len - sent > 0);
}
void sendThread(unsigned int tid, unsigned int from, unsigned int to) {
connectSock(tid);
while(true) {
sendToSocket(tid, imgCmds[tid].c_str(), imgCmds[tid].length());
/*for(unsigned int i = 0; i < 1920; i++) {
for(unsigned int j = 0; j < 1080; j++)
sendToSocket(tid, "PX " + std::to_string(i) + " " + std::to_string(j) + " ffffff\n");
}*/
}
}
int main(int argc, char* argv[]) {
std::srand(unsigned(std::time(0)));
for(int i = 0; i <= std::max(screenWidth, screenHeight); i++)
iToStr[i] = std::to_string(i);
for(int i = 0; i <= 255; i++) {
iToHex[i] = "";
iToHex[i] += hex_lookup[i / 16];
iToHex[i] += hex_lookup[i % 16];
//std::cout << i << " " << iToHex[i] << std::endl;
}
threadNum = std::atoi(argv[2]);
sockfds = std::vector<int>(threadNum);
unsigned error = lodepng::decode(image, width, height, std::string(argv[1]));
std::cout << width << std::endl << height << std::endl;
unsigned int emptyCounter = 0;
for(unsigned int i = 0; i < width * height * 4; i += 4) {
if(image[i + 3] == 0)
emptyCounter++;
}
std::vector<std::string> tempCMD;
unsigned int stored = 0;
for(unsigned int i = 0; i < width * height * 4; i += 4) {
if(image[i + 3] == 0)
continue;
std::string temp = iToHex[image[i]] + iToHex[image[i + 1]] + iToHex[image[i + 2]];
tempCMD.push_back("PX " + iToStr[startx + (i / 4 % width)] + " " + iToStr[starty + (i / 4 / width)] + " " + temp + "\n");
}
for(size_t i = 0; i < threadNum; i++) {
std::random_shuffle(tempCMD.begin(), tempCMD.end());
imgCmds.push_back(std::accumulate(tempCMD.begin(), tempCMD.end(), std::string("")));
}
std::vector<std::thread> consumers;
//sendThread(0, 0, width * height);
unsigned int total = imgCmds.size();
for(size_t i = 0; i < threadNum - 1; i++) {
consumers.push_back(std::thread([ = ]() {
sendThread(i, (total / threadNum)*i, (total / threadNum) * (i + 1));
}));
std::cout << (total / threadNum)*i << " " << (total / threadNum) * (i + 1) << std::endl;
}
consumers.push_back(std::thread([ = ]() {
sendThread(threadNum - 1, (total / threadNum) * (threadNum - 1), total);
}));
std::cout << (total / threadNum) * (threadNum - 1) << " " << total << std::endl;
for(size_t i = 0; i < consumers.size(); i++)
consumers[i].join();
/*for(unsigned int i = 0; i < 1920; i++) {
for(unsigned int j = 0; j < 1080; j++) {
}
//std::pair<unsigned int, unsigned int> xy = iToXY(i);
cmd << std::hex << (image[i] < 16 ? "0" : "") << static_cast<unsigned int>(image[i])
<< (image[i + 1] < 16 ? "0" : "") << static_cast<unsigned int>(image[i + 1])
<< (image[i + 2] < 16 ? "0" : "") << static_cast<unsigned int>(image[i + 2]);
imgCmds.push_back(cmd.str());
cmd.clear();
if(i % 1000 == 0)
std::cout << i << std::endl;
}*/
//for(size_t i = 0; i < width * height; i += 4)
//std::cout << "R: " << static_cast<unsigned int>(image[i]) << std::endl;
//sendThread(0, height * width - 1, 0);
}