Skip to content

Commit

Permalink
Drop unnecessary connkey management
Browse files Browse the repository at this point in the history
partially based on upstream PR: eycorsican#117

Signed-off-by: Syrone Wong <[email protected]>
  • Loading branch information
wongsyrone committed Jul 16, 2020
1 parent 8339de5 commit 3295ba3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 128 deletions.
2 changes: 1 addition & 1 deletion core/lwip.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (s *lwipStack) closeInternal() {

func (s *lwipStack) Close(t LWIPSysCheckTimeoutsClosingType) error {
if s.GetRunningStatus() {
tcpConns.Range(func(_, c interface{}) bool {
tcpConns.Range(func(c, _ interface{}) bool {
c.(*tcpConn).Abort()
return true
})
Expand Down
82 changes: 34 additions & 48 deletions core/tcp_callback_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,11 @@ func tcpRecvFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb, p *C.struct_pbuf, pas
return C.ERR_ABRT
}

conn, ok := GoPointerRestore(arg)
if !ok {
// The connection does not exists.
C.tcp_abort(tpcb)
shouldFreePbuf = true
return C.ERR_ABRT
}
var conn = (*tcpConn)(arg)

if p == nil {
// Peer closed, EOF.
err := conn.(TCPConn).LocalClosed()
err := conn.LocalClosed()
switch err.(*lwipError).Code {
case LWIP_ERR_ABRT:
shouldFreePbuf = true
Expand All @@ -101,7 +95,7 @@ func tcpRecvFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb, p *C.struct_pbuf, pas
C.pbuf_copy_partial(p, unsafe.Pointer(&buf[0]), p.tot_len, 0)
}

rerr := conn.(TCPConn).Receive(buf[:totlen])
rerr := conn.Receive(buf[:totlen])
if rerr != nil {
switch rerr.(*lwipError).Code {
case LWIP_ERR_ABRT:
Expand Down Expand Up @@ -130,56 +124,48 @@ func tcpRecvFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb, p *C.struct_pbuf, pas

//export tcpSentFn
func tcpSentFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb, len C.u16_t) C.err_t {
if conn, ok := GoPointerRestore(arg); ok {
err := conn.(TCPConn).Sent(uint16(len))
switch err.(*lwipError).Code {
case LWIP_ERR_ABRT:
return C.ERR_ABRT
case LWIP_ERR_OK:
return C.ERR_OK
default:
panic("unexpected error")
}
} else {
lwipMutex.Lock()
defer lwipMutex.Unlock()
C.tcp_abort(tpcb)
var conn = (*tcpConn)(arg)

err := conn.Sent(uint16(len))
switch err.(*lwipError).Code {
case LWIP_ERR_ABRT:
return C.ERR_ABRT
case LWIP_ERR_OK:
return C.ERR_OK
default:
panic("unexpected error")
}

}

//export tcpErrFn
func tcpErrFn(arg unsafe.Pointer, err C.err_t) {
if conn, ok := GoPointerRestore(arg); ok {
switch err {
case C.ERR_ABRT:
// Aborted through tcp_abort or by a TCP timer
conn.(TCPConn).Err(errors.New("connection aborted"))
case C.ERR_RST:
// The connection was reset by the remote host
conn.(TCPConn).Err(errors.New("connection reseted"))
default:
conn.(TCPConn).Err(errors.New(fmt.Sprintf("lwip error code %v", int(err))))
}
var conn = (*tcpConn)(arg)

switch err {
case C.ERR_ABRT:
// Aborted through tcp_abort or by a TCP timer
conn.Err(errors.New("connection aborted"))
case C.ERR_RST:
// The connection was reset by the remote host
conn.Err(errors.New("connection reseted"))
default:
conn.Err(errors.New(fmt.Sprintf("lwip error code %v", int(err))))
}
}

//export tcpPollFn
func tcpPollFn(arg unsafe.Pointer, tpcb *C.struct_tcp_pcb) C.err_t {
if conn, ok := GoPointerRestore(arg); ok {
err := conn.(TCPConn).Poll()
switch err.(*lwipError).Code {
case LWIP_ERR_ABRT:
return C.ERR_ABRT
case LWIP_ERR_OK:
return C.ERR_OK
default:
panic("unexpected error")
}
} else {
lwipMutex.Lock()
defer lwipMutex.Unlock()
C.tcp_abort(tpcb)
var conn = (*tcpConn)(arg)

err := conn.Poll()
switch err.(*lwipError).Code {
case LWIP_ERR_ABRT:
return C.ERR_ABRT
case LWIP_ERR_OK:
return C.ERR_OK
default:
panic("unexpected error")
}

}
22 changes: 9 additions & 13 deletions core/tcp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ tcp_keepalive_settings_cgo(struct tcp_pcb *pcb)
pcb->so_options |= SOF_KEEPALIVE;
#endif
}
void tcp_arg_cgo(struct tcp_pcb *pcb, uintptr_t ptr) {
tcp_arg(pcb, (void*)ptr);
}
*/
import "C"
import (
Expand All @@ -39,6 +43,8 @@ import (

type tcpConnState uint

var tcpConns sync.Map

const (
// tcpNewConn is the initial state.
tcpNewConn tcpConnState = iota
Expand Down Expand Up @@ -83,7 +89,6 @@ type tcpConn struct {
handler TCPConnHandler
remoteAddr *net.TCPAddr
localAddr *net.TCPAddr
connKey unsafe.Pointer
state tcpConnState
sndPipeReader *nio.PipeReader
sndPipeWriter *nio.PipeWriter
Expand Down Expand Up @@ -111,17 +116,13 @@ func newTCPConn(pcb *C.struct_tcp_pcb, handler TCPConnHandler) (TCPConn, error)
handler: handler,
localAddr: ParseTCPAddr(ipAddrNTOA(pcb.remote_ip), uint16(pcb.remote_port)),
remoteAddr: ParseTCPAddr(ipAddrNTOA(pcb.local_ip), uint16(pcb.local_port)),
connKey: nil,
state: tcpNewConn,
sndPipeReader: pipeReader,
sndPipeWriter: pipeWriter,
}

// Associate conn with key and save to the global map.
identifierPtr := GoPointerSave(conn)
conn.SetConnKey(identifierPtr)
// Pass the pointer identifier subsequent tcp callbacks.
C.tcp_arg(pcb, identifierPtr)
C.tcp_arg_cgo(pcb, C.uintptr_t(uintptr(unsafe.Pointer(conn))))
tcpConns.Store(conn, true)

// Connecting remote host could take some time, do it in another goroutine
// to prevent blocking the lwip thread.
Expand Down Expand Up @@ -160,11 +161,6 @@ func (conn *tcpConn) SetWriteDeadline(t time.Time) error {
return nil
}

func (conn *tcpConn) SetConnKey(p unsafe.Pointer) error {
conn.connKey = p
return nil
}

func (conn *tcpConn) receiveCheck() error {
conn.Lock()
defer conn.Unlock()
Expand Down Expand Up @@ -508,7 +504,7 @@ func (conn *tcpConn) release() {
lwipMutex.Lock()
defer lwipMutex.Unlock()

GoPointerUnref(conn.connKey)
tcpConns.Delete(conn)

conn.sndPipeWriter.Close()
conn.sndPipeReader.Close()
Expand Down
66 changes: 0 additions & 66 deletions core/tcp_conn_map.go

This file was deleted.

0 comments on commit 3295ba3

Please sign in to comment.