Porting to c++ need advice
I'm porting a (mainly) finished C proxy for Minecraft over to C++, for a variety of reasons, but I can't figure out the best way to approach the issue. The only definite requirements are that two threads must be running concurrently, one using select and monitoring the actual proxy part, and the other dealing with commands that use loops and would lag the network communication.
I've came up with a temporarily working solution using a bunch of nested classes, although this expanded the once-simple network function to this abomination:
Code:
void * proxy::proxy_main (void *empty)
{
struct timeval tv = {DEFAULT_TIMEOUT, 0};
fd_set copy = master_set;
ssize_t size;
unsigned char packet[1028];
while (select(max_fd + 1, ©, NULL, NULL, &tv) > 0) {
if (FD_ISSET(server_fd, ©)) {
out->lock_server();
if ((size = out->parser.packet_len (server_fd)) < 0)
break;
if (out->inet.recv_all(server_fd, packet, size) != size)
break;
out->unlock_server();
out->lock_client();
if (out->parser.server_data_handler(packet))
if (out->inet.send_all(client_fd, packet, size) != size)
break;
out->unlock_client();
FD_CLR(server_fd, ©);
}
if (FD_ISSET(client_fd, ©)) {
out->lock_client();
if ((size = out->parser.packet_len (client_fd)) < 0)
break;
if (out->inet.recv_all(client_fd, packet, size) != size)
break;
out->unlock_client();
out->lock_server();
if (out->parser.client_data_handler(packet))
if (out->inet.send_all(server_fd, packet, size) != size)
break;
out->unlock_server();
FD_CLR(client_fd, ©);
}
copy = master_set;
}
return NULL;
}
I already tried a much simpler solution with no annoying "out->", but that failed as there were too many classes to link together to make the "class <class name> public: <other class>" trick work effectively. I'm new to C++, however, so does anybody have ideas as to how to simplify the project?