Exemple #1
0
 public void AddMethod <RequestType, RequestSerializerType, ResponseType, ResponseSerializerType>(string fullMethodName, RpcCallFunc <RequestType, ResponseType> callback)
     where RequestType : class, ProtoBuf.IExtensible
     where RequestSerializerType : ProtoBuf.Meta.TypeModel
     where ResponseType : class, ProtoBuf.IExtensible
     where ResponseSerializerType : ProtoBuf.Meta.TypeModel
 {
     AddMethod <RequestType, RequestSerializerType, ResponseType, ResponseSerializerType>(fullMethodName, null, callback);
 }
Exemple #2
0
        /*
         * package MyPackage;
         * serivce ServiceName
         * {
         *     rpc MethodName(RequestType) returns(ResponseType);
         * }
         *
         * fullMethodName = MyPackage + "." + ServiceName + "." + MethodName
         *
         */
        public void AddMethod <RequestType, RequestSerializerType, ResponseType, ResponseSerializerType>(string fullMethodName, RpcCallFunc <RequestType, ResponseType> method, RpcCallFunc <RequestType, ResponseType> callback)
            where RequestType : class, ProtoBuf.IExtensible
            where RequestSerializerType : ProtoBuf.Meta.TypeModel
            where ResponseType : class, ProtoBuf.IExtensible
            where ResponseSerializerType : ProtoBuf.Meta.TypeModel
        {
            Dictionary <UInt64, object> requests = new Dictionary <UInt64, object>();
            UInt64 transmit_id_ = 0;
            RequestSerializerType  requestSerializer  = Activator.CreateInstance <RequestSerializerType>();
            ResponseSerializerType responseSerializer = Activator.CreateInstance <ResponseSerializerType>();

            sendRequestFunc_[fullMethodName] = (controller, request) =>
            {
                controller.proto.transmit_id           = ++transmit_id_;
                requests[controller.proto.transmit_id] = request as RequestType;

                using (MemoryStream stream = new MemoryStream())
                {
                    requestSerializer.Serialize(stream, request);
                    controller.proto.message = stream.ToArray();
                }
                controller.proto.stub = true;

                writeBufferPending_.Seek(0, SeekOrigin.End);
                serializer_.SerializeWithLengthPrefix(writeBufferPending_, controller.proto, typeof(ControllerProto), ProtoBuf.PrefixStyle.Fixed32BigEndian, 0);
            };

            handleResponseFunc_[fullMethodName] = (controller) =>
            {
                if (!requests.ContainsKey(controller.proto.transmit_id))
                {
                    return;
                }
                RequestType  request  = requests[controller.proto.transmit_id] as RequestType;
                ResponseType response = null;

                requests.Remove(controller.proto.transmit_id);
                using (MemoryStream stream = new MemoryStream(controller.proto.message, 0, controller.proto.message.Length))
                {
                    response = responseSerializer.Deserialize(stream, null, typeof(ResponseType)) as ResponseType;
                }
                callback(controller, request, response);
            };

            handleRequestFunc_[fullMethodName] = (controller) =>
            {
                RequestType  request  = null;
                ResponseType response = Activator.CreateInstance <ResponseType>();
                using (MemoryStream stream = new MemoryStream())
                {
                    request = requestSerializer.Deserialize(stream, null, typeof(RequestType)) as RequestType;
                }

                method(controller, request, response);

                using (MemoryStream stream = new MemoryStream())
                {
                    responseSerializer.Serialize(stream, response);
                    controller.proto.message = stream.ToArray();
                }
                controller.proto.stub = false;

                writeBufferPending_.Seek(0, SeekOrigin.End);
                serializer_.SerializeWithLengthPrefix(writeBufferPending_, controller.proto, typeof(ControllerProto), ProtoBuf.PrefixStyle.Fixed32BigEndian, 0);
            };

            ConnectedCallback.Add(() =>
            {
                requests.Clear();
            });

            DisconnectedCallback.Add(() =>
            {
                requests.Clear();
            });
        }