This commit is contained in:
Jeffrey Paul 2019-08-19 19:50:40 -05:00
parent 3fcdd86089
commit a3b2ad1703
4 changed files with 73 additions and 51 deletions

View File

@ -3,7 +3,6 @@ package main
import "log" import "log"
import "os" import "os"
import "sircd" import "sircd"
import "sync"
import "time" import "time"
func main() { func main() {

40
sircd/client.go Normal file
View File

@ -0,0 +1,40 @@
package sircd
import (
"log"
"net"
"bytes"
)
func newClientIRCConnection(conn net.Conn, log *log.Logger) *clientIRCConnection {
c := new(clientIRCConnection)
c.conn = conn
c.log = log
c.inputBytes = new(bytes.Buffer)
c.outputBytes = new(bytes.Buffer)
c.ReadSocket()
return c
}
type clientIRCConnection struct {
Id uint64
conn net.Conn
log *log.Logger
inputBytes *bytes.Buffer
outputBytes *bytes.Buffer
}
func (c *clientIRCConnection) ReadSocket() {
// Read the incoming connection into the buffer.
buf := make([]byte, 1024*1024)
bytesRead, err := c.conn.Read(buf)
c.log.Printf("conn<%d>: read %d bytes from net", c.Id, bytesRead)
if err != nil {
c.log.Println("Error reading:", err.Error())
}
c.inputBytes.Write(buf)
}
func (c *clientIRCConnection) ParseMessages() {
c.log.Fatalln("not implemented")
}

View File

@ -1,19 +1,20 @@
package sircd package sircd
import ( import (
"log"
"net" "net"
"os" "os"
"log" "container/list"
) )
type sircd struct { type sircd struct {
Running bool Running bool
log *log.Logger log *log.Logger
netName string netName string
ircPort uint16 ircPort uint16
httpPort uint16 httpPort uint16
ircListener net.Listener ircListener net.Listener
ircClients []*clientIRCConnection ircClients *list.List
} }
const ( const (
@ -24,25 +25,26 @@ const (
func NewSircd() *sircd { func NewSircd() *sircd {
s := new(sircd) s := new(sircd)
s.Running = true s.Running = true
s.ircClients = list.New()
return s return s
} }
func (s *sircd) SetLogger(l *log.Logger) { func (s *sircd) SetLogger(l *log.Logger) {
s.log = l s.log = l
} }
func (s *sircd) startIRCServer() { 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 {
log.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()
s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT) s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT)
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 {
@ -52,24 +54,27 @@ func (s *sircd) startIRCServer() {
go s.handleIRCConnection(conn) go s.handleIRCConnection(conn)
} }
}
func (s *sircd) processIRC() {
for {
s.readFromClients()
}
}
func (s *sircd) readFromClients() {
for e := s.ircClients.Front(); e != nil; e = e.Next() {
client := e.Value.(clientIRCConnection)
client.ReadSocket()
client.ParseMessages()
}
} }
func (s *sircd) Start() { func (s *sircd) Start() {
go s.startIRCServer() go s.startIRCServer()
} }
func (s *sircd) handleIRCConnection(conn net.Conn) { func (s *sircd) handleIRCConnection(conn net.Conn) {
c := new(clientIRCConnection) c := newClientIRCConnection(conn, s.log)
c.conn = conn s.ircClients.PushBack(c)
c.log = s.log
if err != nil {
c.log.Println("Error reading:", err.Error())
}
// Send a response back to person contacting us.
conn.Write([]byte("Message received."))
s.log.Println("Received message (%i bytes): %s", byteLen, buf)
// Close the connection when you're done with it.
conn.Close()
} }

View File

@ -1,22 +0,0 @@
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
}