Skip to content

Commit

Permalink
修改名称
Browse files Browse the repository at this point in the history
  • Loading branch information
bg5sbk committed Jun 24, 2015
1 parent 32aa574 commit 0e665b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
47 changes: 34 additions & 13 deletions codec_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,65 @@ import (
"io"
)

func Raw() PSCodecType {
return rawCodecType{}
func Bytes() PSCodecType {
return bytesCodecType{}
}

type rawCodecType struct{}
func String() PacketCodecType {
return stringCodecType{}
}

type bytesCodecType struct{}

func (_ rawCodecType) NewPacketCodec() PacketCodec {
return rawPacketCodec{}
func (_ bytesCodecType) NewPacketCodec() PacketCodec {
return bytesPacketCodec{}
}

func (_ rawCodecType) NewStreamCodec(r *bufio.Reader, w *bufio.Writer) StreamCodec {
return rawStreamCodec{r, w}
func (_ bytesCodecType) NewStreamCodec(r *bufio.Reader, w *bufio.Writer) StreamCodec {
return bytesStreamCodec{r, w}
}

type rawPacketCodec struct{}
type bytesPacketCodec struct{}

func (codec rawPacketCodec) DecodePacket(msg interface{}, b []byte) error {
func (codec bytesPacketCodec) DecodePacket(msg interface{}, b []byte) error {
*(msg.(*[]byte)) = b
return nil
}

func (codec rawPacketCodec) EncodePacket(msg interface{}) ([]byte, error) {
func (codec bytesPacketCodec) EncodePacket(msg interface{}) ([]byte, error) {
return msg.([]byte), nil
}

type rawStreamCodec struct {
type bytesStreamCodec struct {
r *bufio.Reader
w *bufio.Writer
}

func (codec rawStreamCodec) DecodeStream(msg interface{}) error {
func (codec bytesStreamCodec) DecodeStream(msg interface{}) error {
_, err := io.ReadFull(codec.r, msg.([]byte))
return err
}

func (codec rawStreamCodec) EncodeStream(msg interface{}) error {
func (codec bytesStreamCodec) EncodeStream(msg interface{}) error {
if _, err := codec.w.Write(msg.([]byte)); err != nil {
return err
}
return codec.w.Flush()
}

type stringCodecType struct{}

func (_ stringCodecType) NewPacketCodec() PacketCodec {
return stringPacketCodec{}
}

type stringPacketCodec struct{}

func (codec stringPacketCodec) DecodePacket(msg interface{}, b []byte) error {
*(msg.(*string)) = string(b)
return nil
}

func (codec stringPacketCodec) EncodePacket(msg interface{}) ([]byte, error) {
return []byte(msg.(string)), nil
}
2 changes: 1 addition & 1 deletion gateway/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Frontend struct {

func NewFrontend(listener link.IPacketListener, handshaker ClientHandshaker) *Frontend {
front := &Frontend{
server: link.NewServer(listener, link.Raw()),
server: link.NewServer(listener, link.Bytes()),
links: make(map[uint64]*frontendLink),
handshaker: handshaker,
}
Expand Down
12 changes: 6 additions & 6 deletions gateway/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func RandBytes(n int) []byte {
}

func StartEchoBackend() (*link.Server, error) {
backend, err := link.Serve("tcp://0.0.0.0:0", NewBackend(), link.Raw())
backend, err := link.Serve("tcp://0.0.0.0:0", NewBackend(), link.Bytes())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func Test_Simple(t *testing.T) {
gateway := StartTestGateway(t, backend.Listener().Addr().String())
gatewayAddr := gateway.server.Listener().Addr().String()

client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Raw())
client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Bytes())
unitest.NotError(t, err)

for i := 0; i < 10000; i++ {
Expand Down Expand Up @@ -110,7 +110,7 @@ func Test_Complex(t *testing.T) {
go func() {
defer wg.Done()

client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Raw())
client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Bytes())
unitest.NotError(t, err)

for j := 0; j < 500; j++ {
Expand Down Expand Up @@ -145,13 +145,13 @@ func Test_Broadcast(t *testing.T) {
var (
clientNum = 20
packetNum = 2000
channel = NewChannel(link.Raw())
channel = NewChannel(link.Bytes())
broadcastMsg []byte
broadcastWait sync.WaitGroup
clientWait sync.WaitGroup
)

backend, err := link.Serve("tcp://0.0.0.0:0", NewBackend(), link.Raw())
backend, err := link.Serve("tcp://0.0.0.0:0", NewBackend(), link.Bytes())
unitest.NotError(t, err)

go backend.Loop(func(session *link.Session) {
Expand Down Expand Up @@ -187,7 +187,7 @@ func Test_Broadcast(t *testing.T) {
go func() {
defer wg.Done()

client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Raw())
client, err := link.Connect("tcp://"+gatewayAddr, link.Packet(link.Uint16BE), link.Bytes())
unitest.NotError(t, err)

for j := 0; j < packetNum; j++ {
Expand Down
8 changes: 4 additions & 4 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func SessionTest(t *testing.T, protocol ClientProtocol, codec CodecType, test fu
MakeSureSessionGoroutineExit(t)
}

func Test_RawStream(t *testing.T) {
SessionTest(t, Stream(), Raw(), func(t *testing.T, session *Session) {
func Test_BytesStream(t *testing.T) {
SessionTest(t, Stream(), Bytes(), func(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandBytes(1024)
err := session.Send(msg1)
Expand All @@ -95,8 +95,8 @@ func Test_RawStream(t *testing.T) {
})
}

func Test_RawPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), Raw(), func(t *testing.T, session *Session) {
func Test_BytesPacket(t *testing.T) {
SessionTest(t, Packet(Uint16BE), Bytes(), func(t *testing.T, session *Session) {
for i := 0; i < 2000; i++ {
msg1 := RandBytes(1024)
err := session.Send(msg1)
Expand Down

0 comments on commit 0e665b5

Please sign in to comment.