Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "net"
- "os"
- "strings"
- )
- // ----------------------------------------------------------------------------
- // types start
- // ----------------------------------------------------------------------------
- type tcpServerConfig struct {
- maxConnections int64
- listen []uint16
- //keepAlive bool
- //keepAliveTimeSecond int
- //keepAliveIntervalSecond int
- //keepAliveProbe int
- }
- // типа разные серверы внутри одного приложения
- type appConfig struct {
- device tcpServerConfig
- soft tcpServerConfig
- }
- type connection struct {
- id uint64
- client net.Conn
- }
- type connectionHandler func(con connection)
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- // functions
- // ----------------------------------------------------------------------------
- func panicOnError(e error) {
- if e != nil {
- panic(e)
- }
- }
- func readConfig(configFilePath string) *appConfig {
- // todo parse config
- file, err := os.ReadFile(configFilePath)
- _ = file
- panicOnError(err)
- result := appConfig{
- device: tcpServerConfig{
- maxConnections: -1,
- listen: make([]uint16, 0),
- },
- soft: tcpServerConfig{
- maxConnections: -1,
- listen: make([]uint16, 0),
- },
- }
- result.device.listen = make([]uint16, 1)
- result.device.listen[0] = 8000
- result.soft.listen = make([]uint16, 1)
- result.soft.listen[0] = 8001
- return &result
- }
- func runServer(listen []uint16, handler connectionHandler) {
- listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", listen[0]))
- panicOnError(err)
- fmt.Printf("Server started on port : %d\n", listen[0])
- for {
- con, err := listener.Accept()
- panicOnError(err)
- connection := connection{client: con}
- go handler(connection)
- }
- }
- func echoHandler(connection connection) {
- fmt.Printf("Start handle connection from %s\n", connection.client.RemoteAddr())
- buff := make([]uint8, 1024)
- for {
- n, err := connection.client.Read(buff)
- if err != nil {
- fmt.Printf("Error during receive from %s; error: %s\n", connection.client.RemoteAddr(), err)
- break
- }
- if n == 0 {
- fmt.Printf("FIN connection from %s\n", connection.client.RemoteAddr())
- break
- }
- nSend, err := connection.client.Write(buff[:n])
- _ = nSend
- if err != nil {
- fmt.Printf("Error during send to %s; error: %s\n", connection.client.RemoteAddr(), err)
- break
- }
- }
- fmt.Printf("Complete handle connection from %s\n", connection.client.RemoteAddr())
- }
- // ----------------------------------------------------------------------------
- // main
- func main() {
- fmt.Printf("Init application\n")
- config := readConfig("./app.config")
- go runServer(config.device.listen, echoHandler)
- go runServer(config.soft.listen, echoHandler)
- reader := bufio.NewReader(os.Stdin)
- for {
- fmt.Printf("Print ! to stop application\n")
- input, err := reader.ReadString('\n')
- panicOnError(err)
- if strings.TrimRight(input, "\r\n") == "!" {
- break
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement