Added async tasks for console and network
This commit is contained in:
parent
4c31bcb8a0
commit
3ee652111a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/steel.exe
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
# Steel
|
||||||
|
Steel is associated with stability and robustness. And this is all this cross-platform Minecraft Server-Software is about.
|
||||||
37
pkg/cmd/console.go
Normal file
37
pkg/cmd/console.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Start(wg *sync.WaitGroup, stopChan chan struct{}) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
for {
|
||||||
|
fmt.Print("> ")
|
||||||
|
input, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
_, err := fmt.Fprintf(os.Stderr, "Error while reading input: %v", err)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
close(stopChan)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
input = strings.TrimSpace(input)
|
||||||
|
switch input {
|
||||||
|
case "stop":
|
||||||
|
fmt.Println("Received stop command.")
|
||||||
|
close(stopChan)
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
fmt.Println("Unknown command:", input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
74
pkg/net/network.go
Normal file
74
pkg/net/network.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Start Starts the TCP server on the specified address.
|
||||||
|
func Start(addr string, stopChan <-chan struct{}, wg *sync.WaitGroup) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
// Start listening on the specified address
|
||||||
|
listener, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
_, err := fmt.Fprintf(os.Stderr, "Error starting TCP server: %v", err)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the listener when the application closes.
|
||||||
|
defer func(listener net.Listener) {
|
||||||
|
err := listener.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(listener)
|
||||||
|
|
||||||
|
fmt.Printf("TCP server listening on %s\n", addr)
|
||||||
|
|
||||||
|
// Listen for an incoming connection in a goroutine.
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
select {
|
||||||
|
case <-stopChan:
|
||||||
|
return // Safe exit if stop has been signalled
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(os.Stderr, "Error accepting connection: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle connections in a new goroutine.
|
||||||
|
go handleRequest(conn)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Block until we receive a stop signal
|
||||||
|
<-stopChan
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleRequest handles incoming requests.
|
||||||
|
func handleRequest(conn net.Conn) {
|
||||||
|
// Close the connection when you're done with it.
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
// Make a buffer to hold incoming data.
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
|
||||||
|
// Read the incoming connection into the buffer.
|
||||||
|
_, err := conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error reading: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send a response back to person contacting us.
|
||||||
|
conn.Write([]byte("Hello, World!\n"))
|
||||||
|
}
|
||||||
24
steel.go
Normal file
24
steel.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cimeyclust.com/steel/pkg/cmd"
|
||||||
|
"cimeyclust.com/steel/pkg/net"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Channel for stopping the program
|
||||||
|
stopChan := make(chan struct{})
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// Start the console handler
|
||||||
|
wg.Add(1)
|
||||||
|
go cmd.Start(&wg, stopChan)
|
||||||
|
|
||||||
|
// Start the network server
|
||||||
|
wg.Add(1)
|
||||||
|
go net.Start("localhost:8080", stopChan, &wg)
|
||||||
|
|
||||||
|
// Wait for all components to finish
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user