aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/transport/channel_netw.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet/transport/channel_netw.go')
-rw-r--r--src/gnunet/transport/channel_netw.go39
1 files changed, 28 insertions, 11 deletions
diff --git a/src/gnunet/transport/channel_netw.go b/src/gnunet/transport/channel_netw.go
index 69ddda5..ecfd8e2 100644
--- a/src/gnunet/transport/channel_netw.go
+++ b/src/gnunet/transport/channel_netw.go
@@ -14,11 +14,12 @@ import (
14 14
15// NetworkChannel 15// NetworkChannel
16type NetworkChannel struct { 16type NetworkChannel struct {
17 network string 17 network string // network protocol identifier ("tcp", "unix", ...)
18 conn net.Conn 18 conn net.Conn // associated connection
19} 19}
20 20
21// NewNetworkChannel 21// NewNetworkChannel creates a new channel for a given network protocol.
22// The channel is in pending state and need to be opened before use.
22func NewNetworkChannel(netw string) Channel { 23func NewNetworkChannel(netw string) Channel {
23 return &NetworkChannel{ 24 return &NetworkChannel{
24 network: netw, 25 network: netw,
@@ -26,7 +27,11 @@ func NewNetworkChannel(netw string) Channel {
26 } 27 }
27} 28}
28 29
29// Open 30// Open a network channel based on specification:
31// The specification is a string separated into parts by the '+' delimiter
32// (e.g. "unix+/tmp/gnunet-service-gns-go.sock+perm=0770"). The network
33// identifier (first part) must match the network specification of the
34// underlaying NetworkChannel instance.
30func (c *NetworkChannel) Open(spec string) (err error) { 35func (c *NetworkChannel) Open(spec string) (err error) {
31 parts := strings.Split(spec, "+") 36 parts := strings.Split(spec, "+")
32 // check for correct protocol 37 // check for correct protocol
@@ -38,7 +43,7 @@ func (c *NetworkChannel) Open(spec string) (err error) {
38 return 43 return
39} 44}
40 45
41// Close 46// Close a network channel
42func (c *NetworkChannel) Close() error { 47func (c *NetworkChannel) Close() error {
43 if c.conn != nil { 48 if c.conn != nil {
44 return c.conn.Close() 49 return c.conn.Close()
@@ -46,7 +51,8 @@ func (c *NetworkChannel) Close() error {
46 return ErrChannelNotOpened 51 return ErrChannelNotOpened
47} 52}
48 53
49// Read 54// Read bytes from a network channel into buffer: Returns the number of read
55// bytes and an error code. Only works on open channels ;)
50func (c *NetworkChannel) Read(buf []byte) (int, error) { 56func (c *NetworkChannel) Read(buf []byte) (int, error) {
51 if c.conn == nil { 57 if c.conn == nil {
52 return 0, ErrChannelNotOpened 58 return 0, ErrChannelNotOpened
@@ -54,7 +60,8 @@ func (c *NetworkChannel) Read(buf []byte) (int, error) {
54 return c.conn.Read(buf) 60 return c.conn.Read(buf)
55} 61}
56 62
57// Write 63// Write buffer to a network channel: Returns the number of written bytes and
64// an error code.
58func (c *NetworkChannel) Write(buf []byte) (int, error) { 65func (c *NetworkChannel) Write(buf []byte) (int, error) {
59 if c.conn == nil { 66 if c.conn == nil {
60 return 0, ErrChannelNotOpened 67 return 0, ErrChannelNotOpened
@@ -67,8 +74,8 @@ func (c *NetworkChannel) Write(buf []byte) (int, error) {
67 74
68// NetworkChannelServer 75// NetworkChannelServer
69type NetworkChannelServer struct { 76type NetworkChannelServer struct {
70 network string 77 network string // network protocol to listen on
71 listener net.Listener 78 listener net.Listener // reference to listener object
72} 79}
73 80
74// NewNetworkChannelServer 81// NewNetworkChannelServer
@@ -79,7 +86,9 @@ func NewNetworkChannelServer(netw string) ChannelServer {
79 } 86 }
80} 87}
81 88
82// Open 89// Open a network channel server (= start running it) based on the given
90// specification. For every client connection to the server, the associated
91// network channel for the connection is send via the hdlr channel.
83func (s *NetworkChannelServer) Open(spec string, hdlr chan<- Channel) (err error) { 92func (s *NetworkChannelServer) Open(spec string, hdlr chan<- Channel) (err error) {
84 parts := strings.Split(spec, "+") 93 parts := strings.Split(spec, "+")
85 // check for correct protocol 94 // check for correct protocol
@@ -136,7 +145,7 @@ func (s *NetworkChannelServer) Open(spec string, hdlr chan<- Channel) (err error
136 return nil 145 return nil
137} 146}
138 147
139// Close 148// Close a network channel server (= stop the server)
140func (s *NetworkChannelServer) Close() error { 149func (s *NetworkChannelServer) Close() error {
141 if s.listener != nil { 150 if s.listener != nil {
142 err := s.listener.Close() 151 err := s.listener.Close()
@@ -147,27 +156,35 @@ func (s *NetworkChannelServer) Close() error {
147} 156}
148 157
149//////////////////////////////////////////////////////////////////////// 158////////////////////////////////////////////////////////////////////////
159// helper functions to instantiate network channels and servers for
160// common network protocols
150 161
162// NewSocketChannel: Unix Domain Socket connection
151func NewSocketChannel() Channel { 163func NewSocketChannel() Channel {
152 return NewNetworkChannel("unix") 164 return NewNetworkChannel("unix")
153} 165}
154 166
167// NewTCPChannel: TCP connection
155func NewTCPChannel() Channel { 168func NewTCPChannel() Channel {
156 return NewNetworkChannel("tcp") 169 return NewNetworkChannel("tcp")
157} 170}
158 171
172// NewUDPChannel: UDP connection
159func NewUDPChannel() Channel { 173func NewUDPChannel() Channel {
160 return NewNetworkChannel("udp") 174 return NewNetworkChannel("udp")
161} 175}
162 176
177// NewSocketChannelServer: Unix Domain Socket listener
163func NewSocketChannelServer() ChannelServer { 178func NewSocketChannelServer() ChannelServer {
164 return NewNetworkChannelServer("unix") 179 return NewNetworkChannelServer("unix")
165} 180}
166 181
182// NewTCPChannelServer: TCP listener
167func NewTCPChannelServer() ChannelServer { 183func NewTCPChannelServer() ChannelServer {
168 return NewNetworkChannelServer("tcp") 184 return NewNetworkChannelServer("tcp")
169} 185}
170 186
187// NewUDPChannelServer: UDP listener
171func NewUDPChannelServer() ChannelServer { 188func NewUDPChannelServer() ChannelServer {
172 return NewNetworkChannelServer("udp") 189 return NewNetworkChannelServer("udp")
173} 190}