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 "os"
import "sircd"
import "sync"
import "time"
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
import (
"log"
"net"
"os"
"log"
"container/list"
)
type sircd struct {
Running bool
log *log.Logger
netName string
ircPort uint16
httpPort uint16
Running bool
log *log.Logger
netName string
ircPort uint16
httpPort uint16
ircListener net.Listener
ircClients []*clientIRCConnection
ircClients *list.List
}
const (
@ -24,25 +25,26 @@ const (
func NewSircd() *sircd {
s := new(sircd)
s.Running = true
s.Running = true
s.ircClients = list.New()
return s
}
func (s *sircd) SetLogger(l *log.Logger) {
s.log = l
s.log = l
}
func (s *sircd) startIRCServer() {
var err error
var err error
s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
if err != nil {
log.Println("Error listening:", err.Error())
os.Exit(1)
}
defer s.ircListener.Close()
s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT)
}
defer s.ircListener.Close()
s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT)
for {
for {
// Listen for an incoming connection.
conn, err := s.ircListener.Accept()
if err != nil {
@ -52,24 +54,27 @@ func (s *sircd) startIRCServer() {
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() {
go s.startIRCServer()
go s.startIRCServer()
}
func (s *sircd) handleIRCConnection(conn net.Conn) {
c := new(clientIRCConnection)
c.conn = conn
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()
c := newClientIRCConnection(conn, s.log)
s.ircClients.PushBack(c)
}

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
}