private static void BindingFromStringBinding(RpcHandle handle, String bindingString) { RpcError result = RpcBindingFromStringBinding(bindingString, out handle.Handle); RpcException.Assert(result); }
private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input) { Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length); Ptr <MIDL_STUB_DESC> pStub; if (!handle.GetPtr(out pStub)) { pStub = handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false)); } int szResponse = 0; IntPtr response, result; using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input)) { if (RpcApi.Is64BitProcess) { try { result = NdrClientCall2x64(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle, input.Length, pInputBuffer.Handle, out szResponse, out response); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } } else { using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[10])) { pStack32.Data[0] = handle.Handle.ToInt32(); pStack32.Data[1] = input.Length; pStack32.Data[2] = pInputBuffer.Handle.ToInt32(); pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof(int) * 6); pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof(int) * 8); pStack32.Data[5] = 0; //reserved pStack32.Data[6] = 0; //output: int dwSizeResponse pStack32.Data[8] = 0; //output: byte* lpResponse try { result = NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle); } catch (SEHException ex) { RpcException.Assert(ex.ErrorCode); throw; } szResponse = pStack32.Data[6]; response = new IntPtr(pStack32.Data[8]); } } GC.KeepAlive(pInputBuffer); } int id = 0; if (result == IntPtr.Zero || int.TryParse(result.ToString(), out id)) { byte[] output = new byte[szResponse]; if (szResponse > 0 && response != IntPtr.Zero) { Marshal.Copy(response, output, 0, output.Length); } RpcApi.Free(response); return(output); } RpcApi.Free(response); //RpcException.Assert(result.ToInt32()); Log.Verbose("InvokeRpc.InvokeRpc response on {0}, recieved {1} bytes", handle.Handle, szResponse); return(new byte[0]); }
/// <summary> /// synchronous execution of an RPC request /// </summary> /// <typeparam name="TReq"></typeparam> /// <typeparam name="TResp"></typeparam> /// <param name="callBody"></param> /// <param name="reqArgs"></param> /// <param name="completed"></param> /// <param name="excepted"></param> public IDisposable Request <TReq, TResp>(call_body callBody, TReq reqArgs, Action <TResp> completed, Action <Exception> excepted) { Exception resEx = null; TResp respArgs = default(TResp); rpc_msg reqHeader = new rpc_msg() { xid = 0xF1E2D3C4, body = new body() { mtype = msg_type.CALL, cbody = callBody } }; try { UdpDatagram dtg = new UdpDatagram(); Writer w = Toolkit.CreateWriter(dtg); w.Write(reqHeader); w.Write(reqArgs); byte[] outBuff = dtg.ToArray(); Log.Trace(() => "sending byte dump: " + outBuff.ToDisplay()); _client.Send(outBuff, outBuff.Length, _ep); MessageReader mr = new MessageReader(); Reader r = Toolkit.CreateReader(mr); rpc_msg respMsg; long endTime = Stopwatch.GetTimestamp() + _timeout * System.Diagnostics.Stopwatch.Frequency / 1000; _client.Client.ReceiveTimeout = _timeout; while (true) { IPEndPoint ep = _ep; mr.Bytes = _client.Receive(ref ep); respMsg = r.Read <rpc_msg>(); if (respMsg.xid == reqHeader.xid) { break; } int nextTimeout = (int)((double)((endTime - Stopwatch.GetTimestamp()) * 1000) / Stopwatch.Frequency); if (nextTimeout <= 0) { throw new SocketException((int)SocketError.TimedOut); } else { _client.Client.ReceiveTimeout = nextTimeout; } } Log.Trace(() => "received byte dump: " + mr.Bytes.ToDisplay()); resEx = Toolkit.ReplyMessageValidate(respMsg); if (resEx == null) { respArgs = r.Read <TResp>(); mr.CheckEmpty(); } } catch (Exception ex) { resEx = new RpcException("request error", ex); //FIXME: may be to add more context of header } if (resEx == null) { completed(respArgs); } else { excepted(resEx); } return(null); }