From 92f951544b9f27cae1e652f168e34e14f58667f7 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Wed, 22 Mar 2017 16:33:09 -0600 Subject: [PATCH] simplified some function inputs and returns --- .../api_collect_status_traffic.go | 8 ++-- rvpn/genericlistener/conn_tracking.go | 9 ++-- rvpn/genericlistener/conn_wedge.go | 16 ++----- rvpn/genericlistener/connection.go | 44 +++++++++---------- rvpn/genericlistener/connection_table.go | 26 +++++------ rvpn/genericlistener/domain_track.go | 10 ++--- rvpn/genericlistener/listener_generic.go | 2 +- rvpn/genericlistener/one_conn.go | 16 +++---- rvpn/genericlistener/status.go | 1 - rvpn/genericlistener/status_dead_time.go | 2 +- 10 files changed, 56 insertions(+), 78 deletions(-) diff --git a/rvpn/genericlistener/api_collect_status_traffic.go b/rvpn/genericlistener/api_collect_status_traffic.go index b5ba12a..ff6ccd9 100644 --- a/rvpn/genericlistener/api_collect_status_traffic.go +++ b/rvpn/genericlistener/api_collect_status_traffic.go @@ -9,12 +9,12 @@ type TrafficAPI struct { } //NewTrafficStats -- Consttuctor -func NewTrafficAPI(requests int64, responses int64, bytes_in int64, bytes_out int64) (p *TrafficAPI) { +func NewTrafficAPI(requests, responses, bytesIn, bytesOut int64) (p *TrafficAPI) { p = new(TrafficAPI) p.Requests = requests p.Responses = responses - p.BytesIn = bytes_in - p.BytesOut = bytes_out + p.BytesIn = bytesIn + p.BytesOut = bytesOut return -} \ No newline at end of file +} diff --git a/rvpn/genericlistener/conn_tracking.go b/rvpn/genericlistener/conn_tracking.go index 3cc6a34..ed9ed71 100644 --- a/rvpn/genericlistener/conn_tracking.go +++ b/rvpn/genericlistener/conn_tracking.go @@ -79,17 +79,14 @@ func (p *Tracking) list() { //Lookup -- // - get connection from key -func (p *Tracking) Lookup(key string) (c *Track, err error) { +func (p *Tracking) Lookup(key string) (*Track, error) { defer func() { p.mutex.Unlock() }() p.mutex.Lock() if _, ok := p.connections[key]; ok { - c = p.connections[key] - } else { - err = fmt.Errorf("Lookup failed for %s", key) - c = nil + return p.connections[key], nil } - return + return nil, fmt.Errorf("Lookup failed for %s", key) } diff --git a/rvpn/genericlistener/conn_wedge.go b/rvpn/genericlistener/conn_wedge.go index 579db3c..edc174a 100644 --- a/rvpn/genericlistener/conn_wedge.go +++ b/rvpn/genericlistener/conn_wedge.go @@ -28,7 +28,7 @@ func NewWedgeConnSize(c net.Conn, size int) (p *WedgeConn) { } //Discard - discard a number of bytes, perhaps after peeking at the -func (w *WedgeConn) Discard(n int) (discarded int, err error) { +func (w *WedgeConn) Discard(n int) (int, error) { return w.reader.Discard(n) } @@ -44,8 +44,7 @@ func (w *WedgeConn) ReadByte() (byte, error) { //Read -- A normal reader. func (w *WedgeConn) Read(p []byte) (int, error) { - cnt, err := w.reader.Read(p) - return cnt, err + return w.reader.Read(p) } //Buffered -- @@ -56,13 +55,6 @@ func (w *WedgeConn) Buffered() int { //PeekAll -- // - get all the chars available // - pass then back -func (w *WedgeConn) PeekAll() (buf []byte, err error) { - - _, err = w.Peek(1) - if err != nil { - return nil, err - } - - buf, err = w.Peek(w.Buffered()) - return +func (w *WedgeConn) PeekAll() ([]byte, error) { + return w.Peek(w.Buffered()) } diff --git a/rvpn/genericlistener/connection.go b/rvpn/genericlistener/connection.go index 201f96f..93cd988 100755 --- a/rvpn/genericlistener/connection.go +++ b/rvpn/genericlistener/connection.go @@ -106,27 +106,23 @@ func (c *Connection) AddTrackedDomain(domain string) { } //InitialDomains -- Property -func (c *Connection) InitialDomains() (i []interface{}) { - i = c.initialDomains - return +func (c *Connection) InitialDomains() []interface{} { + return c.initialDomains } //ConnectTime -- Property -func (c *Connection) ConnectTime() (t time.Time) { - t = c.connectTime - return +func (c *Connection) ConnectTime() time.Time { + return c.connectTime } //BytesIn -- Property -func (c *Connection) BytesIn() (b int64) { - b = c.bytesIn - return +func (c *Connection) BytesIn() int64 { + return c.bytesIn } //BytesOut -- Property -func (c *Connection) BytesOut() (b int64) { - b = c.bytesOut - return +func (c *Connection) BytesOut() int64 { + return c.bytesOut } //SendCh -- property to sending channel @@ -156,9 +152,8 @@ func (c *Connection) addResponse() { } //ConnectionTable -- property -func (c *Connection) ConnectionTable() (table *Table) { - table = c.connectionTable - return +func (c *Connection) ConnectionTable() *Table { + return c.connectionTable } //GetState -- Get state of Socket...this is a high level state. @@ -203,21 +198,24 @@ func (c *Connection) ConnectionID() int64 { //NextWriter -- Wrapper to allow a high level state check before offering NextWriter //The libary failes if client abends during write-cycle. a fast moving write is not caught before socket state bubbles up //A synchronised state is maintained -func (c Connection) NextWriter(wssMessageType int) (w io.WriteCloser, err error) { +func (c *Connection) NextWriter(wssMessageType int) (io.WriteCloser, error) { if c.GetState() == true { - w, err = c.conn.NextWriter(wssMessageType) - } else { - loginfo.Println("NextWriter aborted, state is not true") + return c.conn.NextWriter(wssMessageType) } - return + + // Is returning a nil error actually the proper thing to do here? + loginfo.Println("NextWriter aborted, state is not true") + return nil, nil } //Write -- Wrapper to allow a high level state check before allowing a write to the socket. -func (c *Connection) Write(w io.WriteCloser, message []byte) (cnt int, err error) { +func (c *Connection) Write(w io.WriteCloser, message []byte) (int, error) { if c.GetState() == true { - cnt, err = w.Write(message) + return w.Write(message) } - return + + // Is returning a nil error actually the proper thing to do here? + return 0, nil } //Reader -- export the reader function diff --git a/rvpn/genericlistener/connection_table.go b/rvpn/genericlistener/connection_table.go index b946bba..3312e08 100755 --- a/rvpn/genericlistener/connection_table.go +++ b/rvpn/genericlistener/connection_table.go @@ -24,7 +24,7 @@ type Table struct { } //NewTable -- consructor -func NewTable(dwell int, idle int) (p *Table) { +func NewTable(dwell, idle int) (p *Table) { p = new(Table) p.connections = make(map[*Connection][]string) p.domains = make(map[string]*Connection) @@ -38,15 +38,14 @@ func NewTable(dwell int, idle int) (p *Table) { } //Connections Property -func (c *Table) Connections() (table map[*Connection][]string) { - table = c.connections - return +func (c *Table) Connections() map[*Connection][]string { + return c.connections } //ConnByDomain -- Obtains a connection from a domain announcement. -func (c *Table) ConnByDomain(domain string) (conn *Connection, ok bool) { - conn, ok = c.domains[domain] - return +func (c *Table) ConnByDomain(domain string) (*Connection, bool) { + conn, ok := c.domains[domain] + return conn, ok } //reaper -- @@ -69,16 +68,14 @@ func (c *Table) reaper(delay int, idle int) { } //GetConnection -- find connection by server-id -func (c *Table) GetConnection(serverID int64) (conn *Connection, err error) { +func (c *Table) GetConnection(serverID int64) (*Connection, error) { for conn := range c.connections { if conn.ConnectionID() == serverID { - return conn, err + return conn, nil } } - err = fmt.Errorf("Server-id %d not found", serverID) - - return nil, err + return nil, fmt.Errorf("Server-id %d not found", serverID) } //Run -- Execute @@ -144,7 +141,6 @@ func (c *Table) Run(ctx context.Context) { } //Register -- Property -func (c *Table) Register() (r chan *Registration) { - r = c.register - return +func (c *Table) Register() chan *Registration { + return c.register } diff --git a/rvpn/genericlistener/domain_track.go b/rvpn/genericlistener/domain_track.go index 2bb2ed2..6bf17c0 100644 --- a/rvpn/genericlistener/domain_track.go +++ b/rvpn/genericlistener/domain_track.go @@ -21,15 +21,13 @@ func NewDomainTrack(domainName string) (p *DomainTrack) { } //BytesIn -- Property -func (c *DomainTrack) BytesIn() (b int64) { - b = c.bytesIn - return +func (c *DomainTrack) BytesIn() int64 { + return c.bytesIn } //BytesOut -- Property -func (c *DomainTrack) BytesOut() (b int64) { - b = c.bytesOut - return +func (c *DomainTrack) BytesOut() int64 { + return c.bytesOut } //AddIn - Property diff --git a/rvpn/genericlistener/listener_generic.go b/rvpn/genericlistener/listener_generic.go index f17b986..fa563b4 100644 --- a/rvpn/genericlistener/listener_generic.go +++ b/rvpn/genericlistener/listener_generic.go @@ -271,7 +271,7 @@ func handleStream(ctx context.Context, wConn *WedgeConn) { //handleExternalHTTPRequest - // - get a wConn and start processing requests -func handleExternalHTTPRequest(ctx context.Context, extConn *WedgeConn, hostname string, service string) { +func handleExternalHTTPRequest(ctx context.Context, extConn *WedgeConn, hostname, service string) { //connectionTracking := ctx.Value(ctxConnectionTrack).(*Tracking) serverStatus := ctx.Value(ctxServerStatus).(*Status) diff --git a/rvpn/genericlistener/one_conn.go b/rvpn/genericlistener/one_conn.go index 1e9b104..ffc17cd 100644 --- a/rvpn/genericlistener/one_conn.go +++ b/rvpn/genericlistener/one_conn.go @@ -9,18 +9,16 @@ type oneConnListener struct { conn net.Conn } -func (l *oneConnListener) Accept() (c net.Conn, err error) { - c = l.conn - - if c == nil { - err = io.EOF - loginfo.Println("Accept") - return +func (l *oneConnListener) Accept() (net.Conn, error) { + if l.conn == nil { + loginfo.Println("Accept EOF") + return nil, io.EOF } - err = nil + + c := l.conn l.conn = nil loginfo.Println("Accept", c.LocalAddr().String(), c.RemoteAddr().String()) - return + return c, nil } func (l *oneConnListener) Close() error { diff --git a/rvpn/genericlistener/status.go b/rvpn/genericlistener/status.go index 0578ec0..631b85e 100644 --- a/rvpn/genericlistener/status.go +++ b/rvpn/genericlistener/status.go @@ -46,7 +46,6 @@ func (p *Status) WSSConnectionRegister(newRegistration *Registration) { //unregisters a south facing connection //intercept and update global statistics func (p *Status) WSSConnectionUnregister() { - } // External Facing Functions diff --git a/rvpn/genericlistener/status_dead_time.go b/rvpn/genericlistener/status_dead_time.go index 637170c..f68a9c9 100644 --- a/rvpn/genericlistener/status_dead_time.go +++ b/rvpn/genericlistener/status_dead_time.go @@ -8,7 +8,7 @@ type StatusDeadTime struct { } //NewStatusDeadTime -- constructor -func NewStatusDeadTime(dwell int, idle int, cancelcheck int) (p *StatusDeadTime) { +func NewStatusDeadTime(dwell, idle, cancelcheck int) (p *StatusDeadTime) { p = new(StatusDeadTime) p.dwell = dwell p.idle = idle