Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of TCP #26

Merged
merged 11 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
listen & serve tcp
  • Loading branch information
NoelM committed Feb 7, 2025
commit c56724d7d57c800948f1e00898053f4d38f2b194
4 changes: 2 additions & 2 deletions notel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func main() {
group.Add(1)
go serveWebSocket(&group, connConfig, metrics)

case "telnet":
case "tcp":
group.Add(1)
go serveTelnet(&group, connConfig, metrics)
go serveTCP(&group, connConfig, metrics)
}
}
group.Wait()
Expand Down
57 changes: 57 additions & 0 deletions notel/tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"log"
"net"
"sync"

"github.com/NoelM/minigo"
"github.com/NoelM/minigo/notel/confs"
"github.com/NoelM/minigo/notel/logs"
)

func serveTCP(wg *sync.WaitGroup, connConf confs.ConnectorConf, metrics *Metrics) {
defer wg.Done()

handler := func(conn net.Conn) {
tcp, _ := minigo.NewTCP(conn)
_ = tcp.Init()

var innerWg sync.WaitGroup
innerWg.Add(2)

network := minigo.NewNetwork(tcp, false, &innerWg, "TCP")
m := minigo.NewMinitel(network, false, &innerWg, connConf.Tag, metrics.ConnLostCount)
m.NoCSI()
go m.Serve()

NotelApplication(m, &innerWg, &connConf, metrics)
innerWg.Wait()

logs.InfoLog("[%s] serve-TCP: disconnect\n", connConf.Tag)
tcp.Disconnect()

logs.InfoLog("[%s] serve-TCP: session closed\n", connConf.Tag)
}

err := listenAndServeTCP(connConf.Path, handler)
log.Fatal(err)
}

func listenAndServeTCP(path string, handler func(net.Conn)) error {

listener, err := net.Listen("tcp", path)
if err != nil {
log.Fatal(err)
}
defer listener.Close()

for {
conn, err := listener.Accept()
if err != nil {
return err
}

go handler(conn)
}
}
50 changes: 0 additions & 50 deletions notel/telnet.go

This file was deleted.

19 changes: 9 additions & 10 deletions telnet.go → tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ package minigo

import (
"net"
"time"
)

type Telnet struct {
type TCP struct {
conn net.Conn

connected bool
}

func NewTelnet(conn net.Conn) (*Telnet, error) {
return &Telnet{
func NewTCP(conn net.Conn) (*TCP, error) {
return &TCP{
conn: conn,
}, nil
}

func (t *Telnet) Init() error {
func (t *TCP) Init() error {
t.connected = true
return nil
}

func (t *Telnet) Write(b []byte) error {
func (t *TCP) Write(b []byte) error {
_, err := t.conn.Write(b)

if err != nil {
Expand All @@ -33,9 +32,8 @@ func (t *Telnet) Write(b []byte) error {
return nil
}

func (t *Telnet) Read() ([]byte, error) {
func (t *TCP) Read() ([]byte, error) {
msg := make([]byte, 256)
t.conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
n, err := t.conn.Read(msg)

if err != nil {
Expand All @@ -46,10 +44,11 @@ func (t *Telnet) Read() ([]byte, error) {
return msg[:n], nil
}

func (t *Telnet) Connected() bool {
func (t *TCP) Connected() bool {
return t.connected
}

func (t *Telnet) Disconnect() {
func (t *TCP) Disconnect() {
t.conn.Close()
t.connected = false
}