File Transfer using gRPC ++

Viewed 1783

My objective is to chunk a file and then use a streaming connection to send the file to a server using gRPC++. Is there anything wrong in my code? The RPC call for file upload does not seem to be successful.

My proto file has a single object "bytes"

message FileContent {

  bytes content = 1;

}

Client Code

char *buffer = new char[2048];
// Open a stream-based connection with the gRPC server

std::unique_ptr<ClientWriter<FileContent> > writer(this->service_stub->Store(&context, &fileack));
// send the file name to the server

filecontent.set_content(filename);
std::cout << "Client: RPC call with File Name:" << filecontent.content() << endl;
writer->Write(filecontent);

// Get a file handle for the file we want to upload and the file length 
fileStream.open(filename, ios::binary);
while (!fileStream.eof())
{
     std::this_thread::sleep_for(std::chrono::milliseconds(100));
     filecontent.clear_content();
     fileStream.read(buffer,2048);
     filecontent.set_content(std::string(buffer, 2048));
     writer->Write(filecontent);
}

Server Code

   Status Store(ServerContext* context, ServerReader<FileContent>* reader, FileAck* fileack) override 
{
         FileContent filecontent;
         ofstream fileStream;
         std::string serverfilepath;
         if (fileStream.is_open())
         {
             std::cout << "Reading Data";
             while (reader->Read(&filecontent))
             {
                 fileStream << filecontent.mutable_content();
             }

               fileStream.close();

         }

         else
         {
            reader->Read(&filecontent);
            fileStream.open(serverfilepath, ios::binary);
         }

         return Status::OK;
        }

The 1st RPC call for sending the file name works, but subsequent RPC calls

are not successful. I don't see the server receiving any data. Does my code look OK?

0 Answers
Related