File tree 2 files changed +54
-32
lines changed
2 files changed +54
-32
lines changed Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
+ "encoding/binary"
4
5
"log"
5
6
"net"
6
- //"fmt"
7
- //"bytes"
8
- "encoding/binary"
9
7
10
8
"code.google.com/p/goprotobuf/proto"
11
9
"example_mess"
12
10
)
13
11
14
- var _ = net .Dial
15
- //var _ = bytes.Buffer
12
+ func passOrDie (err error ) {
13
+ if err != nil {
14
+ log .Fatal (err )
15
+ }
16
+ }
16
17
17
18
func main () {
18
-
19
19
socket , err := net .Dial ("unix" , "socket" )
20
- if err != nil {
21
- log .Fatal ("Failed to connect to socket: " , err )
22
- }
23
-
20
+ passOrDie (err )
24
21
defer socket .Close ()
25
22
26
- test := & example_mess.Boring {
23
+ msg := & example_mess.Boring {
27
24
Cont : proto .String ("Greets from Go" ),
28
25
}
29
- serialized , err := proto .Marshal (test )
30
- if err != nil {
31
- log .Fatal ("marshaling error: " , err )
32
- }
33
-
34
-
35
- //buf := new(bytes.Buffer)
36
- println (len (serialized ))
37
- //err = binary.Write(buf, binary.BigEndian, int32(1337))
38
- //fmt.Printf("% d", buf.Bytes())
39
-
26
+
27
+ serialized , err := proto .Marshal (msg )
28
+ passOrDie (err )
29
+
40
30
// Go is unique in that, thanks to its interfaces, it's practical to encode directly to the socket
41
31
err = binary .Write (socket , binary .BigEndian , int32 (len (serialized )))
42
- if err != nil {
43
- log .Fatal ("failed writing to socket: " , err )
44
- }
45
-
32
+ passOrDie (err )
33
+
46
34
socket .Write (serialized )
47
35
}
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
+ "encoding/binary"
4
5
"log"
6
+ "net"
7
+ "syscall"
5
8
6
9
"code.google.com/p/goprotobuf/proto"
7
10
"example_mess"
8
11
)
9
12
10
- func main () {
11
- test := & example_mess.Boring {
12
- Cont : proto .String ("wew" ),
13
- }
14
- _ , err := proto .Marshal (test )
13
+ func passOrDie (err error ) {
15
14
if err != nil {
16
- log .Fatal ("marshaling error: " , err )
15
+ log .Fatal (err )
17
16
}
18
17
}
18
+
19
+ func handler (connection net.Conn ) {
20
+ var message_length int32
21
+
22
+ // encoding/binary is quite clever! Notice that we're reading from the socket and encoding directly into the int32 here. Exactly 4 bytes (the size of message_length) will be read.
23
+ err := binary .Read (connection , binary .BigEndian , & message_length )
24
+ passOrDie (err )
25
+
26
+ println (message_length )
27
+
28
+ msg := new (example_mess.Boring )
29
+
30
+ buf := make ([]byte , message_length )
31
+ err = binary .Read (connection , binary .BigEndian , buf )
32
+ passOrDie (err )
33
+ err = proto .Unmarshal (buf , msg )
34
+ passOrDie (err )
35
+
36
+ println ("Message: " , msg .GetCont ())
37
+
38
+ }
39
+
40
+ func main () {
41
+ syscall .Unlink ("socket" )
42
+ listener , err := net .Listen ("unix" , "socket" )
43
+ passOrDie (err )
44
+
45
+ for {
46
+ connection , err := listener .Accept ()
47
+ passOrDie (err )
48
+
49
+ go handler (connection )
50
+ }
51
+
52
+ }
You can’t perform that action at this time.
0 commit comments