11package client
22
33import (
4- "sync/atomic"
54 "encoding/binary"
65 "log"
76 "net"
87 "strconv"
98 "strings"
9+ "sync/atomic"
1010 "time"
1111
1212 "github.com/leviathan1995/spleen/service"
@@ -37,7 +37,7 @@ func NewClient(clientID int, serverIP string, serverPort int, limitRate []string
3737 }
3838}
3939
40- var connections int64 = 0 ;
40+ var connections uint64 = 0
4141
4242func (c * client ) Run () {
4343 log .Printf ("Begin to running the client[%d]" , c .clientID )
@@ -46,20 +46,21 @@ func (c *client) Run() {
4646 }
4747
4848 for {
49- if atomic .LoadInt64 (& connections ) < 10 {
50- for i := atomic .LoadInt64 (& connections ); i < 10 ; i ++ {
49+ if atomic .LoadUint64 (& connections ) < 10 {
50+ for i := atomic .LoadUint64 (& connections ); i < 10 ; i ++ {
5151 srvConn , err := c .DialSrv ()
5252 if err != nil {
53- log .Printf ("Connect to the server %s:%d failed: %s. \n " , c .srvAddr .IP .String (), c .srvAddr .Port , err )
53+ log .Printf ("Connect to the proxy %s:%d failed: %s. \n " , c .srvAddr .IP .String (), c .srvAddr .Port , err )
5454 continue
5555 }
56- log .Printf ("Connect to the server %s:%d successful.\n " , c .srvAddr .IP .String (), c .srvAddr .Port )
56+ log .Printf ("Connect to the proxy %s:%d successful.\n " , c .srvAddr .IP .String (), c .srvAddr .Port )
5757 srvConn .SetKeepAlive (true )
5858 srvConn .SetLinger (0 )
59- connections = atomic .AddInt64 (& connections , 1 )
59+ atomic . StoreUint64 ( & connections , atomic .AddUint64 (& connections , 1 ) )
6060 go c .handleConn (srvConn )
6161 }
6262 } else {
63+ log .Printf ("Currently, We still have %d active connections." , atomic .LoadUint64 (& connections ))
6364 time .Sleep (1 * time .Second )
6465 }
6566 }
@@ -70,55 +71,57 @@ func (c *client) DialSrv() (*net.TCPConn, error) {
7071}
7172
7273func (c * client ) handleConn (srvConn * net.TCPConn ) {
73- defer srvConn .Close ()
74-
75- transBuf := make ([]byte , 8 )
74+ transBuf := make ([]byte , service .IDBuf )
7675 /* Send the ID of client to proxy. */
7776 binary .LittleEndian .PutUint64 (transBuf , uint64 (c .clientID ))
7877 err := c .TCPWrite (srvConn , transBuf )
7978 if err != nil {
80- connections = atomic .AddInt64 (& connections , - 1 )
79+ atomic .StoreUint64 (& connections , atomic .AddUint64 (& connections , ^ uint64 (1 - 1 )))
80+ _ = srvConn .Close ()
8181 log .Println ("Try to send the ID of client to the proxy failed." )
8282 return
8383 }
8484
8585 /* Waiting for the transfer port from proxy. */
86- nRead , err := srvConn . Read ( transBuf )
87- connections = atomic .AddInt64 (& connections , - 1 )
86+ err = c . TCPRead ( srvConn , transBuf , service . PortBuf )
87+ atomic . StoreUint64 ( & connections , atomic .AddUint64 (& connections , ^ uint64 ( 1 - 1 )) )
8888 if err != nil {
89- log .Println ("Try to read the destination port failed." )
89+ _ = srvConn .Close ()
90+ log .Println ("Try to read destination port from the proxy failed." )
9091 return
9192 }
92- port := int64 (binary .LittleEndian .Uint64 (transBuf [: nRead ] ))
93+ port := int64 (binary .LittleEndian .Uint64 (transBuf ))
9394
9495 /* Try to direct connect to the destination sever. */
9596 dstAddr , err := net .ResolveTCPAddr ("tcp" , ":" + strconv .Itoa (int (port )))
9697 if err != nil {
97- log .Printf ("Try to resolve TCPAddr %s failed: %s.\n " , "localhost" + ":" + string (port ), err .Error ())
98+ _ = srvConn .Close ()
99+ log .Printf ("Try to resolve TCPAddr %s failed: %s.\n " , "localhost" + ":" + strconv .FormatInt (port , 10 ), err .Error ())
98100 return
99101 }
100102
101103 dstConn , err := net .DialTCP ("tcp" , nil , dstAddr )
102104 if err != nil {
105+ _ = srvConn .Close ()
103106 log .Printf ("Connect to localhost:%d failed." , dstAddr .Port )
104107 return
105108 } else {
106109 log .Printf ("Connect to the destination address localhost:%d successful." , dstAddr .Port )
107110 }
108- defer dstConn .Close ()
109111
110- dstConn .SetKeepAlive (true )
112+ _ = dstConn .SetKeepAlive (true )
111113 _ = dstConn .SetLinger (0 )
112114
113115 var limitRate int64
114-
115116 if rate , found := c .limitRate [port ]; found {
116117 limitRate = rate * 1024 /* bytes */
117118 }
118119
119120 go func () {
120121 errTransfer := c .TransferToTCP (dstConn , srvConn , limitRate )
121122 if errTransfer != nil {
123+ _ = srvConn .Close ()
124+ _ = dstConn .Close ()
122125 return
123126 }
124127 }()
0 commit comments