mirror of
https://github.com/xtaci/smux.git
synced 2024-04-21 10:51:48 +00:00
use writeFrameInternal instead of writeFrameWithDeadline
This commit is contained in:
+4
-5
@@ -289,7 +289,7 @@ func (s *Session) keepalive() {
|
||||
for {
|
||||
select {
|
||||
case <-tickerPing.C:
|
||||
_, err := s.writeFrameWithDeadline(newFrame(cmdNOP, 0), tickerTimeout.C)
|
||||
_, err := s.writeFrameInternal(newFrame(cmdNOP, 0), tickerTimeout.C)
|
||||
if err == errTimeout {
|
||||
if !atomic.CompareAndSwapInt32(&s.dataReady, 1, 0) {
|
||||
s.Close()
|
||||
@@ -341,12 +341,11 @@ func (s *Session) sendLoop() {
|
||||
// writeFrame writes the frame to the underlying connection
|
||||
// and returns the number of bytes written if successful
|
||||
func (s *Session) writeFrame(f Frame) (n int, err error) {
|
||||
return s.writeFrameWithDeadline(f, nil)
|
||||
return s.writeFrameInternal(f, nil)
|
||||
}
|
||||
|
||||
// writeFrame may block forever in keepalive function, then it never timeout
|
||||
// so set a deadline to writeFrame only used in keepalive
|
||||
func (s *Session) writeFrameWithDeadline(f Frame, deadline <-chan time.Time) (int, error) {
|
||||
// internal writeFrame version to support deadline used in keepalive
|
||||
func (s *Session) writeFrameInternal(f Frame, deadline <-chan time.Time) (int, error) {
|
||||
req := writeRequest{
|
||||
frame: f,
|
||||
result: make(chan writeResult, 1),
|
||||
|
||||
+6
-6
@@ -594,7 +594,7 @@ func TestRandomFrame(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeadlineFrame(t *testing.T) {
|
||||
func TestWriteFrameInternal(t *testing.T) {
|
||||
addr, stop, cli, err := setupServer(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -619,10 +619,10 @@ func TestDeadlineFrame(t *testing.T) {
|
||||
session.Close()
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(byte(rand.Uint32()), rand.Uint32())
|
||||
session.writeFrameWithDeadline(f, time.After(session.config.KeepAliveTimeout))
|
||||
session.writeFrameInternal(f, time.After(session.config.KeepAliveTimeout))
|
||||
}
|
||||
|
||||
// random cmds, writeFrameWithDeadline only used in keepalive with cmd of cmdNOP
|
||||
// random cmds
|
||||
cli, err = net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -631,14 +631,14 @@ func TestDeadlineFrame(t *testing.T) {
|
||||
session, _ = Client(cli, nil)
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
session.writeFrameWithDeadline(f, time.After(session.config.KeepAliveTimeout))
|
||||
session.writeFrameInternal(f, time.After(session.config.KeepAliveTimeout))
|
||||
}
|
||||
//deadline occur
|
||||
{
|
||||
c := make(chan time.Time)
|
||||
close(c)
|
||||
f := newFrame(allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
_, err := session.writeFrameWithDeadline(f, c)
|
||||
_, err := session.writeFrameInternal(f, c)
|
||||
if err != errTimeout {
|
||||
t.Fatal("write frame with deadline failed", err)
|
||||
}
|
||||
@@ -663,7 +663,7 @@ func TestDeadlineFrame(t *testing.T) {
|
||||
time.Sleep(time.Second)
|
||||
close(c)
|
||||
}()
|
||||
_, err = session.writeFrameWithDeadline(f, c)
|
||||
_, err = session.writeFrameInternal(f, c)
|
||||
if err.Error() != errBrokenPipe {
|
||||
t.Fatal("write frame with deadline failed", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user