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,9 +1,10 @@
package sircd
import (
"log"
"net"
"os"
"log"
"container/list"
)
type sircd struct {
@ -13,7 +14,7 @@ type sircd struct {
ircPort uint16
httpPort uint16
ircListener net.Listener
ircClients []*clientIRCConnection
ircClients *list.List
}
const (
@ -25,6 +26,7 @@ const (
func NewSircd() *sircd {
s := new(sircd)
s.Running = true
s.ircClients = list.New()
return s
}
@ -52,7 +54,20 @@ 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() {
@ -60,16 +75,6 @@ func (s *sircd) Start() {
}
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
}