コード例 #1
0
ファイル: RpcClient.cs プロジェクト: topfreegames/NPitaya
        static void KickCallback(IntPtr userData, IntPtr err, IntPtr kickAnswerBuf)
        {
            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($"Kick failed: code={pe.Code} message={pe.Message}"));
                return;
            }

            Int32  len;
            IntPtr resData = PitayaCluster.pitaya_buffer_data(kickAnswerBuf, out len);

            var kickAnswer = new KickAnswer();

            kickAnswer.MergeFrom(new CodedInputStream(GetDataFromRawPointer(resData, len)));

            if (!kickAnswer.Kicked)
            {
                context.t.SetException(new Exception($"Kick failed: received kicked=false from server"));
                return;
            }

            context.t.SetResult(true);
        }
コード例 #2
0
ファイル: RpcClient.cs プロジェクト: topfreegames/NPitaya
        static void RpcCallback <T>(IntPtr userData, IntPtr err, IntPtr responseBuf)
        {
            var handle  = GCHandle.FromIntPtr(userData);
            var context = (CallbackContext <T>)handle.Target;

            if (err != IntPtr.Zero)
            {
                var pitayaError = new PitayaError(
                    PitayaCluster.pitaya_error_code(err),
                    PitayaCluster.pitaya_error_message(err));
                context.t.SetException(new PitayaException($"RPC call failed: ({pitayaError.Code}: {pitayaError.Message})"));
                return;
            }

            Int32  len;
            IntPtr resData = PitayaCluster.pitaya_buffer_data(responseBuf, out len);

            byte[] resBytes = GetDataFromRawPointer(resData, len);

            try
            {
                // An RPC message comes encoded as a Protos.Response protobuf message.
                // The data field of the message is going to be either encoded in protobuf or JSON,
                // which is taken care by our serializer variable

                var rpcResponse = new Protos.Response();
                rpcResponse.MergeFrom(resBytes);

                if (rpcResponse.Error == null)
                {
                    T remoteResponse = (T)context.serializer.Unmarshal(rpcResponse.Data.ToByteArray(), typeof(T));
                    context.t.SetResult(remoteResponse);
                }
                else
                {
                    context.t.SetException(new PitayaException(
                                               "rpc failed: code={0} msg={1}",
                                               rpcResponse.Error.Code, rpcResponse.Error.Msg
                                               ));
                }
            }
            catch (Exception e)
            {
                context.t.SetException(new PitayaException(
                                           "failed to parse rpc response: {0} (payload={1})",
                                           e.Message, Encoding.UTF8.GetString(resBytes)
                                           ));
            }
        }