pull/238/head
Shelikhoo 8 years ago
parent 3e9e9b520e
commit a0fda39274
No known key found for this signature in database
GPG Key ID: 7791BDB0709ABD21

@ -2,6 +2,7 @@ package ws
import ( import (
"bufio" "bufio"
"fmt"
"io" "io"
"net" "net"
"sync" "sync"
@ -23,7 +24,7 @@ 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()
//defer ws.rlock.Unlock() //defer ws.rlock.Unlock()
//ws.checkifRWAfterClosing() //ws.checkifRWAfterClosing()
if ws.connClosing { if ws.connClosing {
@ -42,7 +43,19 @@ func (ws *wsconn) Read(b []byte) (n int, err error) {
return nil return nil
} }
readNext := func(b []byte) (n int, err error) { /*It seems golang's support for recursive in anonymous func it yet to complete.
func1:=func(){
func1()
}
won't work, failed to compile for it can't find func1.
Should following work around panic,
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 { if ws.readBuffer == nil {
err = getNewBuffer() err = getNewBuffer()
if err != nil { if err != nil {
@ -60,7 +73,7 @@ func (ws *wsconn) Read(b []byte) (n int, err error) {
if err == io.EOF { if err == io.EOF {
ws.readBuffer = nil ws.readBuffer = nil
if n == 0 { if n == 0 {
return ws.Read(b) return readNext(b)
} }
return n, nil return n, nil
} }
@ -69,13 +82,19 @@ func (ws *wsconn) Read(b []byte) (n int, err error) {
} }
n, err = readNext(b) n, err = readNext(b)
ws.rlock.Unlock()
return n, err return n, err
} }
func (ws *wsconn) Write(b []byte) (n int, err error) { func (ws *wsconn) Write(b []byte) (n int, err error) {
ws.wlock.Lock()
defer func() {
if r := recover(); r != nil {
fmt.Println("WS workaround: recover", r)
ws.wlock.Unlock()
}
}()
//defer //defer
//ws.checkifRWAfterClosing() //ws.checkifRWAfterClosing()
if ws.connClosing { if ws.connClosing {
@ -103,10 +122,12 @@ func (ws *wsconn) Write(b []byte) (n int, err error) {
return n, err return n, err
} }
n, err = writeWs(b) n, err = writeWs(b)
ws.wlock.Unlock()
return n, err return n, err
} }
func (ws *wsconn) Close() error { func (ws *wsconn) Close() error {
ws.connClosing = true ws.connClosing = true
ws.wsc.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add((time.Second * 5)))
err := ws.wsc.Close() err := ws.wsc.Close()
ws.retloc.Broadcast() ws.retloc.Broadcast()
return err return err
@ -157,7 +178,7 @@ func (ws *wsconn) setup() {
} }
initConnectedCond() initConnectedCond()
//ws.pingPong() ws.pingPong()
} }
func (ws *wsconn) Reusable() bool { func (ws *wsconn) Reusable() bool {
@ -185,8 +206,10 @@ func (ws *wsconn) pingPong() {
select { select {
case <-pongRcv: case <-pongRcv:
//log.Debug("WS:Pong~" + ws.wsc.UnderlyingConn().RemoteAddr().String())
break break
case <-tick.C: case <-tick.C:
log.Debug("WS:Closing as ping is not responded~" + ws.wsc.UnderlyingConn().LocalAddr().String() + "-" + ws.wsc.UnderlyingConn().RemoteAddr().String())
ws.Close() ws.Close()
} }
<-tick.C <-tick.C

Loading…
Cancel
Save