This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
tcp_server.go
127 lines (110 loc) · 2.78 KB
/
tcp_server.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
package tcp_server
import (
"bufio"
"crypto/tls"
"log"
"net"
)
// Client holds info about connection
type Client struct {
conn net.Conn
Server *server
}
// TCP server
type server struct {
address string // Address to open connection: localhost:9999
config *tls.Config
onNewClientCallback func(c *Client)
onClientConnectionClosed func(c *Client, err error)
onNewMessage func(c *Client, message string)
}
// Read client data from channel
func (c *Client) listen() {
c.Server.onNewClientCallback(c)
reader := bufio.NewReader(c.conn)
for {
message, err := reader.ReadString('\n')
if err != nil {
c.conn.Close()
c.Server.onClientConnectionClosed(c, err)
return
}
c.Server.onNewMessage(c, message)
}
}
// Send text message to client
func (c *Client) Send(message string) error {
return c.SendBytes([]byte(message))
}
// Send bytes to client
func (c *Client) SendBytes(b []byte) error {
_, err := c.conn.Write(b)
if err != nil {
c.conn.Close()
c.Server.onClientConnectionClosed(c, err)
}
return err
}
func (c *Client) Conn() net.Conn {
return c.conn
}
func (c *Client) Close() error {
return c.conn.Close()
}
// Called right after server starts listening new client
func (s *server) OnNewClient(callback func(c *Client)) {
s.onNewClientCallback = callback
}
// Called right after connection closed
func (s *server) OnClientConnectionClosed(callback func(c *Client, err error)) {
s.onClientConnectionClosed = callback
}
// Called when Client receives new message
func (s *server) OnNewMessage(callback func(c *Client, message string)) {
s.onNewMessage = callback
}
// Listen starts network server
func (s *server) Listen() {
var listener net.Listener
var err error
if s.config == nil {
listener, err = net.Listen("tcp", s.address)
} else {
listener, err = tls.Listen("tcp", s.address, s.config)
}
if err != nil {
log.Fatal("Error starting TCP server.\r\n", err)
}
defer listener.Close()
for {
conn, _ := listener.Accept()
client := &Client{
conn: conn,
Server: s,
}
go client.listen()
}
}
// Creates new tcp server instance
func New(address string) *server {
log.Println("Creating server with address", address)
server := &server{
address: address,
}
server.OnNewClient(func(c *Client) {})
server.OnNewMessage(func(c *Client, message string) {})
server.OnClientConnectionClosed(func(c *Client, err error) {})
return server
}
func NewWithTLS(address, certFile, keyFile string) *server {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatal("Error loading certificate files. Unable to create TCP server with TLS functionality.\r\n", err)
}
config := &tls.Config{
Certificates: []tls.Certificate{cert},
}
server := New(address)
server.config = config
return server
}