-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtm.go
60 lines (53 loc) · 1.1 KB
/
gtm.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
package gtm
import (
"log"
"net"
)
const (
WriterStopped = iota
ReaderStopped
ConnectionClosed
WriterStarted
ReaderStarted
)
type conn struct {
Netcon net.Conn
IncommingMessages chan *[]byte
InfoChan chan int
internalComsChan chan int
outgoingMessages chan *[]byte
writerKiller chan bool
MessagesSent uint64
MessagesReceived uint64
ReaderListening bool
WriterListening bool
}
func NewConnection(con net.Conn) conn {
c := conn{
Netcon: con,
IncommingMessages: make(chan *[]byte, 100),
InfoChan: make(chan int, 5),
internalComsChan: make(chan int, 5),
outgoingMessages: make(chan *[]byte, 100),
writerKiller: make(chan bool, 1),
MessagesSent: 0,
MessagesReceived: 0,
ReaderListening: false,
WriterListening: false,
}
go c.startReader()
go c.startWriter()
return c
}
func fatalLog(v ...interface{}) {
// later this will use a custom first class
// logging function specified by the user
log.Fatalln(v)
}
func (c *conn) Close() {
err := c.Netcon.Close()
if err != nil {
fatalLog(err)
}
c.killWriter()
}