mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
Apply coding style
This commit is contained in:
parent
273c8f6c57
commit
bfb4e0d9b0
@ -24,60 +24,56 @@ type wsconn struct {
|
|||||||
|
|
||||||
func (ws *wsconn) Read(b []byte) (n int, err error) {
|
func (ws *wsconn) Read(b []byte) (n int, err error) {
|
||||||
ws.rlock.Lock()
|
ws.rlock.Lock()
|
||||||
|
n, err = ws.read(b)
|
||||||
|
ws.rlock.Unlock()
|
||||||
|
return n, err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *wsconn) read(b []byte) (n int, err error) {
|
||||||
|
|
||||||
if ws.connClosing {
|
if ws.connClosing {
|
||||||
|
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
getNewBuffer := func() error {
|
|
||||||
_, r, err := ws.wsc.NextReader()
|
n, err = ws.readNext(b)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *wsconn) getNewReadBuffer() error {
|
||||||
|
_, r, err := ws.wsc.NextReader()
|
||||||
|
if err != nil {
|
||||||
|
log.Warning("WS transport: ws connection NewFrameReader return " + err.Error())
|
||||||
|
ws.connClosing = true
|
||||||
|
ws.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ws.readBuffer = bufio.NewReader(r)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *wsconn) readNext(b []byte) (n int, err error) {
|
||||||
|
if ws.readBuffer == nil {
|
||||||
|
err = ws.getNewReadBuffer()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warning("WS transport: ws connection NewFrameReader return " + err.Error())
|
return 0, err
|
||||||
ws.connClosing = true
|
|
||||||
ws.Close()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
ws.readBuffer = bufio.NewReader(r)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*It seems golang's support for recursive in anonymous func is yet to complete.
|
n, err = ws.readBuffer.Read(b)
|
||||||
func1:=func(){
|
|
||||||
func1()
|
|
||||||
}
|
|
||||||
won't work, failed to compile for it can't find func1.
|
|
||||||
|
|
||||||
Should following workaround panic,
|
if err == nil {
|
||||||
readNext could have been called before the actual defination was made,
|
|
||||||
This is very unlikely.
|
|
||||||
*/
|
|
||||||
readNext := func(b []byte) (n int, err error) { panic("Runtime unstable. Please report this bug to developer.") }
|
|
||||||
|
|
||||||
readNext = func(b []byte) (n int, err error) {
|
|
||||||
if ws.readBuffer == nil {
|
|
||||||
err = getNewBuffer()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err = ws.readBuffer.Read(b)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == io.EOF {
|
|
||||||
ws.readBuffer = nil
|
|
||||||
if n == 0 {
|
|
||||||
return readNext(b)
|
|
||||||
}
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
return n, err
|
return n, err
|
||||||
|
|
||||||
}
|
}
|
||||||
n, err = readNext(b)
|
|
||||||
ws.rlock.Unlock()
|
if err == io.EOF {
|
||||||
|
ws.readBuffer = nil
|
||||||
|
if n == 0 {
|
||||||
|
return ws.readNext(b)
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
return n, err
|
return n, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user