Skip to content

Commit

Permalink
trifle: rename structs, functions and variables with more rational names
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Feb 9, 2020
1 parent dbb3b65 commit 8a59a51
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 204 deletions.
12 changes: 6 additions & 6 deletions acceptor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func (svr *server) acceptNewConnection(fd int) error {
if err := unix.SetNonblock(nfd, true); err != nil {
return err
}
lp := svr.subLoopGroup.next()
c := newTCPConn(nfd, lp, sa)
_ = lp.poller.Trigger(func() (err error) {
if err = lp.poller.AddRead(nfd); err != nil {
el := svr.subLoopGroup.next()
c := newTCPConn(nfd, el, sa)
_ = el.poller.Trigger(func() (err error) {
if err = el.poller.AddRead(nfd); err != nil {
return
}
lp.connections[nfd] = c
err = lp.loopOpen(c)
el.connections[nfd] = c
err = el.loopOpen(c)
return
})
return nil
Expand Down
14 changes: 7 additions & 7 deletions acceptor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ func (svr *server) listenerRun() {
buf := bytebuffer.Get()
_, _ = buf.Write(packet[:n])

lp := svr.subLoopGroup.next()
lp.ch <- &udpIn{newUDPConn(lp, svr.ln.lnaddr, addr, buf)}
el := svr.subLoopGroup.next()
el.ch <- &udpIn{newUDPConn(el, svr.ln.lnaddr, addr, buf)}
} else {
// Accept TCP socket.
conn, e := svr.ln.ln.Accept()
if e != nil {
err = e
return
}
lp := svr.subLoopGroup.next()
c := newTCPConn(conn, lp)
lp.ch <- c
el := svr.subLoopGroup.next()
c := newTCPConn(conn, el)
el.ch <- c
go func() {
var packet [0xFFFF]byte
for {
n, err := c.conn.Read(packet[:])
if err != nil {
_ = c.conn.SetReadDeadline(time.Time{})
lp.ch <- &stderr{c, err}
el.ch <- &stderr{c, err}
return
}
buf := bytebuffer.Get()
_, _ = buf.Write(packet[:n])
lp.ch <- &tcpIn{c, buf}
el.ch <- &tcpIn{c, buf}
}
}()
}
Expand Down
12 changes: 6 additions & 6 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type conn struct {
fd int // file descriptor
sa unix.Sockaddr // remote socket address
ctx interface{} // user-defined context
loop *loop // connected loop
loop *eventloop // connected event-loop
buffer []byte // reuse memory of inbound data as a temporary buffer
codec ICodec // codec for TCP
opened bool // connection opened event fired
Expand All @@ -32,12 +32,12 @@ type conn struct {
outboundBuffer *ringbuffer.RingBuffer // buffer for data that is ready to write to client
}

func newTCPConn(fd int, lp *loop, sa unix.Sockaddr) *conn {
func newTCPConn(fd int, el *eventloop, sa unix.Sockaddr) *conn {
return &conn{
fd: fd,
sa: sa,
loop: lp,
codec: lp.codec,
loop: el,
codec: el.codec,
inboundBuffer: prb.Get(),
outboundBuffer: prb.Get(),
}
Expand All @@ -58,11 +58,11 @@ func (c *conn) releaseTCP() {
c.byteBuffer = nil
}

func newUDPConn(fd int, lp *loop, sa unix.Sockaddr) *conn {
func newUDPConn(fd int, el *eventloop, sa unix.Sockaddr) *conn {
return &conn{
fd: fd,
sa: sa,
localAddr: lp.svr.ln.lnaddr,
localAddr: el.svr.ln.lnaddr,
remoteAddr: netpoll.SockaddrToUDPAddr(sa),
}
}
Expand Down
12 changes: 6 additions & 6 deletions connection_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type udpIn struct {
type stdConn struct {
ctx interface{} // user-defined context
conn net.Conn // original connection
loop *loop // owner loop
loop *eventloop // owner event-loop
done int32 // 0: attached, 1: closed
buffer *bytebuffer.ByteBuffer // reuse memory of inbound data as a temporary buffer
codec ICodec // codec for TCP
Expand All @@ -46,11 +46,11 @@ type stdConn struct {
inboundBuffer *ringbuffer.RingBuffer // buffer for data from client
}

func newTCPConn(conn net.Conn, lp *loop) *stdConn {
func newTCPConn(conn net.Conn, el *eventloop) *stdConn {
return &stdConn{
conn: conn,
loop: lp,
codec: lp.codec,
loop: el,
codec: el.codec,
inboundBuffer: prb.Get(),
}
}
Expand All @@ -65,9 +65,9 @@ func (c *stdConn) releaseTCP() {
c.buffer = nil
}

func newUDPConn(lp *loop, localAddr, remoteAddr net.Addr, buf *bytebuffer.ByteBuffer) *stdConn {
func newUDPConn(el *eventloop, localAddr, remoteAddr net.Addr, buf *bytebuffer.ByteBuffer) *stdConn {
return &stdConn{
loop: lp,
loop: el,
localAddr: localAddr,
remoteAddr: remoteAddr,
buffer: buf,
Expand Down
22 changes: 11 additions & 11 deletions eventloop_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,38 @@ package gnet
// IEventLoopGroup represents a set of event-loops.
type (
IEventLoopGroup interface {
register(*loop)
next() *loop
iterate(func(int, *loop) bool)
register(*eventloop)
next() *eventloop
iterate(func(int, *eventloop) bool)
len() int
}

eventLoopGroup struct {
nextLoopIndex int
eventLoops []*loop
eventLoops []*eventloop
size int
}
)

func (g *eventLoopGroup) register(lp *loop) {
g.eventLoops = append(g.eventLoops, lp)
func (g *eventLoopGroup) register(el *eventloop) {
g.eventLoops = append(g.eventLoops, el)
g.size++
}

// Built-in load-balance algorithm is Round-Robin.
// TODO: support more load-balance algorithms.
func (g *eventLoopGroup) next() (lp *loop) {
lp = g.eventLoops[g.nextLoopIndex]
func (g *eventLoopGroup) next() (el *eventloop) {
el = g.eventLoops[g.nextLoopIndex]
g.nextLoopIndex++
if g.nextLoopIndex >= g.size {
g.nextLoopIndex = 0
}
return
}

func (g *eventLoopGroup) iterate(f func(int, *loop) bool) {
for i, lp := range g.eventLoops {
if !f(i, lp) {
func (g *eventLoopGroup) iterate(f func(int, *eventloop) bool) {
for i, el := range g.eventLoops {
if !f(i, el) {
break
}
}
Expand Down
Loading

0 comments on commit 8a59a51

Please sign in to comment.