Esempio n. 1
0
        static void PushCallback(IntPtr userData, IntPtr err)
        {
            var handle  = GCHandle.FromIntPtr(userData);
            var context = (CallbackContext <bool>)handle.Target;

            if (err != IntPtr.Zero)
            {
                var pe = new PitayaError(
                    PitayaCluster.pitaya_error_code(err),
                    PitayaCluster.pitaya_error_message(err));
                context.t.SetException(new Exception($"Push failed: code={pe.Code}, message={pe.Message}"));
                return;
            }

            context.t.SetResult(true);
        }
Esempio n. 2
0
        public static void Initialize(string envPrefix,
                                      string configFile,
                                      Server serverInfo,
                                      NativeLogLevel logLevel,
                                      NativeLogKind logKind,
                                      Action <string> logFunction,
                                      MetricsConfiguration metricsConfig,
                                      ServiceDiscoveryListener serviceDiscoveryListener = null)
        {
            _serviceDiscoveryListener    = serviceDiscoveryListener;
            _handleRpcCallback           = new HandleRpcCallbackFunc(HandleRpcCallback);
            _clusterNotificationCallback = new ClusterNotificationCallbackFunc(ClusterNotificationCallback);
            _logFunctionCallback         = new LogFunction(LogFunctionCallback);
            var logCtx        = GCHandle.Alloc(logFunction, GCHandleType.Normal);
            var pitayaMetrics = IntPtr.Zero;

            if (metricsConfig.IsEnabled)
            {
                _metricsReporter = new MetricsReporter(metricsConfig);
                pitayaMetrics    = _metricsReporter.GetPitayaPtr();
            }

            IntPtr err = pitaya_initialize_with_nats(
                IntPtr.Zero,
                envPrefix,
                configFile,
                Marshal.GetFunctionPointerForDelegate(_handleRpcCallback),
                Marshal.GetFunctionPointerForDelegate(_clusterNotificationCallback),
                logLevel,
                logKind,
                Marshal.GetFunctionPointerForDelegate(_logFunctionCallback),
                GCHandle.ToIntPtr(logCtx),
                pitayaMetrics,
                serverInfo.Handle,
                out pitaya
                );

            if (err != IntPtr.Zero)
            {
                var pitayaError = new PitayaError(pitaya_error_code(err), pitaya_error_message(err));
                pitaya_error_drop(err);
                throw new PitayaException($"Initialization failed: code={pitayaError.Code} msg={pitayaError.Message}");
            }

            _rpcClient = new RpcClient(pitaya, _serializer);
            _metricsReporter?.Start();
        }