|
@@ -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
|
|