@@ -175,8 +175,11 @@ class RPCEndpoint::EventHandler : public dmlc::Stream {
175175 for (int i = 0 ; i < num_args; ++i) {
176176 int tcode = type_codes[i];
177177 if (tcode == kTVMObjectHandle || tcode == kTVMObjectRValueRefArg ) {
178- LOG (FATAL) << " ValueError: Cannot pass argument " << i << " , type "
179- << args[i].AsObjectRef <ObjectRef>()->GetTypeKey () << " is not supported by RPC" ;
178+ if (!args[i].IsObjectRef <RPCObjectRef>()) {
179+ LOG (FATAL) << " ValueError: Cannot pass argument " << i << " , type "
180+ << args[i].AsObjectRef <ObjectRef>()->GetTypeKey ()
181+ << " is not supported by RPC" ;
182+ }
180183 } else if (tcode == kDLDevice ) {
181184 DLDevice dev = args[i];
182185 ICHECK (!IsRPCSessionDevice (dev)) << " InternalError: cannot pass RPC device in the channel" ;
@@ -219,14 +222,48 @@ class RPCEndpoint::EventHandler : public dmlc::Stream {
219222 this ->Write (cdata);
220223 }
221224
222- void WriteObject (void * obj) { this ->ThrowError (RPCServerStatus::kUnknownTypeCode ); }
223- uint64_t GetObjectBytes (void * obj) {
224- this ->ThrowError (RPCServerStatus::kUnknownTypeCode );
225- return 0 ;
225+ void WriteObject (Object* obj) {
226+ // NOTE: for now all remote object are encoded as RPCObjectRef
227+ // follow the same disco protocol in case we would like to upgrade later
228+ //
229+ // Rationale note: Only handle remote object allows the same mechanism to work for minRPC
230+ // which is needed for wasm and other env that goes through C API
231+ if (obj->IsInstance <RPCObjectRefObj>()) {
232+ auto * ref = static_cast <RPCObjectRefObj*>(obj);
233+ this ->template Write <uint32_t >(kRuntimeRPCObjectRefTypeIndex );
234+ uint64_t handle = reinterpret_cast <uint64_t >(ref->object_handle ());
235+ this ->template Write <int64_t >(handle);
236+ } else {
237+ LOG (FATAL) << " ValueError: Object type is not supported in RPC calling convention: "
238+ << obj->GetTypeKey () << " (type_index = " << obj->type_index () << " )" ;
239+ }
240+ }
241+ uint64_t GetObjectBytes (Object* obj) {
242+ if (obj->IsInstance <RPCObjectRefObj>()) {
243+ return sizeof (uint32_t ) + sizeof (int64_t );
244+ } else {
245+ LOG (FATAL) << " ValueError: Object type is not supported in RPC calling convention: "
246+ << obj->GetTypeKey () << " (type_index = " << obj->type_index () << " )" ;
247+ }
226248 }
227249
228250 void ReadObject (int * tcode, TVMValue* value) {
229- this ->ThrowError (RPCServerStatus::kUnknownTypeCode );
251+ // NOTE: for now all remote object are encoded as RPCObjectRef
252+ // follow the same disco protocol in case we would like to upgrade later
253+ //
254+ // Rationale note: Only handle remote object allows the same mechanism to work for minRPC
255+ // which is needed for wasm and other env that goes through C API
256+ uint32_t type_index;
257+ this ->template Read <uint32_t >(&type_index);
258+ if (type_index == kRuntimeRPCObjectRefTypeIndex ) {
259+ uint64_t handle;
260+ this ->template Read <uint64_t >(&handle);
261+ tcode[0 ] = kTVMObjectHandle ;
262+ value[0 ].v_handle = reinterpret_cast <void *>(handle);
263+ } else {
264+ LOG (FATAL) << " ValueError: Object type is not supported in Disco calling convention: "
265+ << Object::TypeIndex2Key (type_index) << " (type_index = " << type_index << " )" ;
266+ }
230267 }
231268
232269 void MessageDone () {
0 commit comments