gonews/networker/server.go

49 lines
1.0 KiB
Go
Raw Normal View History

2019-09-02 22:24:34 +05:00
package networker
import (
// stdlib
"log"
"net"
// local
"sources.dev.pztrn.name/gonews/gonews/configuration"
2019-09-02 22:24:34 +05:00
)
var connections map[string]*connection
2019-09-02 22:24:34 +05:00
// This function responsible for accepting incoming connections for
// each address configuration.
func startServer(config configuration.Network) {
log.Println("Starting server on " + config.Address + " (type: " + config.Type + ")")
if connections == nil {
connections = make(map[string]*connection)
}
2019-09-02 22:24:34 +05:00
l, err := net.Listen("tcp", config.Address)
if err != nil {
log.Fatalln("Failed to start TCP server on " + config.Address + ": " + err.Error())
}
2019-10-22 04:38:40 +05:00
2019-09-02 22:24:34 +05:00
defer func() {
err := l.Close()
if err != nil {
log.Println("Failed to close TCP server on " + config.Address + ": " + err.Error())
}
}()
for {
conn, err1 := l.Accept()
if err1 != nil {
log.Println("Failed to accept new incoming connection: " + err1.Error())
continue
}
c := &connection{}
c.Initialize(conn)
connections[conn.RemoteAddr().String()] = c
go c.Start()
2019-09-02 22:24:34 +05:00
}
}