@@ -51,6 +51,13 @@ type MessageClient interface {
5151 Disconnect () (err error )
5252 Reconnect () error
5353 Send (e protocol.ChunkEncoder ) error
54+ SendCompressed (tag string , entries protocol.EntryList ) error
55+ SendCompressedFromBytes (tag string , entries []byte ) error
56+ SendForward (tag string , entries protocol.EntryList ) error
57+ SendMessage (tag string , record interface {}) error
58+ SendMessageExt (tag string , record interface {}) error
59+ SendPacked (tag string , entries protocol.EntryList ) error
60+ SendPackedFromBytes (tag string , entries []byte ) error
5461 SendRaw (raw []byte ) error
5562}
5663
@@ -140,6 +147,62 @@ func (c *Client) connect() error {
140147 return nil
141148}
142149
150+ // Handshake initiates handshake mode. Users must call this before attempting
151+ // to send any messages when the server is configured with a shared key, otherwise
152+ // the server will reject any message events. Successful completion of the
153+ // handshake puts the connection into message (or forward) mode, at which time
154+ // the client is free to send event messages.
155+ func (c * Client ) Handshake () error {
156+ c .sessionLock .RLock ()
157+ defer c .sessionLock .RUnlock ()
158+
159+ if c .session == nil {
160+ return errors .New ("not connected" )
161+ }
162+
163+ var helo protocol.Helo
164+
165+ r := msgp .NewReader (c .session .Connection )
166+ err := helo .DecodeMsg (r )
167+
168+ if err != nil {
169+ return err
170+ }
171+
172+ salt := make ([]byte , 16 )
173+
174+ _ , err = rand .Read (salt )
175+ if err != nil {
176+ return err
177+ }
178+
179+ ping , err := protocol .NewPing (c .Hostname , c .AuthInfo .SharedKey , salt , helo .Options .Nonce )
180+ if err != nil {
181+ return err
182+ }
183+
184+ err = msgp .Encode (c .session .Connection , ping )
185+ if err != nil {
186+ return err
187+ }
188+
189+ var pong protocol.Pong
190+
191+ err = pong .DecodeMsg (r )
192+ if err != nil {
193+ return err
194+ }
195+
196+ if err := protocol .ValidatePongDigest (& pong , c .AuthInfo .SharedKey ,
197+ helo .Options .Nonce , salt ); err != nil {
198+ return err
199+ }
200+
201+ c .session .TransportPhase = true
202+
203+ return nil
204+ }
205+
143206// Connect initializes the Session and Connection objects by opening
144207// a client connect to the target configured in the ConnectionFactory
145208func (c * Client ) Connect () error {
@@ -239,61 +302,69 @@ func (c *Client) Send(e protocol.ChunkEncoder) error {
239302// is not yet in transport phase, an error is returned,
240303// and no message is sent.
241304func (c * Client ) SendRaw (m []byte ) error {
242- return c .Send (protocol .RawMessage (m ))
243- }
244-
245- // Handshake initiates handshake mode. Users must call this before attempting
246- // to send any messages when the server is configured with a shared key, otherwise
247- // the server will reject any message events. Successful completion of the
248- // handshake puts the connection into message (or forward) mode, at which time
249- // the client is free to send event messages.
250- func (c * Client ) Handshake () error {
251305 c .sessionLock .RLock ()
252306 defer c .sessionLock .RUnlock ()
253307
254308 if c .session == nil {
255- return errors .New ("not connected " )
309+ return errors .New ("no active session " )
256310 }
257311
258- var helo protocol.Helo
312+ if ! c .session .TransportPhase {
313+ return errors .New ("session handshake not completed" )
314+ }
259315
260- r := msgp .NewReader (c .session .Connection )
261- err := helo .DecodeMsg (r )
316+ _ , err := c .session .Connection .Write (m )
262317
263- if err != nil {
264- return err
318+ return err
319+ }
320+
321+ func (c * Client ) SendPacked (tag string , entries protocol.EntryList ) error {
322+ msg , err := protocol .NewPackedForwardMessage (tag , entries )
323+ if err == nil {
324+ err = c .Send (msg )
265325 }
266326
267- salt := make ([]byte , 16 )
327+ return err
328+ }
268329
269- _ , err = rand .Read (salt )
270- if err != nil {
271- return err
272- }
330+ func (c * Client ) SendPackedFromBytes (tag string , entries []byte ) error {
331+ msg := protocol .NewPackedForwardMessageFromBytes (tag , entries )
273332
274- ping , err := protocol .NewPing (c .Hostname , c .AuthInfo .SharedKey , salt , helo .Options .Nonce )
275- if err != nil {
276- return err
277- }
333+ return c .Send (msg )
334+ }
278335
279- err = msgp .Encode (c .session .Connection , ping )
280- if err != nil {
281- return err
282- }
336+ func (c * Client ) SendMessage (tag string , record interface {}) error {
337+ msg := protocol .NewMessage (tag , record )
283338
284- var pong protocol.Pong
339+ return c .Send (msg )
340+ }
285341
286- err = pong .DecodeMsg (r )
287- if err != nil {
288- return err
289- }
342+ func (c * Client ) SendMessageExt (tag string , record interface {}) error {
343+ msg := protocol .NewMessageExt (tag , record )
290344
291- if err := protocol .ValidatePongDigest (& pong , c .AuthInfo .SharedKey ,
292- helo .Options .Nonce , salt ); err != nil {
293- return err
345+ return c .Send (msg )
346+ }
347+
348+ func (c * Client ) SendForward (tag string , entries protocol.EntryList ) error {
349+ msg := protocol .NewForwardMessage (tag , entries )
350+
351+ return c .Send (msg )
352+ }
353+
354+ func (c * Client ) SendCompressed (tag string , entries protocol.EntryList ) error {
355+ msg , err := protocol .NewCompressedPackedForwardMessage (tag , entries )
356+ if err == nil {
357+ err = c .Send (msg )
294358 }
295359
296- c .session .TransportPhase = true
360+ return err
361+ }
297362
298- return nil
363+ func (c * Client ) SendCompressedFromBytes (tag string , entries []byte ) error {
364+ msg , err := protocol .NewCompressedPackedForwardMessageFromBytes (tag , entries )
365+ if err == nil {
366+ err = c .Send (msg )
367+ }
368+
369+ return err
299370}
0 commit comments