Search code examples
c++filefile-iothread-safetyboost-thread

simple server thread for receiving file from a local PC - file temporarily empty


(I'm not a pro)

I'd like some tip on what is going wrong in my code.

The connexion is always with the same computer once the first file is received.

Just after receiving the file I want to open it to modify some variable in my main program. But the file is considered empty in the program. If I stop the program and open the file directly it contain all the data that should be there (not empty). Should I do something more when closing the output file?

modele_vao, and modele_count are variable shared with the main program (that's the reason for the mutex)

Here's the code for the thread:

    void threadservfct()
{

    while (1)
    {

        {
            boost::mutex::scoped_lock lk(m);

            std::wcout << "Server_thread START " << std::endl;
            boost::array<char, 1024> buf;
            size_t file_size = 0;
            boost::asio::io_service io_service;
            boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 13));
            boost::system::error_code error;
            boost::asio::ip::tcp::socket socket(io_service);
            acceptor.accept(socket);
            boost::asio::streambuf request_buf;
            boost::asio::read_until(socket, request_buf, "\n\n");
            std::wcout << "request size:" << request_buf.size() << std::endl;
            std::istream request_stream(&request_buf);
            std::string file_path;
            request_stream >> file_path;
            request_stream >> file_size;
            request_stream.read(buf.c_array(), 2); // eat the "\n\n" 
            size_t pos = file_path.find_last_of("\\");
            if (pos != std::string::npos)
                file_path = file_path.substr(pos + 1);
            std::ofstream output_file(file_path.c_str(), std::ios_base::binary);


            if (!output_file)
            {
                std::wcout << "failed to open " << std::endl;
            }

            // write extra bytes to file
            do
            {
                request_stream.read(buf.c_array(), (std::streamsize)buf.size());
                std::wcout << __FUNCTION__ << " write " << request_stream.gcount() << " bytes" << std::endl;
                output_file.write(buf.c_array(), request_stream.gcount());
            } while (request_stream.gcount()>0);

            for (;;)
            {
                size_t len = socket.read_some(boost::asio::buffer(buf), error);
                if (len>0)
                    output_file.write(buf.c_array(), (std::streamsize)len);
                if (output_file.tellp() == (std::fstream::pos_type)(std::streamsize)file_size)
                    break; // file was received
                if (error)
                {
                    std::wcout << error << std::endl;
                    break;
                }
            }
            std::wcout << "received " << output_file.tellp() << " bytes" << std::endl;


            output_file.close();

            std::wcout << "END TRANSMISSION. Reiceived:" << file_path.c_str() << std::endl;


            std::wcout << "import new mesh" << std::endl;
            assert(load_mesh(file_path.c_str(), &modele_vao, &modele_count));
            std::wcout << "end mesh import" << std::endl;
        }

    }
}

Solution

  • Found the problem, the file I was opening with load_mesh was a .obj that was refering to a mtl file. The mtl file wasnt sent so that's why it didnt worked.

    pretty stupid error