Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbae632a

Browse files
committed
refactor: remove unused Bufferflow implementations and simplify buffer handling
1 parentaff40ab commitbae632a

File tree

8 files changed

+15
-253
lines changed

8 files changed

+15
-253
lines changed

‎bufferflow.go‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎bufferflow_default.go‎

Lines changed: 0 additions & 72 deletions
This file was deleted.

‎bufferflow_timed.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type BufferflowTimed struct {
3333
bufferedOutputstring
3434
}
3535

36-
//NewBufferflowTimed will create a new timed bufferflow
37-
funcNewBufferflowTimed(portstring,outputchan<- []byte)*BufferflowTimed {
36+
//NewBufferFlowTimed will create a new timed bufferflow
37+
funcNewBufferFlowTimed(portstring,outputchan<- []byte)*BufferflowTimed {
3838
return&BufferflowTimed{
3939
port:port,
4040
output:output,

‎bufferflow_timedraw.go‎

Lines changed: 0 additions & 89 deletions
This file was deleted.

‎conn.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ func wsHandler() *WsServer {
220220
c:=&connection{send:make(chan []byte,256*10),ws:so}
221221
h.register<-c
222222
so.On("command",func(messagestring) {
223+
fmt.Println("command:",message)
223224
h.broadcast<- []byte(message)
224225
})
225226

‎hub.go‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,8 @@ func checkCmd(m []byte) {
146146
gospErr("Problem converting baud rate "+args[2])
147147
return
148148
}
149-
// pass in buffer type now as string. if user does not
150-
// ask for a buffer type pass in empty string
151-
bufferAlgorithm:="default"// use the default buffer if none is specified
152-
iflen(args)>3 {
153-
// cool. we got a buffer type request
154-
buftype:=strings.Replace(args[3],"\n","",-1)
155-
bufferAlgorithm=buftype
156-
}
157-
fmt.Println("bufferAlgorithm: ",bufferAlgorithm)
158-
gospHandlerOpen(args[1],baud,bufferAlgorithm)
149+
150+
gospHandlerOpen(args[1],baud)
159151

160152
}elseifstrings.HasPrefix(sl,"close") {
161153

‎serial.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var sh = serialhub{
6767
func (sh*serialhub)Register(port*serport) {
6868
sh.mu.Lock()
6969
//log.Print("Registering a port: ", p.portConf.Name)
70-
h.broadcastSys<- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\""+port.portConf.Name+"\",\"Baud\":"+strconv.Itoa(port.portConf.Baud)+",\"BufferType\":\""+port.BufferType+"\"}")
70+
h.broadcastSys<- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\""+port.portConf.Name+"\",\"Baud\":"+strconv.Itoa(port.portConf.Baud)+"\"}")
7171
sh.ports[port]=true
7272
sh.mu.Unlock()
7373
}

‎serialport.go‎

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io"
2222
"strconv"
2323
"time"
24-
"unicode/utf8"
2524

2625
log"github.com/sirupsen/logrus"
2726
serial"go.bug.st/serial"
@@ -56,10 +55,7 @@ type serport struct {
5655
// channel containing raw base64 encoded binary data (outbound messages)
5756
sendRawchanstring
5857

59-
// Do we have an extra channel/thread to watch our buffer?
60-
BufferTypestring
61-
//bufferwatcher *BufferflowDummypause
62-
bufferwatcherBufferflow
58+
bufferFlow*BufferflowTimed
6359
}
6460

6561
// SpPortMessage is the serial port message
@@ -74,10 +70,9 @@ type SpPortMessageRaw struct {
7470
D []byte// the data, i.e. G0 X0 Y0
7571
}
7672

77-
func (p*serport)reader(buftypestring) {
73+
func (p*serport)reader() {
7874

7975
timeCheckOpen:=time.Now()
80-
varbufferedCh bytes.Buffer
8176

8277
serialBuffer:=make([]byte,1024)
8378
for {
@@ -95,39 +90,8 @@ func (p *serport) reader(buftype string) {
9590
// read can return legitimate bytes as well as an error
9691
// so process the n bytes red, if n > 0
9792
ifn>0&&err==nil {
98-
9993
log.Print("Read "+strconv.Itoa(n)+" bytes ch: "+string(bufferPart[:n]))
100-
101-
data:=""
102-
switchbuftype {
103-
case"timedraw","timed":
104-
data=string(bufferPart[:n])
105-
// give the data to our bufferflow so it can do it's work
106-
// to read/translate the data to see if it wants to block
107-
// writes to the serialport. each bufferflow type will decide
108-
// this on its own based on its logic
109-
p.bufferwatcher.OnIncomingData(data)
110-
case"default":// the bufferbuftype is actually called default 🤷‍♂️
111-
// save the left out bytes for the next iteration due to UTF-8 encoding
112-
bufferPart=append(bufferedCh.Bytes(),bufferPart[:n]...)
113-
n+=len(bufferedCh.Bytes())
114-
bufferedCh.Reset()
115-
fori,w:=0,0;i<n;i+=w {
116-
runeValue,width:=utf8.DecodeRune(bufferPart[i:n])// try to decode the first i bytes in the buffer (UTF8 runes do not have a fixed length)
117-
ifruneValue==utf8.RuneError {
118-
bufferedCh.Write(bufferPart[i:n])
119-
break
120-
}
121-
ifi==n {
122-
bufferedCh.Reset()
123-
}
124-
data+=string(runeValue)
125-
w=width
126-
}
127-
p.bufferwatcher.OnIncomingData(data)
128-
default:
129-
log.Panicf("unknown buffer type %s",buftype)
130-
}
94+
p.bufferFlow.OnIncomingData(string(bufferPart[:n]))
13195
}
13296

13397
// double check that we got characters in the buffer
@@ -272,7 +236,7 @@ func (p *serport) writerRaw() {
272236
h.broadcastSys<- []byte(msgstr)
273237
}
274238

275-
funcspHandlerOpen(portnamestring,baudint,buftypestring) {
239+
funcspHandlerOpen(portnamestring,baudint) {
276240

277241
log.Print("Inside spHandler")
278242

@@ -311,23 +275,12 @@ func spHandlerOpen(portname string, baud int, buftype string) {
311275
portConf:conf,
312276
portIo:sp,
313277
portName:portname,
314-
BufferType:buftype}
315-
316-
varbwBufferflow
317-
318-
switchbuftype {
319-
case"timed":
320-
bw=NewBufferflowTimed(portname,h.broadcastSys)
321-
case"timedraw":
322-
bw=NewBufferflowTimedRaw(portname,h.broadcastSys)
323-
case"default":
324-
bw=NewBufferflowDefault(portname,h.broadcastSys)
325-
default:
326-
log.Panicf("unknown buffer type: %s",buftype)
327278
}
328279

280+
bw:=NewBufferFlowTimed(portname,h.broadcastSys)
329281
bw.Init()
330-
p.bufferwatcher=bw
282+
283+
p.bufferFlow=bw
331284

332285
sh.Register(p)
333286
defersh.Unregister(p)
@@ -342,14 +295,14 @@ func spHandlerOpen(portname string, baud int, buftype string) {
342295
// this is thread to send to serial port but with base64 decoding
343296
gop.writerRaw()
344297

345-
p.reader(buftype)
298+
p.reader()
346299

347300
serialPorts.List()
348301
}
349302

350303
func (p*serport)Close() {
351304
p.isClosing=true
352-
p.bufferwatcher.Close()
305+
p.bufferFlow.Close()
353306
p.portIo.Close()
354307
serialPorts.MarkPortAsClosed(p.portName)
355308
serialPorts.List()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp