missing error handling added

This commit is contained in:
Torsten Harenberg
2025-02-28 14:59:31 +01:00
parent 49058adbfd
commit 4ed3ce9b10
7 changed files with 265 additions and 147 deletions

View File

@@ -22,7 +22,7 @@ func Chunks(s string, chunkSize int) []string {
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string = make([]string, 0, (len(s)-1)/chunkSize+1)
chunks := make([]string, 0, (len(s)-1)/chunkSize+1)
currentLen := 0
currentStart := 0
for i := range s {
@@ -41,7 +41,10 @@ func Chunks(s string, chunkSize int) []string {
func handleTCPCmdConnection(conn net.Conn) {
defer func() {
s.Status &^= StatusTCPCmdActive
conn.Close()
err := conn.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
}()
s.Status |= StatusTCPCmdActive
@@ -83,7 +86,10 @@ func handleTCPCmdConnection(conn net.Conn) {
return
} else {
s.Protocol <- fmt.Sprintf(color.InCyan("Response: %s\n"), msg)
conn.Write([]byte(fmt.Sprintf("%s\r", msg)))
_, err = conn.Write([]byte(fmt.Sprintf("%s\r", msg)))
if err != nil {
writeDebug(err.Error(), 0)
}
}
}
}(ctx)
@@ -98,7 +104,10 @@ func handleTCPCmdConnection(conn net.Conn) {
alivecancel()
//buffercancel()
cancel()
conn.Close()
err = conn.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
break
}
@@ -138,7 +147,10 @@ func handleTCPCmdConnection(conn net.Conn) {
func handleTCPDataConnection(conn net.Conn) {
defer func() {
s.Status &^= StatusTCPDataActive
conn.Close()
err := conn.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
}()
s.Protocol <- fmt.Sprintf(color.InGreen("TCP Data Connection established with %s\n"), conn.RemoteAddr())
@@ -154,9 +166,15 @@ func handleTCPDataConnection(conn net.Conn) {
} else {
//TODO: VARA
if !s.DaemonMode {
s.FromPactor.Enqueue(msg)
err := s.FromPactor.Enqueue(msg)
if err != nil {
writeDebug(err.Error(), 0)
}
}
_, err = conn.Write(msg)
if err != nil {
writeDebug(err.Error(), 0)
}
conn.Write(msg)
}
}
}()
@@ -171,7 +189,10 @@ func handleTCPDataConnection(conn net.Conn) {
if err != nil {
s.Status &^= StatusTCPDataActive
cancel()
conn.Close()
err := conn.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
return
}
//TODO: muss das nicht weg?
@@ -180,15 +201,24 @@ func handleTCPDataConnection(conn net.Conn) {
}
*/
if !s.DaemonMode {
s.ToPactor.Enqueue(temp)
err := s.ToPactor.Enqueue(temp)
if err != nil {
writeDebug(err.Error(), 0)
}
}
if n > 255 {
// we can dump at max 255 bytes into
for _, ck := range Chunks(string(temp), 255) {
s.Data.Data.Enqueue([]byte(ck))
err := s.Data.Data.Enqueue([]byte(ck))
if err != nil {
writeDebug(err.Error(), 0)
}
}
} else {
s.Data.Data.Enqueue(temp)
err := s.Data.Data.Enqueue(temp)
if err != nil {
writeDebug(err.Error(), 0)
}
}
}
}
@@ -199,7 +229,7 @@ func processVARACommand(command string) (string, string, error) {
case strings.HasPrefix(command, "CONNECT"):
re, err := regexp.MatchString(`CONNECT \w+ \w+`, command)
if err != nil || !re {
return "", "", errors.New("Error matching regex")
return "", "", errors.New("error matching regex")
}
c := strings.Split(command, " ")
return fmt.Sprintf("C %s", c[2]), "", nil
@@ -220,7 +250,7 @@ func processVARACommand(command string) (string, string, error) {
if strings.HasSuffix(command, "OFF") {
return "%L 0", "", nil
}
return "", "", errors.New("Neither ON nor OFF after LISTEN")
return "", "", errors.New("neither ON nor OFF after LISTEN")
case strings.HasPrefix(command, "MYCALL"):
m := ""
@@ -230,7 +260,7 @@ func processVARACommand(command string) (string, string, error) {
// send own callsign to PACTOR controller
m = s[1]
} else {
return "", "", errors.New("Invalid MYCALL command")
return "", "", errors.New("invalid MYCALL command")
}
return fmt.Sprintf("%s%s", "I ", m), "", nil
@@ -267,7 +297,7 @@ func processVARACommand(command string) (string, string, error) {
default:
// Handle unrecognized commands
return "", "", errors.New("Unknown command")
return "", "", errors.New("unknown command")
}
}
@@ -279,7 +309,12 @@ func tcpCmdServer(Config *Userconfig) {
log.Println(err)
return
}
defer listener.Close()
defer func() {
err := listener.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
}()
s.Protocol <- fmt.Sprintf("TCP Protocol Server listening on %s\n", Config.ServerAddress)
@@ -303,7 +338,12 @@ func tcpDataServer(Config *Userconfig) {
log.Println(err)
return
}
defer listener.Close()
defer func() {
err := listener.Close()
if err != nil {
writeDebug(err.Error(), 0)
}
}()
s.Protocol <- fmt.Sprintf("TCP Data Server listening on %s\n", Config.DataAddress)
for {