@@ -2,14 +2,13 @@ package rscp
2
2
3
3
import (
4
4
"crypto/cipher"
5
+ "errors"
5
6
"fmt"
6
7
"io"
7
8
"math"
8
9
"net"
9
10
"time"
10
11
11
- "errors"
12
-
13
12
"github.com/azihsoyn/rijndael256"
14
13
"github.com/sirupsen/logrus"
15
14
)
@@ -20,7 +19,6 @@ var Log = logrus.New()
20
19
type Client struct {
21
20
config ClientConfig
22
21
connectionString string
23
- isConnected bool
24
22
isAuthenticated bool
25
23
conn net.Conn
26
24
encrypter cipher.BlockMode
@@ -34,15 +32,15 @@ func NewClient(config ClientConfig) (*Client, error) {
34
32
if err := config .check (); err != nil {
35
33
return nil , err
36
34
}
35
+
37
36
key := createAESKey (config .Key )
38
37
initIV := newIV ()
39
38
cipherBlock , _ := rijndael256 .NewCipher (key [:]) // implementation does not return an error
39
+
40
40
// Intitialize the Client structure.
41
41
c := & Client {
42
42
config : config ,
43
43
connectionString : fmt .Sprintf ("%s:%d" , config .Address , config .Port ),
44
- isConnected : false ,
45
- isAuthenticated : false ,
46
44
encrypter : cipher .NewCBCEncrypter (cipherBlock , initIV [:]),
47
45
decrypter : cipher .NewCBCDecrypter (cipherBlock , initIV [:]),
48
46
}
@@ -54,11 +52,8 @@ func (c *Client) send(messages []Message) error {
54
52
if err := validateRequests (messages ); err != nil {
55
53
return err
56
54
}
57
- var (
58
- msg []byte
59
- err error
60
- )
61
- if msg , err = Write (& c .encrypter , messages , c .config .UseChecksum .(bool )); err != nil {
55
+ msg , err := Write (& c .encrypter , messages , c .config .UseChecksum .(bool ))
56
+ if err != nil {
62
57
return err
63
58
}
64
59
if err := c .conn .SetWriteDeadline (time .Now ().Add (c .config .SendTimeout )); err != nil {
@@ -84,11 +79,13 @@ func (c *Client) receive() ([]Message, error) {
84
79
85
80
for i , data := 0 , make ([]byte , uint32 (RSCP_CRYPT_BLOCK_SIZE )* uint32 (c .config .ReceiveBufferBlockSize )); ; {
86
81
var err error
82
+
87
83
if i , err = c .conn .Read (data ); err != nil {
88
84
return nil , fmt .Errorf ("error during receive response: %w" , err )
89
85
} else if i == 0 {
90
86
return nil , ErrRscpInvalidFrameLength
91
87
}
88
+
92
89
switch m , err = Read (& c .decrypter , & buf , & crcFlag , & frameSize , & dataSize , data [:i ]); {
93
90
case errors .Is (err , ErrRscpInvalidFrameLength ):
94
91
// frame not complete
@@ -105,17 +102,15 @@ func (c *Client) receive() ([]Message, error) {
105
102
// connect create connection
106
103
func (c * Client ) connect () error {
107
104
Log .Infof ("Connecting to %s" , c .connectionString )
108
- var (
109
- conn net.Conn
110
- err error
111
- )
112
- if conn , err = net .DialTimeout ("tcp" , c .connectionString , c .config .ConnectionTimeout ); err != nil {
113
- c .isConnected = false
105
+
106
+ conn , err := net .DialTimeout ("tcp" , c .connectionString , c .config .ConnectionTimeout )
107
+ if err != nil {
114
108
return err
115
109
}
110
+
111
+ Log .Infof ("successfully connected to %s" , conn .RemoteAddr ())
116
112
c .conn = conn
117
- c .isConnected = true
118
- Log .Infof ("successfully connected to %s" , c .conn .RemoteAddr ())
113
+
119
114
return nil
120
115
}
121
116
@@ -141,20 +136,20 @@ func (c *Client) authenticate() error {
141
136
if orgLogLevel < RequiredAuthLogLevel {
142
137
Log .SetLevel (orgLogLevel )
143
138
}
144
- var (
145
- messages []Message
146
- err error
147
- )
148
- if messages , err = c .receive (); err != nil {
139
+
140
+ messages , err := c .receive ()
141
+ if err != nil {
149
142
if errors .Is (err , io .EOF ) {
150
143
Log .Warnf ("Hint: EOF during authentification usually is due a wrong rscp key" )
151
144
}
152
145
return fmt .Errorf ("authentication error: %w" , err )
153
146
}
147
+
154
148
if messages [0 ].Tag != RSCP_AUTHENTICATION {
155
149
c .isAuthenticated = false
156
150
return fmt .Errorf ("authentication failed: %+v" , messages [0 ])
157
151
}
152
+
158
153
switch v := messages [0 ].Value .(type ) {
159
154
default :
160
155
c .isAuthenticated = false
@@ -177,38 +172,35 @@ func (c *Client) authenticate() error {
177
172
}
178
173
179
174
// Disconnect the client
180
- func (c * Client ) Disconnect () error {
175
+ func (c * Client ) Disconnect () ( err error ) {
181
176
c .isAuthenticated = false
182
- c .isConnected = false
183
177
184
- if c .isConnected && c . conn != nil {
185
- if err : = c .conn .Close (); err != nil {
186
- return err
187
- }
178
+ if c .conn != nil {
179
+ err = c .conn .Close ()
180
+ c . conn = nil
181
+ Log . Info ( "disconnected" )
188
182
}
189
- Log . Info ( "disconnected" )
190
- return nil
183
+
184
+ return err
191
185
}
192
186
193
187
// Send a message and return the response.
194
188
//
195
189
// connects and authenticates the first time used.
196
190
func (c * Client ) Send (request Message ) (* Message , error ) {
197
- var (
198
- responses []Message
199
- err error
200
- )
201
- if responses , err = (c .SendMultiple ([]Message {request })); err != nil {
191
+ responses , err := c .SendMultiple ([]Message {request })
192
+ if err != nil {
202
193
return nil , err
203
194
}
195
+
204
196
return & responses [0 ], nil
205
197
}
206
198
207
199
// Send multiple messages in one round-trip and return the response.
208
200
//
209
201
// connects and authenticates the first time used.
210
202
func (c * Client ) SendMultiple (requests []Message ) ([]Message , error ) {
211
- if ! c . isConnected {
203
+ if c . conn == nil {
212
204
if err := c .connect (); err != nil {
213
205
return nil , err
214
206
}
@@ -221,12 +213,5 @@ func (c *Client) SendMultiple(requests []Message) ([]Message, error) {
221
213
if err := c .send (requests ); err != nil {
222
214
return nil , err
223
215
}
224
- var (
225
- responses []Message
226
- err error
227
- )
228
- if responses , err = c .receive (); err != nil {
229
- return nil , err
230
- }
231
- return responses , nil
216
+ return c .receive ()
232
217
}
0 commit comments