@@ -6,10 +6,10 @@ import (
66 "errors"
77 "flag"
88 "fmt"
9+ "net"
910 "os"
1011
1112 "dh-gobra/initiator"
12- "dh-gobra/iolib"
1313)
1414
1515type Config struct {
@@ -36,6 +36,8 @@ func parseArgs() Config {
3636 return config
3737}
3838
39+ const MAX_DATA_SIZE = 1024
40+
3941func main () {
4042 // parse args
4143 config := parseArgs ()
@@ -52,7 +54,7 @@ func main() {
5254 reportAndExit (err )
5355 }
5456
55- iolib , err := iolib . NewLibState ( config .PeerEndpoint )
57+ conn , err := net . Dial ( "udp" , config .PeerEndpoint )
5658 if err != nil {
5759 reportAndExit (err )
5860 }
@@ -61,29 +63,23 @@ func main() {
6163 if err != nil {
6264 reportAndExit (err )
6365 }
64-
65- err = iolib .Send (hsMsg1 )
66- if err != nil {
66+ if _ , err := conn .Write (hsMsg1 ); err != nil {
6767 reportAndExit (err )
6868 }
6969
70- hsMsg2 , err := iolib . Recv ( )
71- if err != nil {
70+ hsMsg2 := make ([] byte , MAX_DATA_SIZE )
71+ if _ , err := conn . Read ( hsMsg2 ); err != nil {
7272 reportAndExit (err )
7373 }
74-
75- err = initor .ProcessHsMsg2 (hsMsg2 )
76- if err != nil {
74+ if err := initor .ProcessHsMsg2 (hsMsg2 ); err != nil {
7775 reportAndExit (err )
7876 }
7977
8078 hsMsg3 , err := initor .ProduceHsMsg3 ()
8179 if err != nil {
8280 reportAndExit (err )
8381 }
84-
85- err = iolib .Send (hsMsg3 )
86- if err != nil {
82+ if _ , err := conn .Write (hsMsg3 ); err != nil {
8783 reportAndExit (err )
8884 }
8985
@@ -96,13 +92,12 @@ func main() {
9692 if err != nil {
9793 reportAndExit (err )
9894 }
99- err = iolib .Send (requestMsg )
100- if err != nil {
95+ if _ , err := conn .Write (requestMsg ); err != nil {
10196 reportAndExit (err )
10297 }
10398
104- responseMsg , err := iolib . Recv ( )
105- if err != nil {
99+ responseMsg := make ([] byte , MAX_DATA_SIZE )
100+ if _ , err := conn . Read ( responseMsg ); err != nil {
106101 reportAndExit (err )
107102 }
108103 responsePayload , err := initor .ProcessTransportMsg (responseMsg )
@@ -113,10 +108,7 @@ func main() {
113108 fmt .Println ("Enter a payload to be sent:" )
114109 }
115110
116- iolib .Close ()
117- if err == nil {
118- os .Exit (0 )
119- } else {
111+ if err := conn .Close (); err != nil {
120112 reportAndExit (err )
121113 }
122114}
0 commit comments