-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
55 lines (46 loc) · 881 Bytes
/
main.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
package main
import (
"encoding/binary"
"io"
"net"
"os"
)
type FPMHeader struct {
Version uint8
MessageType uint8
MessageLen uint16
}
func handleConnection(conn net.Conn) {
for {
h := FPMHeader{}
binary.Read(conn, binary.BigEndian, &h.Version)
binary.Read(conn, binary.BigEndian, &h.MessageType)
binary.Read(conn, binary.BigEndian, &h.MessageLen)
if h.Version != 1 {
panic("Unsupported FPM frame version")
}
if h.MessageType != 1 {
panic("Unsupported FPM frame type")
}
n, err := io.CopyN(os.Stdout, conn, int64(h.MessageLen-4))
if err != nil {
panic(err)
}
if n != int64(h.MessageLen-4) {
panic("Couldn't read entire message")
}
}
}
func main() {
ln, err := net.Listen("tcp", ":2620")
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
handleConnection(conn)
}
}