Skip to main content

An Object-oriented Binary RPC Framework

Project description

xrpc

An Object-oriented Binary RPC Framework

Library Usage

1. define service interface (.xidl)

// Greeter.xidl
class HelloReq {
    Str name
}

class HelloReply {
    Str msg
}

class Greeter {
    HelloReply sayHello(HelloReq req)
    sendFile(self, Stream)
}

2. generate code from xidl

xrpc-gen python Greeter.xidl

will got file Greeter.py

# Greeter.py
    class Greeter 接口定义
    class PrGreeter 代理
    class SrGreeter 服务

3. 实现服务

  1. 继承并实现SrGreeter
class GreeterService(SrGreeter):
    def sayHello(req: HelloReq) -> HelloReply:
        print('hello %s' % req.name)
        return HelloReply('Hello, ', req.name)

    def sendFile(file: Stream) -> None:
        save_dir = '__temp/'
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
        s = FileWriterStream(file, save_dir + file.name)
        self.channel.registerStream(s) # will receive the stream
  1. 继承并实现默认服务SrPeer的查找服务接口
class MyPeerService(SrPeer):
    def getService(name: str) -> IInterface:
        if name == Greeter.__name__:
            return GreeterService(self.channel)
        raise ValueError("service not found!")

4. 启动服务

config = Config()
peer = MyPeerService(config)
conn = WSConnection() # use websocket
async with conn.start(peer, 'greeter') as (ip, port):
    # server started, show qrcode
    qr = qrcode.QRCode()
    qr.border = 1
    qr.add_data("%s://%s:%d" % (conn.protocol, ip, port))
    qr.print_ascii(invert=True)
    await asyncio.Future() # wait forever

示例发布了一个名为greeter的websocket服务, 该服务可在局域网内自动发现

5. 调用服务接口

config = Config()
conn = WSConnection() # use websocket
async with conn.connect('greeter', config) as peer:
    # find service
    obj = await peer.getService(Greeter.__name__)
    greeter = PrGreeter(obj)

    # normal rpc call
    ret = await greeter.sayHello(HelloMsg('Daniel'))
    print(ret.msg) # Hello, Daniel
    
    # Send file
    file = FileReaderStream('file.txt')
    await greeter.sendFile(file) # call sendFile
    await peer.channel.sendStream(file) # send file stream

查找并连接名为greeter的websocket 服务

6. 加密握手协议

config中传入handshake即可配置握手协议. 协议实现HandShakeProtocal

config = Config(handshake=HandShakeV1())

握手和加密方式可自定义

7. 使用其他的连接协议

目前支持使用websocket(WSConnection)和tcp连接协议(TCPConnection), 如果想要自定义协议, 请实现Connection

conn = MyCustomConnection() # use custom connection protocal

8. 局域网发现

默认打开局域网发现功能, 如果想要关闭, 则在启动服务时传入参数discoverable=False

async with conn.start(peer, name, discoverable=False) as (ip, port):
    ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oorpc-0.1.2.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oorpc-0.1.2-py3-none-any.whl (30.0 kB view details)

Uploaded Python 3

File details

Details for the file oorpc-0.1.2.tar.gz.

File metadata

  • Download URL: oorpc-0.1.2.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.10 Darwin/23.1.0

File hashes

Hashes for oorpc-0.1.2.tar.gz
Algorithm Hash digest
SHA256 53d441f03da32613fac147c7ef910b894e0bfaaa182cd3d4caaaea12ffeb486e
MD5 2502eed75b0280282e2f0f7890e4a617
BLAKE2b-256 2685d78309553317810c1a8001007214252fad23dda9c04736363972c1690f4d

See more details on using hashes here.

File details

Details for the file oorpc-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: oorpc-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 30.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.10 Darwin/23.1.0

File hashes

Hashes for oorpc-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e73ca7aea69a9ade9924cd7ad41314de7ad535d7272c39c7e095a7047e3e804d
MD5 02bd3a09c0af7a1921575fb1ad840eb3
BLAKE2b-256 e1a80e414738654c195cc2735f314cb5fa34e41958f0345e9f4edff66bc8e235

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page