Skip to content

Commit

Permalink
added ctrl-c interrupt handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackneill committed Jul 13, 2013
1 parent 315f3ef commit 6f5beaf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@

package main

import (
"fmt"
"os"
"os/signal"
)

func main() {
go beforeShutdown()
loadConfig()
db = connectToSql()
defer db.Close()
go connectToSocket("localhost:36200")
loadServer(":" + config["Port"].(string))
}

func beforeShutdown() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
s := <-c
fmt.Println("Got signal:", s)
shutdownSocket()
os.Exit(1)
}
17 changes: 14 additions & 3 deletions socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func listenToSocket() {
if err != nil {
fmt.Println(err)
}
go handlePacket(string(buffer[:n]), n)
handlePacket(string(buffer[:n]), n)
}
}

Expand All @@ -78,16 +78,27 @@ func handlePacket(data string, size int) {
case SMSG_AUTH_DENIED:
fmt.Print("Auth request denied.")
case SMSG_CLOSE_CONNECTION:
fmt.Print("Server closed the connection.")
fmt.Print("Server sent closing signal. Connection closed.")
conn.Close()
default:
fmt.Print("Unknown opcode.")
}
fmt.Println(" --")
fmt.Println(packet[1])
fmt.Println(packet[1:])
fmt.Println("-- END PACKET --")
}

func shutdownSocket() {
fmt.Println("Shutting down socket connection...")
sendCloseSignal()
conn.Close()
}

func sendCloseSignal() {
msg := strconv.Itoa(CMSG_CLOSE_CONNECTION) + PACKET_SEPARATOR + "uh. stomachache. shutting down for now." + PACKET_SEPARATOR
fmt.Fprint(conn, msg)
}

func regConnection() {
msg := strconv.Itoa(CMSG_REQUEST_AUTH) + PACKET_SEPARATOR + "schumix webadmin (reg GUID)" + PACKET_SEPARATOR + md5_gen("schumix") + PACKET_SEPARATOR
fmt.Fprint(conn, msg)
Expand Down

0 comments on commit 6f5beaf

Please sign in to comment.