mirror of
https://github.com/xtaci/smux.git
synced 2024-04-21 10:51:48 +00:00
Revert "config protocol Version in Config"
This reverts commit 9f184cedda.
This commit is contained in:
@@ -5,6 +5,10 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
version = 1
|
||||
)
|
||||
|
||||
const ( // cmds
|
||||
cmdSYN byte = iota // stream open
|
||||
cmdFIN // stream close, a.k.a EOF mark
|
||||
@@ -28,7 +32,7 @@ type Frame struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
func newFrame(version byte, cmd byte, sid uint32) Frame {
|
||||
func newFrame(cmd byte, sid uint32) Frame {
|
||||
return Frame{ver: version, cmd: cmd, sid: sid}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,9 +13,6 @@ import (
|
||||
|
||||
// Config is used to tune the Smux session
|
||||
type Config struct {
|
||||
// Version config
|
||||
Version byte
|
||||
|
||||
// KeepAliveInterval is how often to send a NOP command to the remote
|
||||
KeepAliveInterval time.Duration
|
||||
|
||||
@@ -35,7 +32,6 @@ type Config struct {
|
||||
// DefaultConfig is used to return a default configuration
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Version: 1,
|
||||
KeepAliveInterval: 10 * time.Second,
|
||||
KeepAliveTimeout: 30 * time.Second,
|
||||
MaxFrameSize: 32768,
|
||||
@@ -45,9 +41,6 @@ func DefaultConfig() *Config {
|
||||
|
||||
// VerifyConfig is used to verify the sanity of configuration
|
||||
func VerifyConfig(config *Config) error {
|
||||
if config.Version > 2 {
|
||||
return errors.New("max supported smux version is 2")
|
||||
}
|
||||
if config.KeepAliveInterval == 0 {
|
||||
return errors.New("keep-alive interval must be positive")
|
||||
}
|
||||
|
||||
@@ -25,14 +25,6 @@ func TestConfig(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
config = DefaultConfig()
|
||||
config.Version = 3
|
||||
err = VerifyConfig(config)
|
||||
t.Log(err)
|
||||
if err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
config = DefaultConfig()
|
||||
config.KeepAliveInterval = 10
|
||||
config.KeepAliveTimeout = 5
|
||||
|
||||
+3
-3
@@ -141,7 +141,7 @@ func (s *Session) OpenStream() (*Stream, error) {
|
||||
|
||||
stream := newStream(sid, s.config.MaxFrameSize, s)
|
||||
|
||||
if _, err := s.writeFrame(newFrame(s.config.Version, cmdSYN, sid)); err != nil {
|
||||
if _, err := s.writeFrame(newFrame(cmdSYN, sid)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -367,7 +367,7 @@ func (s *Session) recvLoop() {
|
||||
// read header first
|
||||
if _, err := io.ReadFull(s.conn, hdr[:]); err == nil {
|
||||
atomic.StoreInt32(&s.dataReady, 1)
|
||||
if hdr.Version() != s.config.Version {
|
||||
if hdr.Version() != version {
|
||||
s.notifyProtoError(ErrInvalidProtocol)
|
||||
return
|
||||
}
|
||||
@@ -427,7 +427,7 @@ func (s *Session) keepalive() {
|
||||
for {
|
||||
select {
|
||||
case <-tickerPing.C:
|
||||
s.writeFrameInternal(newFrame(s.config.Version, cmdNOP, 0), tickerPing.C, 0)
|
||||
s.writeFrameInternal(newFrame(cmdNOP, 0), tickerPing.C, 0)
|
||||
s.notifyBucket() // force a signal to the recvLoop
|
||||
case <-tickerTimeout.C:
|
||||
if !atomic.CompareAndSwapInt32(&s.dataReady, 1, 0) {
|
||||
|
||||
+10
-10
@@ -657,7 +657,7 @@ func TestRandomFrame(t *testing.T) {
|
||||
}
|
||||
session, _ = Client(cli, nil)
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(1, cmdSYN, 1000)
|
||||
f := newFrame(cmdSYN, 1000)
|
||||
session.writeFrame(f)
|
||||
}
|
||||
cli.Close()
|
||||
@@ -670,7 +670,7 @@ func TestRandomFrame(t *testing.T) {
|
||||
allcmds := []byte{cmdSYN, cmdFIN, cmdPSH, cmdNOP}
|
||||
session, _ = Client(cli, nil)
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(1, allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
f := newFrame(allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
session.writeFrame(f)
|
||||
}
|
||||
cli.Close()
|
||||
@@ -682,7 +682,7 @@ func TestRandomFrame(t *testing.T) {
|
||||
}
|
||||
session, _ = Client(cli, nil)
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(1, byte(rand.Uint32()), rand.Uint32())
|
||||
f := newFrame(byte(rand.Uint32()), rand.Uint32())
|
||||
session.writeFrame(f)
|
||||
}
|
||||
cli.Close()
|
||||
@@ -694,7 +694,7 @@ func TestRandomFrame(t *testing.T) {
|
||||
}
|
||||
session, _ = Client(cli, nil)
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(1, byte(rand.Uint32()), rand.Uint32())
|
||||
f := newFrame(byte(rand.Uint32()), rand.Uint32())
|
||||
f.ver = byte(rand.Uint32())
|
||||
session.writeFrame(f)
|
||||
}
|
||||
@@ -707,7 +707,7 @@ func TestRandomFrame(t *testing.T) {
|
||||
}
|
||||
session, _ = Client(cli, nil)
|
||||
|
||||
f := newFrame(1, byte(rand.Uint32()), rand.Uint32())
|
||||
f := newFrame(byte(rand.Uint32()), rand.Uint32())
|
||||
rnd := make([]byte, rand.Uint32()%1024)
|
||||
io.ReadFull(crand.Reader, rnd)
|
||||
f.data = rnd
|
||||
@@ -731,7 +731,7 @@ func TestRandomFrame(t *testing.T) {
|
||||
//close first
|
||||
session.Close()
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(1, byte(rand.Uint32()), rand.Uint32())
|
||||
f := newFrame(byte(rand.Uint32()), rand.Uint32())
|
||||
session.writeFrame(f)
|
||||
}
|
||||
}
|
||||
@@ -760,7 +760,7 @@ func TestWriteFrameInternal(t *testing.T) {
|
||||
//close first
|
||||
session.Close()
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(1, byte(rand.Uint32()), rand.Uint32())
|
||||
f := newFrame(byte(rand.Uint32()), rand.Uint32())
|
||||
session.writeFrameInternal(f, time.After(session.config.KeepAliveTimeout), 0)
|
||||
}
|
||||
|
||||
@@ -772,14 +772,14 @@ func TestWriteFrameInternal(t *testing.T) {
|
||||
allcmds := []byte{cmdSYN, cmdFIN, cmdPSH, cmdNOP}
|
||||
session, _ = Client(cli, nil)
|
||||
for i := 0; i < 100; i++ {
|
||||
f := newFrame(1, allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
f := newFrame(allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
session.writeFrameInternal(f, time.After(session.config.KeepAliveTimeout), 0)
|
||||
}
|
||||
//deadline occur
|
||||
{
|
||||
c := make(chan time.Time)
|
||||
close(c)
|
||||
f := newFrame(1, allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
f := newFrame(allcmds[rand.Int()%len(allcmds)], rand.Uint32())
|
||||
_, err := session.writeFrameInternal(f, c, 0)
|
||||
if !strings.Contains(err.Error(), "timeout") {
|
||||
t.Fatal("write frame with deadline failed", err)
|
||||
@@ -796,7 +796,7 @@ func TestWriteFrameInternal(t *testing.T) {
|
||||
config.KeepAliveInterval = time.Second
|
||||
config.KeepAliveTimeout = 2 * time.Second
|
||||
session, _ = Client(&blockWriteConn{cli}, config)
|
||||
f := newFrame(1, byte(rand.Uint32()), rand.Uint32())
|
||||
f := newFrame(byte(rand.Uint32()), rand.Uint32())
|
||||
c := make(chan time.Time)
|
||||
go func() {
|
||||
//die first, deadline second, better for coverage
|
||||
|
||||
@@ -175,7 +175,7 @@ func (s *Stream) Write(b []byte) (n int, err error) {
|
||||
|
||||
// frame split and transmit
|
||||
sent := 0
|
||||
frame := newFrame(s.sess.config.Version, cmdPSH, s.id)
|
||||
frame := newFrame(cmdPSH, s.id)
|
||||
bts := b
|
||||
for len(bts) > 0 {
|
||||
sz := len(bts)
|
||||
@@ -205,7 +205,7 @@ func (s *Stream) Close() error {
|
||||
})
|
||||
|
||||
if once {
|
||||
_, err = s.sess.writeFrame(newFrame(s.sess.config.Version, cmdFIN, s.id))
|
||||
_, err = s.sess.writeFrame(newFrame(cmdFIN, s.id))
|
||||
s.sess.streamClosed(s.id)
|
||||
return err
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user