Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(354)

Unified Diff: src/pkg/websocket/websocket.go

Issue 5058043: code review 5058043: websocket: add mutex to make websocket full-duplex
Patch Set: diff -r 49696534f8b7 https://go.googlecode.com/hg/ Created 13 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/pkg/websocket/hybi.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pkg/websocket/websocket.go
===================================================================
--- a/src/pkg/websocket/websocket.go
+++ b/src/pkg/websocket/websocket.go
@@ -15,6 +15,7 @@
"json"
"net"
"os"
+ "sync"
"url"
)
@@ -147,9 +148,11 @@
buf *bufio.ReadWriter
rwc io.ReadWriteCloser
+ rio sync.Mutex
frameReaderFactory
frameReader
+ wio sync.Mutex
frameWriterFactory
frameHandler
@@ -163,6 +166,8 @@
// will read the rest of the frame data.
// it reads Text frame or Binary frame.
func (ws *Conn) Read(msg []byte) (n int, err os.Error) {
+ ws.rio.Lock()
+ defer ws.rio.Unlock()
again:
if ws.frameReader == nil {
frame, err := ws.frameReaderFactory.NewFrameReader()
@@ -191,6 +196,8 @@
// Write implements the io.Writer interface:
// it writes data as a frame to the WebSocket connection.
func (ws *Conn) Write(msg []byte) (n int, err os.Error) {
+ ws.wio.Lock()
+ defer ws.wio.Unlock()
w, err := ws.frameWriterFactory.NewFrameWriter(ws.PayloadType)
if err != nil {
return 0, err
@@ -279,6 +286,8 @@
if err != nil {
return err
}
+ ws.wio.Lock()
+ defer ws.wio.Unlock()
w, err := ws.frameWriterFactory.NewFrameWriter(payloadType)
_, err = w.Write(data)
w.Close()
@@ -287,6 +296,8 @@
// Receive receives single frame from ws, unmarshaled by cd.Unmarshal and stores in v.
func (cd Codec) Receive(ws *Conn, v interface{}) (err os.Error) {
+ ws.rio.Lock()
+ defer ws.rio.Unlock()
if ws.frameReader != nil {
_, err = io.Copy(ioutil.Discard, ws.frameReader)
if err != nil {
« no previous file with comments | « src/pkg/websocket/hybi.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b