Close the socket in the link

This commit is contained in:
Asim Aslam 2019-10-23 16:39:26 +01:00
parent fe180148a1
commit bf4a73d5c0
2 changed files with 24 additions and 6 deletions

View File

@ -210,6 +210,7 @@ func (l *link) Close() error {
case <-l.closed: case <-l.closed:
return nil return nil
default: default:
l.Socket.Close()
close(l.closed) close(l.closed)
} }
@ -227,12 +228,19 @@ func (l *link) Send(m *transport.Message) error {
// get time now // get time now
now := time.Now() now := time.Now()
// queue the message // check if its closed first
select { select {
case <-l.closed: case <-l.closed:
return io.EOF return io.EOF
default:
}
// queue the message
select {
case l.sendQueue <- p: case l.sendQueue <- p:
// in the send queue // in the send queue
case <-l.closed:
return io.EOF
} }
// error to use // error to use
@ -293,7 +301,17 @@ func (l *link) Send(m *transport.Message) error {
func (l *link) Recv(m *transport.Message) error { func (l *link) Recv(m *transport.Message) error {
select { select {
case <-l.closed: case <-l.closed:
return io.EOF // check if there's any messages left
select {
case pk := <-l.recvQueue:
// check the packet receive error
if pk.err != nil {
return pk.err
}
*m = *pk.message
default:
return io.EOF
}
case pk := <-l.recvQueue: case pk := <-l.recvQueue:
// check the packet receive error // check the packet receive error
if pk.err != nil { if pk.err != nil {

View File

@ -202,8 +202,8 @@ func testBrokenTunAccept(t *testing.T, tun Tunnel, wait chan bool, wg *sync.Wait
t.Fatal(err) t.Fatal(err)
} }
// notify sender we have received the message // notify the sender we have received
<-wait wait <- true
} }
func testBrokenTunSend(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGroup) { func testBrokenTunSend(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGroup) {
@ -234,7 +234,7 @@ func testBrokenTunSend(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGr
<-wait <-wait
// give it time to reconnect // give it time to reconnect
time.Sleep(2 * ReconnectTime) time.Sleep(5 * ReconnectTime)
// send the message // send the message
if err := c.Send(&m); err != nil { if err := c.Send(&m); err != nil {
@ -244,7 +244,7 @@ func testBrokenTunSend(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGr
// wait for the listener to receive the message // wait for the listener to receive the message
// c.Send merely enqueues the message to the link send queue and returns // c.Send merely enqueues the message to the link send queue and returns
// in order to verify it was received we wait for the listener to tell us // in order to verify it was received we wait for the listener to tell us
wait <- true <-wait
} }
func TestReconnectTunnel(t *testing.T) { func TestReconnectTunnel(t *testing.T) {