This commit is contained in:
Jeffrey Paul 2019-08-19 18:30:59 -05:00
commit c72ee3e495
3 changed files with 97 additions and 0 deletions

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
GOPATH := $(shell pwd)
default: run
run: src/*.go
go run src/*.go

12
src/main.go Normal file
View File

@ -0,0 +1,12 @@
package main
import "sircd"
import "time"
func main() {
s := sircd.NewSircd()
s.Start()
for s.Running {
time.Sleep(100 * time.Millisecond)
}
}

79
src/sircd/sircd.go Normal file
View File

@ -0,0 +1,79 @@
package sircd
import (
"fmt"
"net"
"os"
)
type sircd struct {
Running bool
netName string
ircPort uint16
httpPort uint16
ircListener net.Listener
ircClients []*clientIRCConnection
}
type clientIRCConnection struct {
conn net.Conn
}
const (
CONN_HOST = "localhost"
CONN_PORT = "6667"
CONN_TYPE = "tcp"
)
func NewSircd() *sircd {
s := new(sircd)
s.Running = true
return s
}
func (s *sircd) startServer() {
var err error
s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer s.ircListener.Close()
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
fmt.Printf("%T\n",s.ircListener)
for {
// Listen for an incoming connection.
conn, err := s.ircListener.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}
func (s *sircd) Start() {
go func() {
s.startServer()
}()
}
// Handles incoming requests.
func handleRequest(conn net.Conn) {
// Make a buffer to hold incoming data.
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
byteLen, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
// Send a response back to person contacting us.
conn.Write([]byte("Message received."))
fmt.Println("Received message (%i bytes): %s", byteLen, buf)
// Close the connection when you're done with it.
conn.Close()
}