summaryrefslogtreecommitdiff
path: root/client_publish.go
diff options
context:
space:
mode:
authorKeuin <[email protected]>2024-03-08 13:39:53 +0800
committerKeuin <[email protected]>2024-03-08 13:42:11 +0800
commit6ee1bbbd1c491f1a6972fd62cf8ab652d4e8a942 (patch)
tree14aec57c64271db5bba33b8bc85f399e499edda8 /client_publish.go
first open-source versionHEADmaster
Diffstat (limited to 'client_publish.go')
-rw-r--r--client_publish.go40
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
+}