Skip to content

Commit

Permalink
Added read/write locks to messaging channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
林志宇 committed May 7, 2019
1 parent a44f133 commit 2ec0415
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/messaging/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"io"
"sync"
"time"

"github.com/skycoin/skywire/internal/noise"
Expand All @@ -27,6 +28,8 @@ type channel struct {
doneChan chan struct{}

noise *noise.Noise
rMx sync.Mutex
wMx sync.Mutex
}

// Edges returns the public keys of the channel's edge nodes
Expand Down Expand Up @@ -90,6 +93,9 @@ func (c *channel) Write(p []byte) (n int, err error) {
error
})
go func() {
c.wMx.Lock()
defer c.wMx.Unlock()

data := c.noise.EncryptUnsafe(p)
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, uint16(len(data)))
Expand Down Expand Up @@ -147,6 +153,9 @@ func (c *channel) close() {
}

func (c *channel) readEncrypted(ctx context.Context, p []byte) (n int, err error) {
c.rMx.Lock()
defer c.rMx.Unlock()

buf := new(bytes.Buffer)
readAtLeast := func(d []byte) (int, error) {
for {
Expand Down

0 comments on commit 2ec0415

Please sign in to comment.