# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: transport/internet/grpc/config.proto, transport/internet/grpc/encoding/stream.proto
# plugin: python-betterproto2
# This file has been @generated

__all__ = (
    "Config",
    "Hunk",
    "MultiHunk",
    "GrpcServiceStub",
    "GrpcServiceBase",
)

from collections.abc import AsyncIterable, AsyncIterator, Iterable
from dataclasses import dataclass
from typing import TYPE_CHECKING

import betterproto2
import grpclib
from betterproto2.grpc.grpclib_server import ServiceBase

from ......message_pool import default_message_pool

if TYPE_CHECKING:
    import grpclib.server
    from betterproto2.grpc.grpclib_client import MetadataLike
    from grpclib.metadata import Deadline

betterproto2.check_compiler_version("0.5.1")


@dataclass(eq=False, repr=False)
class Config(betterproto2.Message):
    authority: "str" = betterproto2.field(1, betterproto2.TYPE_STRING)

    service_name: "str" = betterproto2.field(2, betterproto2.TYPE_STRING)

    multi_mode: "bool" = betterproto2.field(3, betterproto2.TYPE_BOOL)

    idle_timeout: "int" = betterproto2.field(4, betterproto2.TYPE_INT32)

    health_check_timeout: "int" = betterproto2.field(5, betterproto2.TYPE_INT32)

    permit_without_stream: "bool" = betterproto2.field(6, betterproto2.TYPE_BOOL)

    initial_windows_size: "int" = betterproto2.field(7, betterproto2.TYPE_INT32)

    user_agent: "str" = betterproto2.field(8, betterproto2.TYPE_STRING)


default_message_pool.register_message(
    "xray.transport.internet.grpc.encoding", "Config", Config
)


@dataclass(eq=False, repr=False)
class Hunk(betterproto2.Message):
    data: "bytes" = betterproto2.field(1, betterproto2.TYPE_BYTES)


default_message_pool.register_message(
    "xray.transport.internet.grpc.encoding", "Hunk", Hunk
)


@dataclass(eq=False, repr=False)
class MultiHunk(betterproto2.Message):
    data: "list[bytes]" = betterproto2.field(1, betterproto2.TYPE_BYTES, repeated=True)


default_message_pool.register_message(
    "xray.transport.internet.grpc.encoding", "MultiHunk", MultiHunk
)


class GrpcServiceStub(betterproto2.ServiceStub):
    async def tun(
        self,
        messages: "AsyncIterable[Hunk] | Iterable[Hunk]",
        *,
        timeout: "float | None" = None,
        deadline: "Deadline | None" = None,
        metadata: "MetadataLike | None" = None,
    ) -> "AsyncIterator[Hunk]":
        async for response in self._stream_stream(
            "/xray.transport.internet.grpc.encoding.GRPCService/Tun",
            messages,
            Hunk,
            Hunk,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        ):
            yield response

    async def tun_multi(
        self,
        messages: "AsyncIterable[MultiHunk] | Iterable[MultiHunk]",
        *,
        timeout: "float | None" = None,
        deadline: "Deadline | None" = None,
        metadata: "MetadataLike | None" = None,
    ) -> "AsyncIterator[MultiHunk]":
        async for response in self._stream_stream(
            "/xray.transport.internet.grpc.encoding.GRPCService/TunMulti",
            messages,
            MultiHunk,
            MultiHunk,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        ):
            yield response


class GrpcServiceBase(ServiceBase):
    async def tun(self, messages: "AsyncIterator[Hunk]") -> "AsyncIterator[Hunk]":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def tun_multi(
        self, messages: "AsyncIterator[MultiHunk]"
    ) -> "AsyncIterator[MultiHunk]":
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)

    async def __rpc_tun(self, stream: "grpclib.server.Stream[Hunk, Hunk]") -> None:
        request = stream.__aiter__()
        await self._call_rpc_handler_server_stream(
            self.tun,
            stream,
            request,
        )

    async def __rpc_tun_multi(
        self, stream: "grpclib.server.Stream[MultiHunk, MultiHunk]"
    ) -> None:
        request = stream.__aiter__()
        await self._call_rpc_handler_server_stream(
            self.tun_multi,
            stream,
            request,
        )

    def __mapping__(self) -> "dict[str, grpclib.const.Handler]":
        return {
            "/xray.transport.internet.grpc.encoding.GRPCService/Tun": grpclib.const.Handler(
                self.__rpc_tun,
                grpclib.const.Cardinality.STREAM_STREAM,
                Hunk,
                Hunk,
            ),
            "/xray.transport.internet.grpc.encoding.GRPCService/TunMulti": grpclib.const.Handler(
                self.__rpc_tun_multi,
                grpclib.const.Cardinality.STREAM_STREAM,
                MultiHunk,
                MultiHunk,
            ),
        }
