public static GoString fromString(string str) { byte[] bytes = Encoding.ASCII.GetBytes(str); GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); try { IntPtr ptr = handle.AddrOfPinnedObject(); GoString gstr = new GoString(ptr, str.Length); return(gstr); } finally { if (handle.IsAllocated) { handle.Free(); } } }
public static Server GetServer(string id) { var pnt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Server))); var ret = new Server(); try { Marshal.StructureToPtr(ret, pnt, false); bool success = GetServerInternal(GoString.fromString(id), pnt); if (success) { ret = (Server)Marshal.PtrToStructure(pnt, typeof(Server)); FreeServer(pnt); return(ret); } throw new Exception("failed to get server! with id " + id); } finally { Marshal.FreeHGlobal(pnt); } }
public static T RPC <T>(string serverId, Route route, IMessage msg) { byte[] data = ProtoMessageToByteArray(msg); var ret = new RPCRes(); IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RPCRes))); try { Marshal.StructureToPtr(ret, pnt, false); bool success = SendRPC(GoString.fromString(serverId), route, GoSlice.fromSlice <byte>(data), pnt); if (success) { ret = (RPCRes)Marshal.PtrToStructure(pnt, typeof(RPCRes)); T protoRet = GetProtoMessageFromResponse <T>(ret); FreeRPCRes(pnt); return(protoRet); } throw new Exception("RPC call failed!"); } finally { Marshal.FreeHGlobal(pnt); } }
static extern bool SendRPC(GoString svId, Route route, GoSlice message, IntPtr ret);
static extern bool GetServerInternal(GoString id, IntPtr ret);
public static extern bool ConfigureJaeger(double probability, GoString serviceName);