diff options
Diffstat (limited to 'client_publish.go')
-rw-r--r-- | client_publish.go | 40 |
1 files changed, 40 insertions, 0 deletions
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 +} |