Explorar el Código

starting to clean up code

Keelan Lightfoot hace 7 años
padre
commit
40295fc47b
Se han modificado 3 ficheros con 176 adiciones y 180 borrados
  1. 29
    0
      README.md
  2. 32
    180
      main.go
  3. 115
    0
      netconn.go

+ 29
- 0
README.md Ver fichero

@@ -0,0 +1,29 @@
1
+# rttymodem
2
+This is a backwards modem to connect a teletype to the internet via a terminal unit and your computer's audio interface. The modem supports a subset of the Hayes modem command set, and is backwards because the DTE is on the modulated side of the modem. The DCE side of the modem supports communication over over raw TCP sockets.
3
+
4
+## S Registers
5
+
6
+### 0 - Auto Answer
7
+Default: 0 (Disabled)
8
+
9
+When set to 1, the modem allows inbound TCP connections
10
+
11
+### 2 - Escape Code
12
+Default: ?
13
+The character 
14
+
15
+### 91 - Modulator Enable
16
+Default: 1 (Enabled)
17
+
18
+When set to 1, the modulator audio output is enabled
19
+
20
+### 92 - Turnaround Delay
21
+Default: 0
22
+
23
+This only applies to AT commands entered at the modem interface; this is the delay in milliseconds between submitting a command, and when the response is sent.
24
+
25
+### 93 - Un-shift On Space
26
+Default: Enabled
27
+
28
+### 94 - TCP Port
29
+Default: 3333

+ 32
- 180
main.go Ver fichero

@@ -4,16 +4,12 @@ import (
4 4
 	"fmt"
5 5
 
6 6
 	"bufio"
7
-	"bytes"
8 7
 	"github.com/gordonklaus/portaudio"
9 8
 	"github.com/naleek/gortty"
10 9
 	"io"
11
-	"net"
12 10
 	"regexp"
13 11
 	"strings"
14
-	"unicode/utf8"
15 12
 	"time"
16
-	"os"
17 13
 	"strconv"
18 14
 )
19 15
 
@@ -56,7 +52,6 @@ func main() {
56 52
 	if !useStdio {
57 53
 		// Set up modulator pipeline
58 54
 		ttyToEncoder := make(chan rune, 100)
59
-		m.ttyWriter = NewModemWriter(ttyToEncoder)
60 55
 		encoderToModulator := make(chan byte)
61 56
 		m.encoder = gortty.NewEncoder(ttyToEncoder, encoderToModulator, charset)
62 57
 		m.modulator = gortty.NewModulator(rttySettings, encoderToModulator, modulatorToAudio)
@@ -67,10 +62,6 @@ func main() {
67 62
 		m.demodulator = gortty.NewDemodulator(rttySettings, audioToDemodulator, demodulatorToDecoder)
68 63
 		decoderToTTY := make(chan rune)
69 64
 		m.decoder = gortty.NewDecoder(demodulatorToDecoder, decoderToTTY, charset)
70
-		m.ttyReader = NewModemReader(decoderToTTY)
71
-	} else {
72
-		m.ttyReader = os.Stdin
73
-		m.ttyWriter = os.Stdout
74 65
 	}
75 66
 		
76 67
 	go m.cli()
@@ -103,54 +94,22 @@ func main() {
103 94
 	
104 95
 }
105 96
 
106
-type ModemReader struct {
107
-	input chan rune
108
-}
109
-
110
-func NewModemReader(c chan rune) *ModemReader {
111
-	m := &ModemReader{}
112
-	m.input = c
113
-	return m
114
-}
115
-
116
-func (m *ModemReader) Read(p []byte) (n int, err error) {
117
-	r := <-m.input
118
-
119
-	fmt.Print(string(r))
120
-
121
-	l := utf8.EncodeRune(p, r)
122
-	return l, nil
123
-}
124
-
125
-func (m *ModemReader) Add(b rune) {
126
-	m.input <- b
127
-}
128
-
129
-type ModemWriter struct {
130
-	output chan rune
131
-}
132
-
133
-func NewModemWriter(c chan rune) *ModemWriter {
134
-	m := &ModemWriter{}
135
-	m.output = c
136
-	return m
137
-}
138
-
139
-func (m *ModemWriter) Write(p []byte) (n int, err error) {
140
-
141
-	runes := bytes.Runes(p)
142
-
143
-	fmt.Print(string(runes))
144
-	
145
-	for _, r := range runes {
146
-		m.output <- r
147
-	}
148
-	return len(p), nil
149
-}
97
+//type ModemReader struct {
98
+//	input chan rune
99
+//}
100
+//
101
+//func NewModemReader(c chan rune) *ModemReader {
102
+//	m := &ModemReader{}
103
+//	m.input = c
104
+//	return m
105
+//}
106
+//
107
+//func (m *ModemReader) Read(p []byte) (n int, err error) {
108
+//	r := <-m.input
109
+//	l := utf8.EncodeRune(p, r)
110
+//	return l, nil
111
+//}
150 112
 
151
-func (m *ModemWriter) Get() rune {
152
-	return <-m.output
153
-}
154 113
 
155 114
 type modem struct {
156 115
 	ttyReader io.Reader
@@ -288,7 +247,7 @@ func (m *modem) cli() {
288 247
 	}
289 248
 	
290 249
 	localEcho := false
291
-	ttyBufferedReader := bufio.NewReader(m.ttyReader)
250
+	ttyBufferedReader := bufio.NewReader(m.decoder)
292 251
 	
293 252
 	var netConn *NetConn
294 253
 	
@@ -333,23 +292,23 @@ func (m *modem) cli() {
333 292
 			parameter = params[5]
334 293
 		}
335 294
 
336
-		//fmt.Fprintf(m.ttyWriter, "command: '%s' register: '%s' query: '%s' value: '%s' parameter: '%s'\n", command, register, registerQuery, value, parameter)
295
+		//fmt.Fprintf(m.encoder, "command: '%s' register: '%s' query: '%s' value: '%s' parameter: '%s'\n", command, register, registerQuery, value, parameter)
337 296
 		switch command {
338 297
 		case "?":
339
-			fmt.Fprintf(m.ttyWriter, "Setting Registers\n")
298
+			fmt.Fprintf(m.encoder, "Setting Registers\n")
340 299
 			for s, conf := range settings {
341
-				fmt.Fprintf(m.ttyWriter, "%s: %s (current value: %s)\n", s, conf.help, conf.f(registerGet, ""))
300
+				fmt.Fprintf(m.encoder, "%s: %s (current value: %s)\n", s, conf.help, conf.f(registerGet, ""))
342 301
 			}
343 302
 		case "O": // online
344 303
 			if !netConn.Connected() {
345
-				fmt.Fprintln(m.ttyWriter, "ERROR No active session")
304
+				fmt.Fprintln(m.encoder, "ERROR No active session")
346 305
 				break
347 306
 			}
348
-			fmt.Fprintln(m.ttyWriter, "OK")
349
-			dataMode(netConn, ttyBufferedReader, m.ttyWriter, escapeRune)
307
+			fmt.Fprintln(m.encoder, "OK")
308
+			dataMode(netConn, ttyBufferedReader, m.encoder, escapeRune)
350 309
 		case "D": // dial
351 310
 			if netConn.Connected() {
352
-				fmt.Fprintln(m.ttyWriter, "ERROR Session in progress")
311
+				fmt.Fprintln(m.encoder, "ERROR Session in progress")
353 312
 				break
354 313
 			}
355 314
 			addr := strings.TrimSpace(parameter)
@@ -357,30 +316,30 @@ func (m *modem) cli() {
357 316
 			var err error
358 317
 			netConn, err = NewNetConn(addr)
359 318
 			if err != nil {
360
-				fmt.Fprintln(m.ttyWriter, "ERROR", err)
319
+				fmt.Fprintln(m.encoder, "ERROR", err)
361 320
 				break
362 321
 			} else {
363
-				fmt.Fprintln(m.ttyWriter, "CONNECTED")
364
-				dataMode(netConn, ttyBufferedReader, m.ttyWriter, escapeRune)
322
+				fmt.Fprintln(m.encoder, "CONNECTED")
323
+				dataMode(netConn, ttyBufferedReader, m.encoder, escapeRune)
365 324
 			}
366 325
 		case "H": // hangup
367 326
 			if !netConn.Connected() {
368
-				fmt.Fprintln(m.ttyWriter, "ERROR No active session")
327
+				fmt.Fprintln(m.encoder, "ERROR No active session")
369 328
 				break
370 329
 			}
371 330
 			netConn.Hangup()
372
-			fmt.Fprintln(m.ttyWriter, "OK")
331
+			fmt.Fprintln(m.encoder, "OK")
373 332
 		case "E": // local echo
374 333
 			localEcho = (register == "1")
375
-			fmt.Fprintln(m.ttyWriter, "local echo", localEcho)
334
+			fmt.Fprintln(m.encoder, "local echo", localEcho)
376 335
 		case "S": // local echo
377 336
 			if conf, ok := settings[register]; ok {
378 337
 				if registerQuery == "?" {
379
-					fmt.Fprintln(m.ttyWriter, conf.f(registerGet, ""))
338
+					fmt.Fprintln(m.encoder, conf.f(registerGet, ""))
380 339
 				} else if value != "" {
381
-					fmt.Fprintln(m.ttyWriter, conf.f(registerSet, value))
340
+					fmt.Fprintln(m.encoder, conf.f(registerSet, value))
382 341
 				} else {
383
-					fmt.Fprintln(m.ttyWriter, "ERROR")
342
+					fmt.Fprintln(m.encoder, "ERROR")
384 343
 				}
385 344
 			}
386 345
 		}
@@ -474,113 +433,6 @@ pipeLoop:
474 433
 
475 434
 
476 435
 
477
-type NetConn struct {
478
-	netReader *bufio.Reader
479
-	conn net.Conn
480
-	netReadChan chan rune
481
-	hangup chan struct{}
482
-	paused  bool
483
-	connected bool
484
-}
485
-
486
-func NewNetConn(addr string) (*NetConn, error) {
487
-	c := &NetConn{}
488
-	var err error
489
-	c.conn, err = net.Dial("tcp", addr)
490
-	if err != nil {
491
-		return nil, err
492
-	}
493
-	c.connected = true
494
-
495
-	c.netReader = bufio.NewReader(c.conn)
496
-	
497
-	c.netReadChan = make(chan rune)
498
-	c.hangup = make(chan struct{})
499
-	c.paused = false
500
-	
501
-	go func() {
502
-		for {
503
-			r, l, err := c.netReader.ReadRune()
504
-			if err != nil && err != io.EOF {
505
-				c.cleanup()
506
-				return
507
-			}
508
-			if c.paused || l == 0 {
509
-				select {
510
-				case <- c.hangup:
511
-					c.cleanup()
512
-					return
513
-				default: // discard
514
-				}
515
-			} else {
516
-				select {
517
-				case c.netReadChan <- r:
518
-				case <- c.hangup:
519
-					c.cleanup()
520
-					return
521
-				}
522
-			}
523
-		}
524
-		
525
-	}()
526
-	
527
-	
528
-	return c, nil
529
-}
530
-
531
-func (c *NetConn) Write(r rune) error {
532
-	if c == nil {
533
-		return fmt.Errorf("NetConn not initialized")
534
-	}
535
-	_, err := c.conn.Write([]byte(string(r)))
536
-	if err != nil {	
537
-		close(c.hangup)
538
-	}
539
-	return err
540
-}
541
-
542
-func (c *NetConn) Hangup() {
543
-	if c == nil {
544
-		return
545
-	}
546
-	c.conn.Close()
547
-	close(c.hangup)
548
-}
549
-
550
-func (c *NetConn) cleanup() {
551
-	close(c.netReadChan)
552
-	c.connected = false 
553
-}
554
-
555
-func (c *NetConn) Pause() {
556
-	if c == nil {
557
-		return
558
-	}
559
-	c.paused = true
560
-}
561
-
562
-func (c *NetConn) Resume() {
563
-	if c == nil {
564
-		return
565
-	}
566
-	c.paused = false
567
-}
568
-
569
-func (c *NetConn) OutputChannel() chan rune {
570
-	if c == nil {
571
-		return nil
572
-	}
573
-	return c.netReadChan
574
-}
575
-
576
-func (c *NetConn) Connected() bool {
577
-	if c == nil {
578
-		return false
579
-	}
580
-	return c.connected
581
-}
582
-
583
-
584 436
 
585 437
 
586 438
 

+ 115
- 0
netconn.go Ver fichero

@@ -0,0 +1,115 @@
1
+package main
2
+
3
+import (
4
+	"bufio"
5
+	"net"
6
+	"io"
7
+	"fmt"
8
+)
9
+
10
+type NetConn struct {
11
+	netReader *bufio.Reader
12
+	conn net.Conn
13
+	netReadChan chan rune
14
+	hangup chan struct{}
15
+	paused  bool
16
+	connected bool
17
+}
18
+
19
+func NewNetConn(addr string) (*NetConn, error) {
20
+	c := &NetConn{}
21
+	var err error
22
+	c.conn, err = net.Dial("tcp", addr)
23
+	if err != nil {
24
+		return nil, err
25
+	}
26
+	c.connected = true
27
+
28
+	c.netReader = bufio.NewReader(c.conn)
29
+	
30
+	c.netReadChan = make(chan rune)
31
+	c.hangup = make(chan struct{})
32
+	c.paused = false
33
+	
34
+	go func() {
35
+		for {
36
+			r, l, err := c.netReader.ReadRune()
37
+			if err != nil && err != io.EOF {
38
+				c.cleanup()
39
+				return
40
+			}
41
+			if c.paused || l == 0 {
42
+				select {
43
+				case <- c.hangup:
44
+					c.cleanup()
45
+					return
46
+				default: // discard
47
+				}
48
+			} else {
49
+				select {
50
+				case c.netReadChan <- r:
51
+				case <- c.hangup:
52
+					c.cleanup()
53
+					return
54
+				}
55
+			}
56
+		}
57
+		
58
+	}()
59
+	
60
+	
61
+	return c, nil
62
+}
63
+
64
+func (c *NetConn) Write(r rune) error {
65
+	if c == nil {
66
+		return fmt.Errorf("NetConn not initialized")
67
+	}
68
+	_, err := c.conn.Write([]byte(string(r)))
69
+	if err != nil {	
70
+		close(c.hangup)
71
+	}
72
+	return err
73
+}
74
+
75
+func (c *NetConn) Hangup() {
76
+	if c == nil {
77
+		return
78
+	}
79
+	c.conn.Close()
80
+	close(c.hangup)
81
+}
82
+
83
+func (c *NetConn) cleanup() {
84
+	close(c.netReadChan)
85
+	c.connected = false 
86
+}
87
+
88
+func (c *NetConn) Pause() {
89
+	if c == nil {
90
+		return
91
+	}
92
+	c.paused = true
93
+}
94
+
95
+func (c *NetConn) Resume() {
96
+	if c == nil {
97
+		return
98
+	}
99
+	c.paused = false
100
+}
101
+
102
+func (c *NetConn) OutputChannel() chan rune {
103
+	if c == nil {
104
+		return nil
105
+	}
106
+	return c.netReadChan
107
+}
108
+
109
+func (c *NetConn) Connected() bool {
110
+	if c == nil {
111
+		return false
112
+	}
113
+	return c.connected
114
+}
115
+