11 using namespace forest;
26 if (listenSock < 0) fatal(
"can't create socket");
28 if (!
Np4d::bind4d(listenSock, INADDR_ANY, 30125)) fatal(
"can't bind socket");
35 std::cout << connSock <<
"connected" << std::endl;
36 if (connSock < 0) fatal(
"error on accept");
48 pthread_attr_t attr; pthread_attr_init(&attr);
49 pthread_attr_setstacksize(&attr,4*PTHREAD_STACK_MIN);
50 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
51 if (pthread_create(&thisThread, &attr, &(
handler), (
void *) sockp) != 0)
52 fatal(
"cannot create thread");
55 void sendFile(
string fileName,
int sock)
57 ifstream pFile (fileName.c_str(), ios::in | ios::binary | ios::ate);
59 ifstream::pos_type size;
66 memblock =
new char [size];
67 pFile.seekg (0, ios::beg);
68 pFile.read(memblock, size);
71 int bufSize = (int)size;
74 ss <<
"success:" << bufSize;
75 string echo = ss.str();
76 while (echo.size() < 14)
82 char * ap = &memblock[0];
85 while(bufSize >= CHUNK)
87 Np4d::sendBufBlock(sock, ap, CHUNK);
94 Np4d::sendBufBlock(sock, ap, bufSize);
103 close(sock);
return NULL;
111 int sock = *((
int *) sockp);
delete ((
int *) sockp);
117 if (buf.readAlphas(s1)) {
118 if (s1 ==
"getAvatar") { pkt_type =
"get"; }
119 else if (s1 ==
"getTexture") { pkt_type =
"getTex"; }
122 Np4d::sendString(sock,
"unrecognized input, should've been a getAvatar request.\noverAndOut\n");
123 close(sock);
return NULL;
128 if (pkt_type ==
"get")
133 if (buf.readAlphas(s2) && s2 !=
""){
135 fileName = string(
"clientAvatars/") + s2 + string(
".zip");
136 std::cout << fileName << std::endl;
137 sendFile(fileName, sock);
143 string tex_file_type;
144 if (buf.readAlphas(s3) && s3 !=
"")
147 case "H": tex_file_type = string(
".png");
148 case "M": tex_file_type = string(
".jpg");
149 case "L": tex_file_type = string(
"_lo.jpg");
150 default: tex_file_type = string(
".jpg");
153 string tex_to_send = string(
"clientTextures/") + s2 + tex_file_type;
154 sendFile(tex_to_send, sock);
160 close(sock);
return NULL;
168 close(sock);
return NULL;
176 close(sock);
return NULL;
185 string tex_file_type;
186 if (buf.readAlphas(s3) && s3 !=
"")
189 case "H": tex_file_type = string(
".png");
190 case "M": tex_file_type = string(
".jpg");
191 case "L": tex_file_type = string(
"_lo.jpg");
192 default: tex_file_type = string(
".jpg");
195 string tex_to_send = string(
"clientTextures/") + s2 + tex_file_type;
196 sendFile(tex_to_send, sock);
202 close(sock);
return NULL;
208 close(sock);
return NULL;