Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

让Session继承net.Conn #58

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified README.md
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Dial(network, address string, protocol Protocol, sendChanSize int) (*Sessio
if err != nil {
return nil, err
}
return NewSession(codec, sendChanSize), nil
return NewSession(codec, conn, sendChanSize), nil
}

func DialTimeout(network, address string, timeout time.Duration, protocol Protocol, sendChanSize int) (*Session, error) {
Expand All @@ -56,7 +56,7 @@ func DialTimeout(network, address string, timeout time.Duration, protocol Protoc
if err != nil {
return nil, err
}
return NewSession(codec, sendChanSize), nil
return NewSession(codec, conn, sendChanSize), nil
}

func Accept(listener net.Listener) (net.Conn, error) {
Expand Down
2 changes: 1 addition & 1 deletion codec/bufio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"io"

"github.com/funny/link"
"github.com/zwczou/link"
)

func Bufio(base link.Protocol, readBuf, writeBuf int) link.Protocol {
Expand Down
2 changes: 1 addition & 1 deletion codec/fixlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"
"math"

"github.com/funny/link"
"github.com/zwczou/link"
)

var ErrTooLargePacket = errors.New("Too Large Packet")
Expand Down
2 changes: 1 addition & 1 deletion codec/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"reflect"

"github.com/funny/link"
"github.com/zwczou/link"
)

type JsonProtocol struct {
Expand Down
2 changes: 1 addition & 1 deletion codec/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"testing"

"github.com/funny/link"
"github.com/zwczou/link"
)

type MyMessage1 struct {
Expand Down
9 changes: 6 additions & 3 deletions manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package link

import "sync"
import (
"net"
"sync"
)

const sessionMapNum = 32

Expand Down Expand Up @@ -39,8 +42,8 @@ func (manager *Manager) Dispose() {
})
}

func (manager *Manager) NewSession(codec Codec, sendChanSize int) *Session {
session := newSession(manager, codec, sendChanSize)
func (manager *Manager) NewSession(codec Codec, conn net.Conn, sendChanSize int) *Session {
session := newSession(manager, codec, conn, sendChanSize)
manager.putSession(session)
return session
}
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (server *Server) Serve() error {
conn.Close()
return
}
session := server.manager.NewSession(codec, server.sendChanSize)
session := server.manager.NewSession(codec, conn, server.sendChanSize)
server.handler.HandleSession(session)
}()
}
Expand Down
10 changes: 7 additions & 3 deletions session.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package link

import (
"errors"
"net"
"sync"
"sync/atomic"
)
Expand All @@ -12,6 +13,8 @@ var SessionBlockedError = errors.New("Session Blocked")
var globalSessionId uint64

type Session struct {
net.Conn

id uint64
codec Codec
manager *Manager
Expand All @@ -28,12 +31,13 @@ type Session struct {
State interface{}
}

func NewSession(codec Codec, sendChanSize int) *Session {
return newSession(nil, codec, sendChanSize)
func NewSession(codec Codec, conn net.Conn, sendChanSize int) *Session {
return newSession(nil, codec, conn, sendChanSize)
}

func newSession(manager *Manager, codec Codec, sendChanSize int) *Session {
func newSession(manager *Manager, codec Codec, conn net.Conn, sendChanSize int) *Session {
session := &Session{
Conn: conn,
codec: codec,
manager: manager,
closeChan: make(chan int),
Expand Down
2 changes: 1 addition & 1 deletion session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func BytesTest(t *testing.T, session *Session) {
}

func Test_CloseCallback(t *testing.T) {
session := newSession(nil, nil, 0)
session := newSession(nil, nil, nil, 0)

c := make(chan int, 10)
for i := 0; i < 10; i++ {
Expand Down