diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..359bdbf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/steel.exe diff --git a/README.md b/README.md index e69de29..5dba4f5 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,2 @@ +# Steel +Steel is associated with stability and robustness. And this is all this cross-platform Minecraft Server-Software is about. \ No newline at end of file diff --git a/go.mod b/go.mod index c3c6117..e4710c6 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module Steel +module cimeyclust.com/steel go 1.21 diff --git a/pkg/cmd/console.go b/pkg/cmd/console.go new file mode 100644 index 0000000..3c4a209 --- /dev/null +++ b/pkg/cmd/console.go @@ -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) + } + } +} diff --git a/pkg/net/network.go b/pkg/net/network.go new file mode 100644 index 0000000..25a12d1 --- /dev/null +++ b/pkg/net/network.go @@ -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")) +} diff --git a/steel.go b/steel.go new file mode 100644 index 0000000..a8f8b9c --- /dev/null +++ b/steel.go @@ -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() +}