From 6ee1bbbd1c491f1a6972fd62cf8ab652d4e8a942 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 8 Mar 2024 13:39:53 +0800 Subject: first open-source version --- client_publish.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 client_publish.go (limited to 'client_publish.go') diff --git a/client_publish.go b/client_publish.go new file mode 100644 index 0000000..f80f1ab --- /dev/null +++ b/client_publish.go @@ -0,0 +1,40 @@ +package psmb + +import ( + "bytes" + "fmt" + "github.com/hit-mc/psmb-go/protocol" + "io" +) + +// Publish a message. Panic if client mode is not Publisher. Block if send queue is full. +func (c *Client) Publish(msg []byte) error { + if c.mode.Type() != protocol.ModePublish { + panic(fmt.Errorf("invalid operation")) + } + c.txQueue <- bytesOutboundMessage{ + b: msg, + waitChan: make(chan error), + } + return nil +} + +type OutboundMessage interface { + // getContent return the content reader and length in bytes + getContent() (io.Reader, int64) + // Wait returns a channel which blocks until this message is successfully sent to the server. + Wait() <-chan error +} + +type bytesOutboundMessage struct { + b []byte + waitChan chan error +} + +func (b bytesOutboundMessage) getContent() (io.Reader, int64) { + return bytes.NewReader(b.b), int64(len(b.b)) +} + +func (b bytesOutboundMessage) Wait() <-chan error { + return b.waitChan +} -- cgit v1.2.3