@@ -17,6 +17,7 @@ import (
17
17
18
18
// Call represents an active RPC.
19
19
type Call struct {
20
+ Seq uint64
20
21
ServiceMethod string // format "<service>.<method>"
21
22
Args interface {} // arguments to the function
22
23
Reply interface {} // reply from the function
@@ -34,7 +35,7 @@ func (call *Call) done() {
34
35
// multiple goroutines simultaneously.
35
36
type Client struct {
36
37
cc codec.Codec
37
- opt * Options
38
+ opt * Option
38
39
sending sync.Mutex // protect following
39
40
header codec.Header
40
41
mu sync.Mutex // protect following
@@ -96,6 +97,7 @@ func (client *Client) send(call *Call) {
96
97
97
98
// register this call.
98
99
seq , err := client .registerCall (call )
100
+ call .Seq = seq
99
101
if err != nil {
100
102
call .Error = err
101
103
call .done ()
@@ -173,44 +175,41 @@ func (client *Client) Call(serviceMethod string, args, reply interface{}) error
173
175
return call .Error
174
176
}
175
177
176
- func parseOptions (opts ... * Options ) (* Options , error ) {
178
+ func parseOptions (opts ... * Option ) (* Option , error ) {
177
179
// if opts is nil or pass nil as parameter
178
180
if len (opts ) == 0 || opts [0 ] == nil {
179
- return defaultOptions , nil
181
+ return DefaultOption , nil
180
182
}
181
183
if len (opts ) != 1 {
182
184
return nil , errors .New ("number of options is more than 1" )
183
185
}
184
186
opt := opts [0 ]
185
- opt .MagicNumber = defaultOptions .MagicNumber
187
+ opt .MagicNumber = DefaultOption .MagicNumber
186
188
if opt .CodecType == "" {
187
- opt .CodecType = defaultOptions .CodecType
189
+ opt .CodecType = DefaultOption .CodecType
188
190
}
189
191
return opt , nil
190
192
}
191
193
192
- func NewClient (conn io.ReadWriteCloser , opts ... * Options ) (* Client , error ) {
193
- opt , err := parseOptions (opts ... )
194
- if err != nil {
195
- return nil , err
196
- }
194
+ func NewClient (conn net.Conn , opt * Option ) (* Client , error ) {
197
195
f := codec .NewCodecFuncMap [opt .CodecType ]
198
196
if f == nil {
199
- err = fmt .Errorf ("invalid codec type %s" , opt .CodecType )
197
+ err : = fmt .Errorf ("invalid codec type %s" , opt .CodecType )
200
198
log .Println ("rpc client: codec error:" , err )
201
199
return nil , err
202
200
}
203
201
// send options with server
204
- if err = json .NewEncoder (conn ).Encode (opt ); err != nil {
202
+ if err : = json .NewEncoder (conn ).Encode (opt ); err != nil {
205
203
log .Println ("rpc client: options error: " , err )
206
204
_ = conn .Close ()
207
205
return nil , err
208
206
}
209
207
return newClientCodec (f (conn ), opt ), nil
210
208
}
211
209
212
- func newClientCodec (cc codec.Codec , opt * Options ) * Client {
210
+ func newClientCodec (cc codec.Codec , opt * Option ) * Client {
213
211
client := & Client {
212
+ seq : 1 , // seq starts with 1, 0 means invalid call
214
213
cc : cc ,
215
214
opt : opt ,
216
215
pending : make (map [uint64 ]* Call ),
@@ -219,11 +218,19 @@ func newClientCodec(cc codec.Codec, opt *Options) *Client {
219
218
return client
220
219
}
221
220
222
- // Dial connects to an RPC server at the specified network address
223
- func Dial (network , address string , opts ... * Options ) (* Client , error ) {
221
+ func dial (network , address string , opt * Option ) (* Client , error ) {
224
222
conn , err := net .Dial (network , address )
225
223
if err != nil {
226
224
return nil , err
227
225
}
228
- return NewClient (conn , opts ... )
226
+ return NewClient (conn , opt )
227
+ }
228
+
229
+ // Dial connects to an RPC server at the specified network address
230
+ func Dial (network , address string , opts ... * Option ) (* Client , error ) {
231
+ opt , err := parseOptions (opts ... )
232
+ if err != nil {
233
+ return nil , err
234
+ }
235
+ return dial (network , address , opt )
229
236
}
0 commit comments