Debian Codegraph @

paho/session/session.go

raw
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package session
17
18 import (
19 "context"
20 "errors"
21 "io"
22
23 "github.com/eclipse/paho.golang/packets"
24 paholog "github.com/eclipse/paho.golang/paho/log"
25 )
26
27 // The session state includes:
28 // * Inflight QOS1/2 Publish transactions (both sent and received)
29 // * All requests
30
31 var (
32 ErrNoConnection = errors.New("no connection available") // We are not in-between a call to ConAckReceived and ConnectionLost
33 ErrPacketIdentifiersExhausted = errors.New("all packet identifiers in use") // There are no available Packet IDs
34 )
35
36 // Packet provides sufficient functionality to enable a packet to be transmitted with a packet identifier
37 type Packet interface {
38 SetIdentifier(uint16) // Sets the packet identifier
39 Type() byte // Gets the packet type
40 WriteTo(io.Writer) (int64, error)
41 }
42
43 // SessionManager will manage the mqtt session state; note that the state may outlast a single `Client` instance
44 type SessionManager interface {
45 // ConAckReceived must be called when a CONNACK has been received (with no error). If an error is returned
46 // then the connection will be dropped.
47 ConAckReceived(io.Writer, *packets.Connect, *packets.Connack) error
48
49 // ConnectionLost must be called whenever the connection is lost or a DISCONNECT packet is received. It can be
50 // called multiple times for the same event as long as ConAckReceived is not called in the interim.
51 ConnectionLost(dp *packets.Disconnect) error
52
53 // AddToSession adds a packet to the session state (including allocation of a message identifier).
54 // This should only be used for packets that impact the session state (which does not include QOS0 publish).
55 // If this function returns a nil then:
56 // - A message Identifier has been added to the passed in packet
57 // - If a `PUBLISH` then a slot has been allocated (function will block if RECEIVE MAXIMUM messages are inflight)
58 // - Publish messages will have been written to the store (and will be automatically transmitted if a new connection
59 // is established before the message is fully acknowledged - subject to state rules in the MQTTv5 spec)
60 // - Something will be sent to `resp` when either the message is fully acknowledged or the packet is removed from
61 // the session (in which case nil will be sent).
62 //
63 // If the function returns an error, then any actions taken will be rewound prior to return.
64 AddToSession(ctx context.Context, packet Packet, resp chan<- packets.ControlPacket) error
65
66 // PacketReceived must be called when any packet with a packet identifier is received. It will make any required
67 // response and pass any `PUBLISH` messages that need to be passed to the user via the channel.
68 PacketReceived(*packets.ControlPacket, chan<- *packets.Publish) error
69
70 // Ack must be called when the client message handlers have completed (or, if manual acknowledgements are enabled,
71 // when`client.ACK()` has been called - note the potential issues discussed in issue #160.
72 Ack(pb *packets.Publish) error
73
74 // Close shuts down the session store, this will release any blocked calls
75 // Note: `paho` will only call this if it created the session (i.e. it was not passed in the config)
76 Close() error
77
78 // SetErrorLogger enables error logging via the passed logger (not thread safe)
79 SetErrorLogger(l paholog.Logger)
80
81 // SetDebugLogger enables debug logging via the passed logger (not thread safe)
82 SetDebugLogger(l paholog.Logger)
83 }