@@ -9,14 +9,15 @@ import (
99 "net"
1010 "time"
1111
12- "github.com/unit-io/unitdb-go/internal/utp"
12+ lp "github.com/unit-io/unitdb-go/internal/net"
13+ "github.com/unit-io/unitdb/server/utp"
1314)
1415
1516// Connect takes a connected net.Conn and performs the initial handshake. Paramaters are:
1617// conn - Connected net.Conn
1718// cm - Connect Message
18- func Connect (conn net.Conn , cm * utp.Connect ) (rc int32 , epoch int32 , cid int32 , err error ) {
19- m , err := utp .Encode (cm )
19+ func Connect (conn net.Conn , cm * utp.Connect ) (rc uint8 , epoch int32 , cid int32 , err error ) {
20+ m , err := lp .Encode (cm )
2021 if err != nil {
2122 fmt .Println (err )
2223 return utp .ErrRefusedServerUnavailable , 0 , 0 , err
@@ -31,21 +32,24 @@ func Connect(conn net.Conn, cm *utp.Connect) (rc int32, epoch int32, cid int32,
3132// when the connection is first started.
3233// This prevents receiving incoming data while resume
3334// is in progress if clean session is false.
34- func verifyCONNACK (conn net.Conn ) (int32 , int32 , int32 , error ) {
35- ca , err := utp .Read (conn )
35+ func verifyCONNACK (conn net.Conn ) (uint8 , int32 , int32 , error ) {
36+ ca , err := lp .Read (conn )
3637 if err != nil {
3738 return utp .ErrRefusedServerUnavailable , 0 , 0 , err
3839 }
3940 if ca == nil {
40- return utp .ErrRefusedServerUnavailable , 0 , 0 , errors .New ("nil Connect Acknowledge Message " )
41+ return utp .ErrRefusedServerUnavailable , 0 , 0 , errors .New ("nil connect acknowledge message " )
4142 }
4243
43- msg , ok := ca .(* utp.ConnectAcknowledge )
44+ pack , ok := ca .(* utp.ControlMessage )
4445 if ! ok {
4546 return utp .ErrRefusedServerUnavailable , 0 , 0 , errors .New ("first message must be connect acknowledge message" )
4647 }
4748
48- return msg .ReturnCode , msg .Epoch , msg .ConnID , nil
49+ connack := & utp.ConnectAcknowledge {}
50+ connack .FromBinary (utp.FixedHeader {MessageType : utp .CONNECT , FlowControl : utp .ACKNOWLEDGE }, pack .Message )
51+
52+ return connack .ReturnCode , connack .Epoch , connack .ConnID , nil
4953}
5054
5155// Handle handles incoming messages
@@ -67,7 +71,7 @@ func (c *client) readLoop(ctx context.Context) error {
6771 return nil
6872 default :
6973 // Unpack an incoming Message
70- msg , err := utp .Read (reader )
74+ msg , err := lp .Read (reader )
7175 if err != nil {
7276 return err
7377 }
@@ -84,7 +88,7 @@ func (c *client) readLoop(ctx context.Context) error {
8488}
8589
8690// handle handles inbound messages.
87- func (c * client ) handler (inMsg utp. Message ) error {
91+ func (c * client ) handler (inMsg lp. MessagePack ) error {
8892 c .updateLastAction ()
8993
9094 switch inMsg .Type () {
@@ -143,7 +147,7 @@ func (c *client) writeLoop(ctx context.Context) {
143147 mId := c .inboundID (msg .MessageID )
144148 c .freeID (mId )
145149 }
146- m , err := utp .Encode (outMsg .m )
150+ m , err := lp .Encode (outMsg .m )
147151 if err != nil {
148152 fmt .Println (err )
149153 // return
@@ -183,7 +187,7 @@ func (c *client) dispatcher(ctx context.Context) {
183187// keepalive - Send ping when connection unused for set period
184188// connection passed in to avoid race condition on shutdown
185189func (c * client ) keepalive (ctx context.Context ) {
186- var pingInterval int64
190+ var pingInterval int32
187191 var pingSent time.Time
188192
189193 if c .opts .keepAlive > 10 {
@@ -192,7 +196,7 @@ func (c *client) keepalive(ctx context.Context) {
192196 pingInterval = c .opts .keepAlive / 2
193197 }
194198
195- pingTicker := time .NewTicker (time .Duration (pingInterval * int64 (time .Second )))
199+ pingTicker := time .NewTicker (time .Duration (pingInterval * int32 (time .Second )))
196200 defer func () {
197201 pingTicker .Stop ()
198202 }()
@@ -206,12 +210,12 @@ func (c *client) keepalive(ctx context.Context) {
206210 case <- pingTicker .C :
207211 lastAction := c .lastAction .Load ().(time.Time )
208212 lastTouched := c .lastTouched .Load ().(time.Time )
209- live := TimeNow ().Add (- time .Duration (c .opts .keepAlive * int64 (time .Second )))
213+ live := TimeNow ().Add (- time .Duration (c .opts .keepAlive * int32 (time .Second )))
210214 timeout := TimeNow ().Add (- c .opts .pingTimeout )
211215
212216 if lastAction .After (live ) || lastTouched .After (live ) {
213217 ping := & utp.Pingreq {}
214- m , err := utp .Encode (ping )
218+ m , err := lp .Encode (ping )
215219 if err != nil {
216220 fmt .Println (err )
217221 }
0 commit comments