modularize
This commit is contained in:
parent
c72ee3e495
commit
3fcdd86089
10
src/main.go
10
src/main.go
@ -1,12 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
import "os"
|
||||||
import "sircd"
|
import "sircd"
|
||||||
|
import "sync"
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
l := log.New(os.Stdout, "", log.Ldate | log.Lmicroseconds | log.Lshortfile)
|
||||||
|
l.Println("sircd starting up")
|
||||||
s := sircd.NewSircd()
|
s := sircd.NewSircd()
|
||||||
s.Start()
|
s.SetLogger(l)
|
||||||
|
go s.Start()
|
||||||
for s.Running {
|
for s.Running {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
22
src/sircd/client.go
Normal file
22
src/sircd/client.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package sircd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type clientIRCConnection struct {
|
||||||
|
conn net.Conn
|
||||||
|
log *log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *clientIRCConnection) Read() *[]byte {
|
||||||
|
buf := make([]byte, 1024*1024)
|
||||||
|
// Read the incoming connection into the buffer.
|
||||||
|
_, err := c.conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
s.log.Println("Error reading:", err.Error())
|
||||||
|
}
|
||||||
|
return &buf
|
||||||
|
}
|
@ -1,13 +1,14 @@
|
|||||||
package sircd
|
package sircd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sircd struct {
|
type sircd struct {
|
||||||
Running bool
|
Running bool
|
||||||
|
log *log.Logger
|
||||||
netName string
|
netName string
|
||||||
ircPort uint16
|
ircPort uint16
|
||||||
httpPort uint16
|
httpPort uint16
|
||||||
@ -15,10 +16,6 @@ type sircd struct {
|
|||||||
ircClients []*clientIRCConnection
|
ircClients []*clientIRCConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientIRCConnection struct {
|
|
||||||
conn net.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CONN_HOST = "localhost"
|
CONN_HOST = "localhost"
|
||||||
CONN_PORT = "6667"
|
CONN_PORT = "6667"
|
||||||
@ -31,49 +28,48 @@ func NewSircd() *sircd {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sircd) startServer() {
|
func (s *sircd) SetLogger(l *log.Logger) {
|
||||||
|
s.log = l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sircd) startIRCServer() {
|
||||||
var err error
|
var err error
|
||||||
s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error listening:", err.Error())
|
log.Println("Error listening:", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer s.ircListener.Close()
|
defer s.ircListener.Close()
|
||||||
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
|
s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT)
|
||||||
fmt.Printf("%T\n",s.ircListener)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Listen for an incoming connection.
|
// Listen for an incoming connection.
|
||||||
conn, err := s.ircListener.Accept()
|
conn, err := s.ircListener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error accepting: ", err.Error())
|
s.log.Fatalf("Error accepting: ", err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
// Handle connections in a new goroutine.
|
// Handle connections in a new goroutine.
|
||||||
go handleRequest(conn)
|
go s.handleIRCConnection(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sircd) Start() {
|
func (s *sircd) Start() {
|
||||||
go func() {
|
go s.startIRCServer()
|
||||||
s.startServer()
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles incoming requests.
|
func (s *sircd) handleIRCConnection(conn net.Conn) {
|
||||||
func handleRequest(conn net.Conn) {
|
c := new(clientIRCConnection)
|
||||||
// Make a buffer to hold incoming data.
|
c.conn = conn
|
||||||
buf := make([]byte, 1024)
|
c.log = s.log
|
||||||
// Read the incoming connection into the buffer.
|
|
||||||
byteLen, err := conn.Read(buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error reading:", err.Error())
|
c.log.Println("Error reading:", err.Error())
|
||||||
}
|
}
|
||||||
// Send a response back to person contacting us.
|
// Send a response back to person contacting us.
|
||||||
conn.Write([]byte("Message received."))
|
conn.Write([]byte("Message received."))
|
||||||
fmt.Println("Received message (%i bytes): %s", byteLen, buf)
|
s.log.Println("Received message (%i bytes): %s", byteLen, buf)
|
||||||
// Close the connection when you're done with it.
|
// Close the connection when you're done with it.
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user