/// <reference path="leaf.d.ts" />
declare namespace Leaf {
    type RPCId = number;
    const RPCErrors: {
        APINotExists: Leaf.CustomErrorConstructor;
        Timeout: Leaf.CustomErrorConstructor;
        Conflict: Leaf.CustomErrorConstructor;
        BrokenConnection: Leaf.CustomErrorConstructor;
    };
    interface RPCCallback<TResult> {
        (error: Error, result: TResult): void;
    }
    interface RPCInvoke<TOption, TResult> {
        (option?: TOption, callback?: RPCCallback<TResult>): void;
        RPC?: RPCDeclare<TOption, TResult>;
    }
    interface RPCOption {
        name: string;
        timeout?: number;
    }
    interface RPCMethodHandler {
        (data: any, callback: {
            (err?: Error, data?: any): void;
        }): void;
    }
    class RPCCenter {
        option: {
            defaultTimeout?: number;
        };
        declares: any;
        requestOffset: number;
        methods: {
            [index: string]: RPCMethodHandler;
        };
        remoteEvents: EventEmitter<string>;
        private connection;
        private responseHandlers;
        customNormalize: {
            (data: any): string;
        };
        customDenormalize: {
            (data: string): any;
        };
        private allocateId();
        constructor(option?: {
            defaultTimeout?: number;
        });
        setConnection(con: RPCConnection): void;
        unsetConnection(): void;
        registerRPCMethod(name: string, handler: RPCMethodHandler): void;
        createRPCDeclare<TOption, TResult>(option: RPCOption): RPCDeclare<TOption, TResult>;
        createRPCInvokeInterface<TOption, TResult>(option: RPCOption): RPCInvoke<TOption, TResult>;
        emitRemoteEvent(name: string, data?: any): void;
        invoke(name: string, data: any, callback?: Function, option?: {
            timeout?: number;
        }): void;
        private send(data);
        private handleMessage(rawString);
        private handleInvoke(req);
        private handleResponse(res);
        private handleEvent(event);
        private transformObject(data, handler);
        private normalize(data);
        private denormalize(data);
        private finishHandler(id, err?, value?);
    }
    class RPCDeclare<TOption, TResult> {
        private center;
        private option;
        constructor(center: RPCCenter, option: RPCOption);
        getFunction(): RPCInvoke<TOption, TResult>;
        invoke(data?: TOption, callback?: RPCCallback<TResult>): void;
    }
    type RPCBaseConnectionMessage = string;
    interface RPCConnection {
        events: EventEmitter<"message" | "close">;
        send: {
            (data: string): void;
        };
    }
    abstract class RPCConnectionBase implements RPCConnection {
        events: EventEmitter<string>;
        abstract send(data: string): void;
    }
    enum RPCProtocolBlobType {
        INVOKE = 1,
        RESPONSE = 2,
        EVENT = 3,
    }
    interface RPCResponseHandler {
        id: RPCId;
        callback: Function;
        timer: any;
        finished: boolean;
    }
    interface RPCProtocolBlob {
        type: RPCProtocolBlobType;
    }
    interface RPCProtocolResponseBlob extends RPCProtocolBlob {
        id: RPCId;
        data?: any;
        error?: any;
        type: RPCProtocolBlobType;
    }
    interface RPCProtocolInvokeBlob extends RPCProtocolBlob {
        id: RPCId;
        method: string;
        data: any;
        type: RPCProtocolBlobType;
    }
    interface RPCProtocolEventBlob extends RPCProtocolBlob {
        name: string;
        data: any;
        type: RPCProtocolBlobType;
    }
}
