@@ -57,91 +57,85 @@ func (s *ArduinoCoreServerImpl) Init(ctx context.Context, req *rpc.InitReq) (*rp
57
57
}
58
58
59
59
func (s * ArduinoCoreServerImpl ) Compile (req * rpc.CompileReq , stream rpc.ArduinoCore_CompileServer ) error {
60
- r , w := io .Pipe ()
61
- go func () {
62
- data := make ([]byte , 1024 )
63
- for {
64
- if n , err := r .Read (data ); err != nil {
65
- return
66
- } else {
67
- stream .Send (& rpc.CompileResp {Output : data [:n ]})
68
- }
69
- }
70
- }()
71
- resp , err := compile .Compile (stream .Context (), req , w , func (taskProgress * rpc.TaskProgress ) {
72
- stream .Send (& rpc.CompileResp {TaskProgress : taskProgress })
73
- }, func (downloadProgress * rpc.DownloadProgress ) {
74
- stream .Send (& rpc.CompileResp {DownloadProgress : downloadProgress })
75
- })
60
+ resp , err := compile .Compile (
61
+ stream .Context (), req ,
62
+ feedStream (func (data []byte ) { stream .Send (& rpc.CompileResp {Output : data }) }),
63
+ func (p * rpc.TaskProgress ) { stream .Send (& rpc.CompileResp {TaskProgress : p }) },
64
+ func (p * rpc.DownloadProgress ) { stream .Send (& rpc.CompileResp {DownloadProgress : p }) },
65
+ )
76
66
stream .Send (resp )
77
67
return err
78
68
}
79
69
80
70
func (s * ArduinoCoreServerImpl ) PlatformInstall (req * rpc.PlatformInstallReq , stream rpc.ArduinoCore_PlatformInstallServer ) error {
81
- resp , err := core .PlatformInstall (stream . Context (), req , func ( progress * rpc. DownloadProgress ) {
82
- stream .Send ( & rpc. PlatformInstallResp { Progress : progress })
83
- }, func (taskProgress * rpc.TaskProgress ) {
84
- stream .Send (& rpc.PlatformInstallResp {TaskProgress : taskProgress })
85
- } )
71
+ resp , err := core .PlatformInstall (
72
+ stream .Context (), req ,
73
+ func (p * rpc.DownloadProgress ) { stream . Send ( & rpc. PlatformInstallResp { Progress : p }) },
74
+ func ( p * rpc. TaskProgress ) { stream .Send (& rpc.PlatformInstallResp {TaskProgress : p }) },
75
+ )
86
76
if err != nil {
87
77
return err
88
78
}
89
79
return stream .Send (resp )
90
80
}
91
81
92
82
func (s * ArduinoCoreServerImpl ) PlatformDownload (req * rpc.PlatformDownloadReq , stream rpc.ArduinoCore_PlatformDownloadServer ) error {
93
- resp , err := core .PlatformDownload (stream .Context (), req , func (progress * rpc.DownloadProgress ) {
94
- stream .Send (& rpc.PlatformDownloadResp {Progress : progress })
95
- })
83
+ resp , err := core .PlatformDownload (
84
+ stream .Context (), req ,
85
+ func (p * rpc.DownloadProgress ) { stream .Send (& rpc.PlatformDownloadResp {Progress : p }) },
86
+ )
96
87
if err != nil {
97
88
return err
98
89
}
99
90
return stream .Send (resp )
100
91
}
101
92
102
93
func (s * ArduinoCoreServerImpl ) PlatformUninstall (req * rpc.PlatformUninstallReq , stream rpc.ArduinoCore_PlatformUninstallServer ) error {
103
- resp , err := core .PlatformUninstall (stream .Context (), req , func (taskProgress * rpc.TaskProgress ) {
104
- stream .Send (& rpc.PlatformUninstallResp {TaskProgress : taskProgress })
105
- })
94
+ resp , err := core .PlatformUninstall (
95
+ stream .Context (), req ,
96
+ func (p * rpc.TaskProgress ) { stream .Send (& rpc.PlatformUninstallResp {TaskProgress : p }) },
97
+ )
106
98
if err != nil {
107
99
return err
108
100
}
109
101
return stream .Send (resp )
110
102
}
111
103
112
104
func (s * ArduinoCoreServerImpl ) PlatformUpgrade (req * rpc.PlatformUpgradeReq , stream rpc.ArduinoCore_PlatformUpgradeServer ) error {
113
- resp , err := core .PlatformUpgrade (stream . Context (), req , func ( progress * rpc. DownloadProgress ) {
114
- stream .Send ( & rpc. PlatformUpgradeResp { Progress : progress })
115
- }, func (taskProgress * rpc.TaskProgress ) {
116
- stream .Send (& rpc.PlatformUpgradeResp {TaskProgress : taskProgress })
117
- } )
105
+ resp , err := core .PlatformUpgrade (
106
+ stream .Context (), req ,
107
+ func (p * rpc.DownloadProgress ) { stream . Send ( & rpc. PlatformUpgradeResp { Progress : p }) },
108
+ func ( p * rpc. TaskProgress ) { stream .Send (& rpc.PlatformUpgradeResp {TaskProgress : p }) },
109
+ )
118
110
if err != nil {
119
111
return err
120
112
}
121
113
return stream .Send (resp )
122
114
}
123
115
124
116
func (s * ArduinoCoreServerImpl ) Upload (req * rpc.UploadReq , stream rpc.ArduinoCore_UploadServer ) error {
125
- r , w := io . Pipe ()
126
- r2 , w2 := io . Pipe ()
127
-
128
- feedStream (r , func (data []byte ) { stream .Send (& rpc.UploadResp {OutStream : data }) })
129
- feedStream ( r2 , func ( data [] byte ) { stream . Send ( & rpc. UploadResp { ErrStream : data }) } )
130
-
131
- resp , err := upload . Upload ( stream . Context (), req , w , w2 )
132
- stream . Send ( resp )
133
- return err
117
+ resp , err := upload . Upload (
118
+ stream . Context (), req ,
119
+ feedStream ( func ( data [] byte ) { stream . Send ( & rpc. UploadResp { OutStream : data }) }),
120
+ feedStream (func (data []byte ) { stream .Send (& rpc.UploadResp {ErrStream : data }) }),
121
+ )
122
+ if err != nil {
123
+ return err
124
+ }
125
+ return stream . Send ( resp )
134
126
}
135
127
136
- func feedStream (out io.Reader , streamer func (data []byte )) {
128
+ func feedStream (streamer func (data []byte )) io.Writer {
129
+ r , w := io .Pipe ()
137
130
go func () {
138
131
data := make ([]byte , 1024 )
139
132
for {
140
- if n , err := out .Read (data ); err != nil {
133
+ if n , err := r .Read (data ); err != nil {
141
134
return
142
135
} else {
143
136
streamer (data [:n ])
144
137
}
145
138
}
146
139
}()
140
+ return w
147
141
}
0 commit comments