Skip to content

Commit

Permalink
opt: make code more formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jul 4, 2020
1 parent f46da44 commit 13b45a4
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 52 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ before_install:

install:
- go get -u golang.org/x/lint/golint
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0
- curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s -- -b ~/bin

script:
- |-
case $TRAVIS_OS_NAME in
linux|osx)
golint ./... | reviewdog -f=golint -reporter=github-check
golangci-lint run --out-format=line-number -E gofmt -E golint -E misspell -E lll | reviewdog -f=golangci-lint -reporter=github-check
golangci-lint run --out-format=line-number -E goimports -E gocritic -E misspell -E godot | reviewdog -f=golangci-lint -reporter=github-check
golint ./... | reviewdog -f=golint -reporter=github-pr-review
golangci-lint run --out-format=line-number -E gofmt -E golint -E misspell -E lll | reviewdog -f=golangci-lint -reporter=github-pr-review
golangci-lint run --out-format=line-number -E goimports -E gocritic -E misspell -E godot | reviewdog -f=golangci-lint -reporter=github-pr-review
;;
esac
- go test -v ./...
Expand Down
2 changes: 1 addition & 1 deletion codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (cc *LengthFieldBasedFrameCodec) Decode(c Conn) ([]byte, error) {
err error
)
in = c.Read()
if cc.decoderConfig.LengthFieldOffset > 0 { //discard header(offset)
if cc.decoderConfig.LengthFieldOffset > 0 { // discard header(offset)
header, err = in.readN(cc.decoderConfig.LengthFieldOffset)
if err != nil {
return nil, errUnexpectedEOF
Expand Down
6 changes: 2 additions & 4 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ func (el *eventloop) loopCloseConn(c *conn, err error) error {
if err0 == nil && err1 == nil {
delete(el.connections, c.fd)
el.calibrateCallback(el, -1)
switch el.eventHandler.OnClosed(c, err) {
case Shutdown:
if el.eventHandler.OnClosed(c, err) == Shutdown {
return errServerShutdown
}
c.releaseTCP()
Expand Down Expand Up @@ -277,8 +276,7 @@ func (el *eventloop) loopReadUDP(fd int) error {
el.eventHandler.PreWrite()
_ = c.sendTo(out)
}
switch action {
case Shutdown:
if action == Shutdown {
return errServerShutdown
}
c.releaseUDP()
Expand Down
6 changes: 2 additions & 4 deletions examples/custom_codec/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/panjf2000/gnet/examples/custom_codec/protocol"
"io"
"log"
"net"

"github.com/panjf2000/gnet/examples/custom_codec/protocol"
)

// Example command: go run client.go
Expand All @@ -23,7 +24,6 @@ func main() {
for {

response, err := ClientDecode(conn)

if err != nil {
log.Printf("ClientDecode error, %v\n", err)
}
Expand All @@ -48,7 +48,6 @@ func main() {
conn.Write(pbdata)

select {}

}

// ClientEncode :
Expand Down Expand Up @@ -83,7 +82,6 @@ func ClientEncode(pbVersion, actionType uint16, data []byte) ([]byte, error) {

// ClientDecode :
func ClientDecode(rawConn net.Conn) (*protocol.CustomLengthFieldProtocol, error) {

newPackage := protocol.CustomLengthFieldProtocol{}

headData := make([]byte, protocol.DefaultHeadLength)
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_codec/protocol/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (cc *CustomLengthFieldProtocol) Decode(c gnet.Conn) ([]byte, error) {
return nil, errors.New("not normal protocol")
}
// parse payload
dataLen := int(dataLength) //max int32 can contain 210MB payload
dataLen := int(dataLength) // max int32 can contain 210MB payload
protocolLen := headerLen + dataLen
if dataSize, data := c.ReadN(protocolLen); dataSize == protocolLen {
c.ShiftN(protocolLen)
Expand Down
5 changes: 3 additions & 2 deletions examples/custom_codec/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"flag"
"fmt"
"log"
"time"

"github.com/panjf2000/gnet"
"github.com/panjf2000/gnet/examples/custom_codec/protocol"
"github.com/panjf2000/gnet/pool/goroutine"
"log"
"time"
)

type customCodecServer struct {
Expand Down
1 change: 1 addition & 0 deletions examples/echo_tcp/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (es *echoServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
srv.Addr.String(), srv.Multicore, srv.NumEventLoop)
return
}

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
// Echo synchronously.
out = frame
Expand Down
1 change: 1 addition & 0 deletions examples/echo_udp/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (es *echoServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
srv.Addr.String(), srv.Multicore, srv.NumEventLoop)
return
}

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
// Echo synchronously.
out = frame
Expand Down
1 change: 1 addition & 0 deletions examples/echo_uds/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (es *echoServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
srv.Addr.String(), srv.Multicore, srv.NumEventLoop)
return
}

func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
// Echo synchronously.
out = frame
Expand Down
8 changes: 5 additions & 3 deletions examples/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ type httpServer struct {
*gnet.EventServer
}

var errMsg = "Internal Server Error"
var errMsgBytes = []byte(errMsg)
var (
errMsg = "Internal Server Error"
errMsgBytes = []byte(errMsg)
)

type httpCodec struct {
req request
Expand Down Expand Up @@ -144,7 +146,7 @@ func parseReq(data []byte, req *request) (leftover []byte, err error) {
var i, s int
var head string
var clen int
var q = -1
q := -1
// method, path, proto line
for ; i < len(sdata); i++ {
if sdata[i] == ' ' {
Expand Down
4 changes: 4 additions & 0 deletions examples/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ func (ps *pushServer) OnInitComplete(srv gnet.Server) (action gnet.Action) {
"pushing data every %s ...\n", srv.Addr.String(), srv.Multicore, srv.NumEventLoop, ps.tick.String())
return
}

func (ps *pushServer) OnOpened(c gnet.Conn) (out []byte, action gnet.Action) {
log.Printf("Socket with addr: %s has been opened...\n", c.RemoteAddr().String())
ps.connectedSockets.Store(c.RemoteAddr().String(), c)
return
}

func (ps *pushServer) OnClosed(c gnet.Conn, err error) (action gnet.Action) {
log.Printf("Socket with addr: %s is closing...\n", c.RemoteAddr().String())
ps.connectedSockets.Delete(c.RemoteAddr().String())
return
}

func (ps *pushServer) Tick() (delay time.Duration, action gnet.Action) {
log.Println("It's time to push data to clients!!!")
ps.connectedSockets.Range(func(key, value interface{}) bool {
Expand All @@ -42,6 +45,7 @@ func (ps *pushServer) Tick() (delay time.Duration, action gnet.Action) {
delay = ps.tick
return
}

func (ps *pushServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) {
out = frame
return
Expand Down
2 changes: 1 addition & 1 deletion gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type Conn interface {
BufferLength() (size int)

// InboundBuffer returns the inbound ring-buffer.
//InboundBuffer() *ringbuffer.RingBuffer
// InboundBuffer() *ringbuffer.RingBuffer

// SendTo writes data for UDP sockets, it allows you to send data back to UDP socket in individual goroutines.
SendTo(buf []byte) error
Expand Down
Loading

0 comments on commit 13b45a4

Please sign in to comment.