The std::string, which is used as the container for the grpc datatype 
"bytes", can handle embedded nul bytes.
But In your "Put" method you pass the parameter "val" as char * pointer.
This char * pointer will be converted to a std::string terminating with the 
first occurrence of the nul byte.

So must either past the value "val" as std::string or std::vector<char> 

HTH
Tobias

On Thursday, December 3, 2020 at 4:59:05 PM UTC+1 Joseph Liu wrote:

> So I'm using gRPC to store data in a key-value store.
>
> The protos look like this:
>
>     syntax = "proto3";
>     
>     package keyvaluestore;
>     
>     service KeyValueStore {
>       rpc AddUser(Credentials) returns (Response) {}
>       rpc Get(Request) returns (Response) {}
>       rpc Put(Request) returns (Response) {}
>       rpc Cput(Request) returns (Response) {}
>       rpc Delete(Request) returns (Response) {}
>     }
>     
>     message Credentials {
>       string user = 1;
>       string passwd = 2;
>     }
>     
>     message Request {
>       string user = 1;
>       string key = 2;
>       bytes val = 3;
>       bytes val2 = 4;
>       string addr = 5;
>     }
>     
>     message Response {
>       bytes val = 1;
>       uint32 nbytes = 2;
>       string message = 3;
>     }
>
> Right now, the issue is that if we send over say, an image as byte data 
> which can include the null byte, then when the server receives it in the 
> `Request` object, it treats it as a string; when it does this it only reads 
> it up the the first null byte.
>
> How we pack the `Request` object on the client side:
>
>     bool KeyValueStoreClient::Put(const string& user, const string& key, 
> const char* val) {
>       Request req;
>       req.set_user(user);
>       req.set_key(key);
>       req.set_val(val);
>     
>       ClientContext ctx;
>       Response res;
>       Status status = stub_->Put(&ctx, req, &res);
>     }
>
> Server receives `req->val()` as a string instead of `char*`:
>
>     Status KeyValueStoreServiceImpl::Put(ServerContext* ctx, const 
> Request* req, Response* res) {
>       // req->val() is a string
>     
>     }
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/a2c7ddd8-3ad6-4dc4-8c31-9ba683b208cbn%40googlegroups.com.

Reply via email to