-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.go
82 lines (73 loc) · 2.33 KB
/
custom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package fleet provides a distributed peer-to-peer communication framework.
package fleet
import (
"io/fs"
"sync"
)
// CustomHandler is a function type for handling custom packet types.
// Applications can register handlers for custom packet codes in the range
// 0xa000-0xafff to extend the fleet protocol with application-specific functionality.
//
// Parameters:
// - p: The peer that sent the packet
// - data: The raw packet data
//
// Returns:
// - An error if handling fails, nil otherwise
type CustomHandler func(p *Peer, data []byte) error
// Global registry for custom packet handlers
var (
// Map of packet codes to their handlers
customHandlers = make(map[uint16]CustomHandler)
// Mutex to protect concurrent access to the handlers map
customHandlersLk sync.RWMutex
)
// SetCustomHandler registers a handler function for a custom packet type.
// This allows applications to extend the fleet protocol with custom
// packet types and handling logic.
//
// The packet code should be in the range 0xa000-0xafff, which can be
// obtained using the Custom() function.
//
// Parameters:
// - pc: The packet code to register the handler for
// - h: The handler function to call when this packet type is received
func SetCustomHandler(pc uint16, h CustomHandler) {
customHandlersLk.Lock()
defer customHandlersLk.Unlock()
customHandlers[pc] = h
}
// getCustomHandler retrieves the handler for a given packet code.
// Returns nil if no handler is registered for the code.
//
// Parameters:
// - pc: The packet code to get the handler for
//
// Returns:
// - The registered handler, or nil if none exists
func getCustomHandler(pc uint16) CustomHandler {
customHandlersLk.RLock()
defer customHandlersLk.RUnlock()
if v, ok := customHandlers[pc]; ok {
return v
}
return nil
}
// callCustomHandler invokes the appropriate handler for a custom packet.
// This is called by the peer packet handling logic when a custom packet
// is received.
//
// Parameters:
// - p: The peer that sent the packet
// - pc: The packet code
// - data: The raw packet data
//
// Returns:
// - An error if handling fails or no handler is registered, nil otherwise
func callCustomHandler(p *Peer, pc uint16, data []byte) error {
h := getCustomHandler(pc)
if h == nil {
return fs.ErrNotExist // No handler registered for this packet code
}
return h(p, data)
}