-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdds.go
156 lines (124 loc) · 3.68 KB
/
dds.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package cyclonedds
// #cgo pkg-config: CycloneDDS
// #include <stdlib.h>
// #include <dds/dds.h>
import "C"
import (
"errors"
"unsafe"
)
type DomainID uint32
type Participant struct {
native C.int32_t
}
func (e Participant) getNative() C.int32_t {
return e.native
}
type Topic struct {
native C.int32_t
}
func (e Topic) getNative() C.int32_t {
return e.native
}
type Publisher struct {
native C.int32_t
}
func (e Publisher) getNative() C.int32_t {
return e.native
}
type Subscriber struct {
native C.int32_t
}
func (e Subscriber) getNative() C.int32_t {
return e.native
}
type DataWriter struct {
native C.int32_t
}
func (e DataWriter) getNative() C.int32_t {
return e.native
}
type DataReader struct {
native C.int32_t
}
func (e DataReader) getNative() C.int32_t {
return e.native
}
type ParticipantOrPublisher interface {
Participant | Publisher
getNative() C.int32_t
}
type ParticipantOrSubsciber interface {
Participant | Subscriber
getNative() C.int32_t
}
type Entity interface {
Participant | Topic | Publisher | Subscriber | DataWriter | DataReader
getNative() C.int32_t
}
func checkEntity[E Entity](e E) (E, error) {
if e.getNative() > 0 {
return e, nil
}
return e, errors.New(C.GoString(C.dds_strretcode(-e.getNative())))
}
func CreateParticipant(domain DomainID, qos *Qos) (Participant, error) {
e := Participant{native: C.dds_create_participant(C.uint32_t(domain), nil, nil)}
return checkEntity(e)
}
func (p Participant) Delete() error {
rc := C.dds_delete(p.getNative())
if rc != C.DDS_RETCODE_OK {
return errors.New(C.GoString(C.dds_strretcode(-rc)))
}
return nil
}
func CreateTopic(participant Participant, name string, dataType any, qos *Qos) (Topic, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
dStruct := dynamicTypeCreate(participant, dataType)
defer C.dds_dynamic_type_unref(&dStruct)
var typeInfo *C.dds_typeinfo_t
rc := C.dds_dynamic_type_register(&dStruct, &typeInfo)
if rc != C.DDS_RETCODE_OK {
return Topic{native: 0}, errors.New(C.GoString(C.dds_strretcode(-rc)))
}
defer C.dds_free_typeinfo(typeInfo)
var topicDesc *C.dds_topic_descriptor_t
rc = C.dds_create_topic_descriptor(C.DDS_FIND_SCOPE_LOCAL_DOMAIN, participant.getNative(), typeInfo, 0, &topicDesc)
if rc != C.DDS_RETCODE_OK {
return Topic{native: 0}, errors.New(C.GoString(C.dds_strretcode(-rc)))
}
defer C.dds_delete_topic_descriptor(topicDesc)
e := Topic{native: C.dds_create_topic(participant.getNative(), topicDesc, cname, nil, nil)}
return checkEntity(e)
}
func CreatePublisher(participant Participant, qos *Qos) (Publisher, error) {
e := Publisher{native: C.dds_create_publisher(C.int32_t(participant.native), nil, nil)}
return checkEntity(e)
}
func CreateSubscriber(participant Participant, qos *Qos) (Subscriber, error) {
e := Subscriber{native: C.dds_create_subscriber(C.int32_t(participant.native), nil, nil)}
return checkEntity(e)
}
func CreateWtiter[P ParticipantOrPublisher](participantOrPublisher P, topic Topic) (DataWriter, error) {
e := DataWriter{native: C.dds_create_writer(participantOrPublisher.getNative(), topic.native, nil, nil)}
return checkEntity(e)
}
func (w DataWriter) Write(data any) error {
// var buf bytes.Buffer
// enc := gob.NewEncoder(&buf)
// if err := enc.Encode(data); err != nil {
// return err
// }
// msg := buf.Bytes()
rc := C.dds_write(w.getNative(), unsafe.Pointer(&data))
if rc != C.DDS_RETCODE_OK {
return errors.New(C.GoString(C.dds_strretcode(-rc)))
}
return nil
}
func CreateReader[P ParticipantOrSubsciber](participantOrSubscriber P, topic Topic) (DataReader, error) {
e := DataReader{native: C.dds_create_reader(participantOrSubscriber.getNative(), topic.native, nil, nil)}
return checkEntity(e)
}