Skip to content
This repository has been archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
simplify return code len config
Browse files Browse the repository at this point in the history
  • Loading branch information
funny-falcon committed Oct 15, 2013
1 parent 3aadc62 commit 497415b
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 53 deletions.
1 change: 0 additions & 1 deletion examples/bench/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func main() {
Network: "tcp",
Address: fmt.Sprintf("%s:%d", h, p),
Connections: c,
RetCodeLen: 4,
PingInterval: 1 * time.Second,
Timeout: time.Second,
}
Expand Down
8 changes: 3 additions & 5 deletions examples/colander/colander.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,14 @@ func sumTestService(cx *iproto.Context, req *iproto.Request) (iproto.RetCode, in
var ProxyTestService iproto.EndPoint

var serverConf = server.Config{
Network: "tcp",
Address: ":8766",
RetCodeLen: 4,
EndPoint: &RootService,
Network: "tcp",
Address: ":8766",
EndPoint: &RootService,
}

var recurConf = client.ServerConfig{
Network: "tcp",
Address: ":8765",
RetCodeLen: 4,
Connections: 4,
PingInterval: time.Hour,
Timeout: 2000 * time.Millisecond,
Expand Down
3 changes: 2 additions & 1 deletion net/client/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"github.com/funny-falcon/go-iproto/net"
"log"
"strings"
"time"
Expand All @@ -19,7 +20,7 @@ type ServerConfig struct {
WriteTimeout time.Duration
PingInterval time.Duration

RetCodeLen int
RetCodeType net.RCType

Timeout time.Duration
}
Expand Down
8 changes: 4 additions & 4 deletions net/client/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type CConf struct {
WriteTimeout time.Duration
DialTimeout time.Duration

RetCodeLen int
RetCodeType nt.RCType

ConnErr chan<- Error
}
Expand Down Expand Up @@ -107,8 +107,8 @@ func (conn *Connection) Loop() {
conn.State = CsClosed
} else {
conn.conn = netconn.(nt.NetConn)
conn.reader.Init(conn.conn, conn.ReadTimeout)
conn.writer.Init(conn.conn, conn.WriteTimeout)
conn.reader.Init(conn.conn, conn.ReadTimeout, conn.RetCodeType)
conn.writer.Init(conn.conn, conn.WriteTimeout, conn.RetCodeType)
if err = conn.writer.Ping(); err == nil {
if err = conn.writer.Flush(); err == nil {
err = conn.reader.ReadPing()
Expand Down Expand Up @@ -207,7 +207,7 @@ func (conn *Connection) readLoop() {
defer conn.notifyLoop(readClosed)

for {
if res, conn.readErr = r.ReadResponse(conn.RetCodeLen); conn.readErr != nil {
if res, conn.readErr = r.ReadResponse(); conn.readErr != nil {
break
}

Expand Down
2 changes: 1 addition & 1 deletion net/client/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (cfg *ServerConfig) NewServer() (serv *Server) {
DialTimeout: cfg.DialTimeout,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
RetCodeLen: cfg.RetCodeLen,
RetCodeType: cfg.RetCodeType,
},
},
connErr: make(chan connection.Error, 4),
Expand Down
20 changes: 4 additions & 16 deletions net/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,10 @@ const (
Write
)

type action uint32
type RCType uint32

const (
reset = action(iota + 1)
freeze
unFreeze
)

type state uint32

const (
unset = state(1 << iota)
unfrozen
)

const (
doSet = iota + 1
doClear
RC4byte = RCType(iota)
RC1byte
RC0byte
)
62 changes: 41 additions & 21 deletions net/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ type Request struct {
type Response iproto.Response

type HeaderReader struct {
r SliceReader
r SliceReader
rc RCType
}

func (h *HeaderReader) Init(conn io.Reader, timeout time.Duration) {
func (h *HeaderReader) Init(conn io.Reader, timeout time.Duration, rc RCType) {
h.r = SliceReader{r: conn, size: 8 * 1024, timeout: timeout}
h.rc = rc
}

func (h *HeaderReader) ReadRequest() (req Request, err error) {
Expand All @@ -46,7 +48,7 @@ func (h *HeaderReader) ReadRequest() (req Request, err error) {
return
}

func (h *HeaderReader) ReadResponse(retCodeLen int) (res Response, err error) {
func (h *HeaderReader) ReadResponse() (res Response, err error) {
var code iproto.RetCode
var head, body []byte

Expand All @@ -58,21 +60,26 @@ func (h *HeaderReader) ReadResponse(retCodeLen int) (res Response, err error) {
body_len := bin_le.Uint32(head[4:8])

if msg != iproto.Ping {
if body_len < uint32(retCodeLen) {
code = iproto.RcProtocolError
} else {
body_len -= uint32(retCodeLen)
switch retCodeLen {
case 0:
code = iproto.RcOK
case 1:
switch h.rc {
case RC0byte:
code = iproto.RcOK
case RC1byte:
if body_len < 1 {
code = iproto.RcProtocolError
} else {
var c byte
body_len -= 1
if c, err = h.r.ReadByte(); err != nil {
return
}
code = iproto.RetCode(c)
case 4:
}
case RC4byte:
if body_len < 4 {
code = iproto.RcProtocolError
} else {
var cd []byte
body_len -= 4
if cd, err = h.r.Read(4); err != nil {
return
}
Expand Down Expand Up @@ -111,11 +118,24 @@ func (h *HeaderReader) ReadPing() (err error) {
}

type HeaderWriter struct {
w BufWriter
w BufWriter
rc RCType
rcl int
}

func (h *HeaderWriter) Init(w io.Writer, timeout time.Duration) {
func (h *HeaderWriter) Init(w io.Writer, timeout time.Duration, rc RCType) {
h.w = BufWriter{w: w, timeout: timeout}
h.rc = rc
switch h.rc {
case RC0byte:
h.rcl = 0
case RC1byte:
h.rcl = 1
case RC4byte:
h.rcl = 4
default:
panic("Unsupported return code len")
}
}

func (h *HeaderWriter) WriteRequest(req Request) (err error) {
Expand All @@ -134,28 +154,28 @@ func (h *HeaderWriter) Ping() (err error) {
return h.WriteRequest(ping)
}

func (h *HeaderWriter) WriteResponse(res Response, retCodeLen int) (err error) {
func (h *HeaderWriter) WriteResponse(res Response) (err error) {
retCodeLen := h.rcl
if res.Msg == iproto.Ping {
retCodeLen = 0
}

body_len := uint32(len(res.Body) + retCodeLen)
if err = h.w.Write3Uint32(uint32(res.Msg), body_len, uint32(res.Id)); err != nil {
return
}

switch retCodeLen {
case 0:
case 1:
switch h.rc {
case RC0byte:
case RC1byte:
if err = h.w.WriteByte(byte(res.Code)); err != nil {
return
}
case 4:
case RC4byte:
if err = h.w.WriteUint32(uint32(res.Code)); err != nil {
return
}
default:
panic("Unsupported retCodeLen")
panic("Unsupported return code len")
}

err = h.w.Write(res.Body)
Expand Down
3 changes: 2 additions & 1 deletion net/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"github.com/funny-falcon/go-iproto"
"github.com/funny-falcon/go-iproto/net"
"time"
)

Expand All @@ -14,5 +15,5 @@ type Config struct {
ReadTimeout time.Duration
WriteTimeout time.Duration

RetCodeLen int
RetCodeType net.RCType
}
6 changes: 3 additions & 3 deletions net/server/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (conn *Connection) readLoop() {
var req nt.Request
var err error
var r nt.HeaderReader
r.Init(conn.conn, conn.ReadTimeout)
r.Init(conn.conn, conn.ReadTimeout, conn.RetCodeType)

defer conn.notifyLoop(readClosed)

Expand Down Expand Up @@ -222,7 +222,7 @@ func (conn *Connection) writeLoop() {
var err error
var w nt.HeaderWriter

w.Init(conn.conn, conn.WriteTimeout)
w.Init(conn.conn, conn.WriteTimeout, conn.RetCodeType)

defer func() {
if err == nil {
Expand Down Expand Up @@ -265,7 +265,7 @@ Loop:
}
}

if err = w.WriteResponse(res, conn.RetCodeLen); err != nil {
if err = w.WriteResponse(res); err != nil {
break Loop
}
}
Expand Down

0 comments on commit 497415b

Please sign in to comment.