Advertisement
Guest User

Untitled

a guest
Mar 10th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.13 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "net"
  7.     "os"
  8.     "strings"
  9. )
  10.  
  11. // ----------------------------------------------------------------------------
  12. // types start
  13. // ----------------------------------------------------------------------------
  14. type tcpServerConfig struct {
  15.     maxConnections int64
  16.     listen         []uint16
  17.     //keepAlive               bool
  18.     //keepAliveTimeSecond     int
  19.     //keepAliveIntervalSecond int
  20.     //keepAliveProbe          int
  21. }
  22.  
  23. // типа разные серверы внутри одного приложения
  24. type appConfig struct {
  25.     device tcpServerConfig
  26.     soft   tcpServerConfig
  27. }
  28.  
  29. type connection struct {
  30.     id     uint64
  31.     client net.Conn
  32. }
  33. type connectionHandler func(con connection)
  34.  
  35. // ----------------------------------------------------------------------------
  36.  
  37. // ----------------------------------------------------------------------------
  38. // functions
  39. // ----------------------------------------------------------------------------
  40. func panicOnError(e error) {
  41.     if e != nil {
  42.         panic(e)
  43.     }
  44. }
  45.  
  46. func readConfig(configFilePath string) *appConfig {
  47.     // todo parse config
  48.     file, err := os.ReadFile(configFilePath)
  49.     _ = file
  50.     panicOnError(err)
  51.     result := appConfig{
  52.         device: tcpServerConfig{
  53.             maxConnections: -1,
  54.             listen:         make([]uint16, 0),
  55.         },
  56.         soft: tcpServerConfig{
  57.             maxConnections: -1,
  58.             listen:         make([]uint16, 0),
  59.         },
  60.     }
  61.     result.device.listen = make([]uint16, 1)
  62.     result.device.listen[0] = 8000
  63.  
  64.     result.soft.listen = make([]uint16, 1)
  65.     result.soft.listen[0] = 8001
  66.     return &result
  67. }
  68. func runServer(listen []uint16, handler connectionHandler) {
  69.  
  70.     listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", listen[0]))
  71.     panicOnError(err)
  72.     fmt.Printf("Server started on port : %d\n", listen[0])
  73.     for {
  74.  
  75.         con, err := listener.Accept()
  76.         panicOnError(err)
  77.         connection := connection{client: con}
  78.         go handler(connection)
  79.     }
  80. }
  81.  
  82. func echoHandler(connection connection) {
  83.     fmt.Printf("Start handle connection from %s\n", connection.client.RemoteAddr())
  84.     buff := make([]uint8, 1024)
  85.     for {
  86.         n, err := connection.client.Read(buff)
  87.         if err != nil {
  88.             fmt.Printf("Error during receive from %s; error: %s\n", connection.client.RemoteAddr(), err)
  89.             break
  90.         }
  91.         if n == 0 {
  92.             fmt.Printf("FIN connection from %s\n", connection.client.RemoteAddr())
  93.             break
  94.         }
  95.         nSend, err := connection.client.Write(buff[:n])
  96.         _ = nSend
  97.         if err != nil {
  98.             fmt.Printf("Error during send to %s; error: %s\n", connection.client.RemoteAddr(), err)
  99.             break
  100.         }
  101.     }
  102.  
  103.     fmt.Printf("Complete handle connection from %s\n", connection.client.RemoteAddr())
  104. }
  105.  
  106. // ----------------------------------------------------------------------------
  107.  
  108. // main
  109. func main() {
  110.     fmt.Printf("Init application\n")
  111.  
  112.     config := readConfig("./app.config")
  113.  
  114.     go runServer(config.device.listen, echoHandler)
  115.     go runServer(config.soft.listen, echoHandler)
  116.  
  117.     reader := bufio.NewReader(os.Stdin)
  118.     for {
  119.         fmt.Printf("Print ! to stop application\n")
  120.  
  121.         input, err := reader.ReadString('\n')
  122.         panicOnError(err)
  123.         if strings.TrimRight(input, "\r\n") == "!" {
  124.             break
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement