Example #1
0
        protected bool RegisterRpcHandler(TaskCode code, string name, RpcRequestHandler handler, UInt64 gpid)
        {
            dsn_rpc_request_handler_t cb = (req, param) =>
            {
                // if handler synchnously processes the incoming request
                // we don't need to add_ref and set owner to true
                // in folloiwng two stmts
                // however, we don't know so we do as follows
                Native.dsn_msg_add_ref(req); // released by RpcReadStream
                var rms = new RpcReadStream(req, true);

                var wms = new RpcWriteStream(Native.dsn_msg_create_response(req));
                handler(rms, wms);
            };

            bool r = Native.dsn_rpc_register_handler(code, name, cb, IntPtr.Zero, gpid);

            Logging.dassert(r, "rpc handler registration failed for " + code.ToString());

            lock (_handlers)
            {
                _handlers.Add(code, cb);
            }
            return(true);
        }
Example #2
0
        protected void Reply(RpcWriteStream response)
        {
            Logging.dassert(response.IsFlushed(),
                            "RpcWriteStream must be flushed after write in the same thread");

            Native.dsn_rpc_reply(response.DangerousGetHandle(), ErrorCode.ERR_OK);
        }
Example #3
0
 public SafeTaskHandle ping2(
     string val, 
     pingCallback callback,
     int timeout_milliseconds = 0, 
     int reply_hash = 0,
     int request_hash = 0,
     RpcAddress server = null)
 {
     RpcWriteStream s = new RpcWriteStream(echoHelper.RPC_ECHO_ECHO_PING,timeout_milliseconds, request_hash);
     s.Write(val);
     s.Flush();
     
     return RpcCallAsync2(
                 server != null ? server : _server, 
                 s,
                 this, 
                 (err, rs) => 
                     { 
                         string resp;
                         rs.Read(out resp);
                         callback(err, resp);
                     },
                 reply_hash
                 );
 }       
Example #4
0
 public static void Write(this RpcWriteStream ws, string val)
 {
     using (BinaryWriter writer = new BinaryWriter(ws))
     {
         writer.Write(val);
     }
 }
Example #5
0
 public void ping(
     string val, 
     pingCallback callback,
     int timeout_milliseconds = 0, 
     int reply_thread_hash = 0,
     ulong request_hash = 0,
     RpcAddress server = null)
 {
     var s = new RpcWriteStream(echoHelper.RPC_ECHO_ECHO_PING, timeout_milliseconds, request_hash);
     s.Write(val);
     s.Flush();
     
     RpcCallAsync(
                 server ?? _server, 
                 s,
                 this, 
                 (err, rs) => 
                     { 
                         string resp;
                         rs.Read(out resp);
                         callback(err, resp);
                     },
                 reply_thread_hash
                 );
 }        
Example #6
0
        //
        // this gives you the task handle so you can wait or cancel
        // the task, with the cost of add/ref the task handle
        //
        public static SafeTaskHandle RpcCallAsync2(
            RpcAddress server,
            RpcWriteStream requestStream,
            Clientlet callbackOwner,
            RpcResponseHandler callback,
            int replyHash = 0
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                            "RpcWriteStream must be flushed after write in the same thread");

            var idx  = GlobalInterOpLookupTable.Put(callback);
            var task = Native.dsn_rpc_create_response_task(
                requestStream.DangerousGetHandle(),
                _c_rpc_response_handler_holder,
                (IntPtr)idx,
                replyHash,
                callbackOwner?.tracker() ?? IntPtr.Zero
                );

            var ret = new SafeTaskHandle(task, idx);

            Native.dsn_rpc_call(server.addr, task);
            return(ret);
        }
Example #7
0
        // no callback
        public static void RpcCallOneWay(
            RpcAddress server,
            RpcWriteStream requestStream
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                            "RpcWriteStream must be flushed after write in the same thread");

            Native.dsn_rpc_call_one_way(server, requestStream.DangerousGetHandle());
        }
Example #8
0
        public static RpcReadStream RpcCallSync(
            RpcAddress server,
            RpcWriteStream requestStream
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                            "RpcWriteStream must be flushed after write in the same thread");

            var respMsg = Native.dsn_rpc_call_wait(server.addr, requestStream.DangerousGetHandle());

            return(IntPtr.Zero == respMsg ? null : new RpcReadStream(respMsg, true));
        }
Example #9
0
        protected bool RegisterRpcHandler(TaskCode code, string name, RpcRequestHandler handler)
        {
            dsn_rpc_request_handler_t cb = (dsn_message_t req, IntPtr param) =>
            {
                RpcReadStream  rms = new RpcReadStream(req, false);
                RpcWriteStream wms = new RpcWriteStream(Native.dsn_msg_create_response(req), false);
                handler(rms, wms);
            };

            bool r = Native.dsn_rpc_register_handler(code, name, cb, IntPtr.Zero);

            Logging.dassert(r, "rpc handler registration failed for " + code.ToString());

            lock (_handlers)
            {
                _handlers.Add(code, cb);
            }
            return(true);
        }
Example #10
0
        public static void RpcCallAsync(
            RpcAddress server,
            RpcWriteStream requestStream,
            Servicelet callbackOwner,
            RpcResponseHandler callback,
            int replyHash = 0
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                            "RpcWriteStream must be flushed after write in the same thread");

            var        idx  = GlobalInterOpLookupTable.Put(callback);
            dsn_task_t task = Native.dsn_rpc_create_response_task(
                requestStream.DangerousGetHandle(),
                _c_rpc_response_handler_holder,
                (IntPtr)idx,
                replyHash
                );

            Native.dsn_rpc_call(server, task, callbackOwner != null ? callbackOwner.tracker() : IntPtr.Zero);
        }
Example #11
0
 // ---------- call echoHelper.RPC_ECHO_ECHO_PING ------------
 // - synchronous 
 public ErrorCode ping(
     string val, 
     out string resp, 
     int timeout_milliseconds = 0, 
     int hash = 0,
     RpcAddress server = null)
 {
     RpcWriteStream s = new RpcWriteStream(echoHelper.RPC_ECHO_ECHO_PING, timeout_milliseconds, hash);
     s.Write(val);
     s.Flush();
     
     var respStream = RpcCallSync(server != null ? server : _server, s);
     if (null == respStream)
     {
         resp = default(string);
         return ErrorCode.ERR_TIMEOUT;
     }
     else
     {
         respStream.Read(out resp);
         return ErrorCode.ERR_OK;
     }
 }
Example #12
0
 public RpcReplier(RpcWriteStream respStream, Marshaller marshal)
 {
     _respStream = respStream;
     _marshaller = marshal;
 }
Example #13
0
        public static RpcReadStream RpcCallSync(
            RpcAddress server,
            RpcWriteStream requestStream
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                "RpcWriteStream must be flushed after write in the same thread");

            IntPtr respMsg = Native.dsn_rpc_call_wait(server, requestStream.DangerousGetHandle());
            if (IntPtr.Zero == respMsg)
            {
                return null;
            }
            else
            {
                return new RpcReadStream(respMsg, true);
            }
        }
Example #14
0
        // no callback
        public static void RpcCallOneWay(
            RpcAddress server,
            RpcWriteStream requestStream
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                "RpcWriteStream must be flushed after write in the same thread");

            Native.dsn_rpc_call_one_way(server, requestStream.DangerousGetHandle());
        }
Example #15
0
        //
        // this gives you the task handle so you can wait or cancel
        // the task, with the cost of add/ref the task handle
        //
        public static SafeTaskHandle RpcCallAsync2(
            RpcAddress server,
            RpcWriteStream requestStream,
            Servicelet callbackOwner,
            RpcResponseHandler callback,
            int replyHash = 0
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                "RpcWriteStream must be flushed after write in the same thread");

            var idx = GlobalInterOpLookupTable.Put(callback);
            dsn_task_t task = Native.dsn_rpc_create_response_task(
                requestStream.DangerousGetHandle(),
                _c_rpc_response_handler_holder,
                (IntPtr)idx,
                replyHash
                );

            var ret = new SafeTaskHandle(task);
            Native.dsn_rpc_call(server, task, callbackOwner != null ? callbackOwner.tracker() : IntPtr.Zero);
            return ret;
        }
Example #16
0
        public static void RpcCallAsync(
            RpcAddress server,
            RpcWriteStream requestStream,
            Clientlet callbackOwner,
            RpcResponseHandler callback,
            int replyHash = 0
            )
        {
            Logging.dassert(requestStream.IsFlushed(),
                "RpcWriteStream must be flushed after write in the same thread");

            var idx = GlobalInterOpLookupTable.Put(callback);
            var task = Native.dsn_rpc_create_response_task(
                requestStream.DangerousGetHandle(),
                _c_rpc_response_handler_holder, 
                (IntPtr)idx, 
                replyHash,
                callbackOwner?.tracker() ?? IntPtr.Zero
                );
            Native.dsn_rpc_call(server.addr, task);
        }