@@ -64,7 +64,7 @@ type Options struct {
64
64
// New creates a new Astilectron instance
65
65
func New (o Options ) (a * Astilectron , err error ) {
66
66
// Validate the OS
67
- if err = validateOS (); err != nil {
67
+ if err = validateOS (runtime . GOOS ); err != nil {
68
68
err = errors .Wrap (err , "validating OS failed" )
69
69
return
70
70
}
@@ -79,7 +79,7 @@ func New(o Options) (a *Astilectron, err error) {
79
79
}
80
80
81
81
// Set paths
82
- if a .paths , err = newPaths (o ); err != nil {
82
+ if a .paths , err = newPaths (runtime . GOOS , o ); err != nil {
83
83
err = errors .Wrap (err , "creating new paths failed" )
84
84
return
85
85
}
@@ -105,9 +105,9 @@ func New(o Options) (a *Astilectron, err error) {
105
105
}
106
106
107
107
// validateOS validates the OS
108
- func validateOS () error {
109
- if ! astislice .InStringSlice (runtime . GOOS , validOSes ) {
110
- return fmt .Errorf ("OS %s is not supported" , runtime . GOOS )
108
+ func validateOS (os string ) error {
109
+ if ! astislice .InStringSlice (os , validOSes ) {
110
+ return fmt .Errorf ("OS %s is not supported" , os )
111
111
}
112
112
return nil
113
113
}
@@ -159,7 +159,7 @@ func (a *Astilectron) provision() error {
159
159
160
160
// Provision
161
161
var ctx , _ = a .canceller .NewContext ()
162
- return a .provisioner .Provision (ctx , a .options .AppName , * a .paths )
162
+ return a .provisioner .Provision (ctx , a .options .AppName , runtime . GOOS , * a .paths )
163
163
}
164
164
165
165
// listenTCP listens to the first TCP connection coming its way (this should be Astilectron)
@@ -172,58 +172,63 @@ func (a *Astilectron) listenTCP() (err error) {
172
172
return errors .Wrap (err , "tcp net.Listen failed" )
173
173
}
174
174
175
- // Accept
175
+ // Check a connection has been accepted quickly enough
176
176
var chanAccepted = make (chan bool )
177
- go func () {
178
- for i := 0 ; i <= 1 ; i ++ {
179
- // Accept
180
- var conn net.Conn
181
- var err error
182
- if conn , err = a .listener .Accept (); err != nil {
183
- astilog .Errorf ("%s while TCP accepting" , err )
184
- a .dispatcher .dispatch (Event {Name : EventNameAppErrorAccept , TargetID : mainTargetID })
185
- a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
186
- return
187
- }
188
-
189
- // We only accept the first connection which should be Astilectron, close the next one and stop
190
- // the app
191
- if i > 0 {
192
- astilog .Errorf ("Too many TCP connections" )
193
- a .dispatcher .dispatch (Event {Name : EventNameAppTooManyAccept , TargetID : mainTargetID })
194
- a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
195
- conn .Close ()
196
- return
197
- }
198
-
199
- // Let the timer know a connection has been accepted
200
- chanAccepted <- true
201
-
202
- // Create reader and writer
203
- a .writer = newWriter (conn )
204
- a .reader = newReader (a .dispatcher , conn )
205
- go a .reader .read ()
177
+ go a .watchNoAccept (30 * time .Second , chanAccepted )
178
+
179
+ // Accept connections
180
+ go a .acceptTCP (chanAccepted )
181
+ return
182
+ }
183
+
184
+ // watchNoAccept checks whether a TCP connection is accepted quickly enough
185
+ func (a * Astilectron ) watchNoAccept (timeout time.Duration , chanAccepted chan bool ) {
186
+ var t = time .NewTimer (timeout )
187
+ defer t .Stop ()
188
+ for {
189
+ select {
190
+ case <- chanAccepted :
191
+ return
192
+ case <- t .C :
193
+ astilog .Errorf ("No TCP connection has been accepted in the past %s" , timeout )
194
+ a .dispatcher .dispatch (Event {Name : EventNameAppNoAccept , TargetID : mainTargetID })
195
+ a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
196
+ return
206
197
}
207
- }()
198
+ }
199
+ }
208
200
209
- // We check a connection has been accepted
210
- go func () {
211
- const timeout = 30 * time .Second
212
- var t = time .NewTimer (timeout )
213
- defer t .Stop ()
214
- for {
215
- select {
216
- case <- chanAccepted :
217
- return
218
- case <- t .C :
219
- astilog .Errorf ("No TCP connection has been accepted in the past %s" , timeout )
220
- a .dispatcher .dispatch (Event {Name : EventNameAppNoAccept , TargetID : mainTargetID })
221
- a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
222
- return
223
- }
201
+ // watchAcceptTCP accepts TCP connections
202
+ func (a * Astilectron ) acceptTCP (chanAccepted chan bool ) {
203
+ for i := 0 ; i <= 1 ; i ++ {
204
+ // Accept
205
+ var conn net.Conn
206
+ var err error
207
+ if conn , err = a .listener .Accept (); err != nil {
208
+ astilog .Errorf ("%s while TCP accepting" , err )
209
+ a .dispatcher .dispatch (Event {Name : EventNameAppErrorAccept , TargetID : mainTargetID })
210
+ a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
211
+ return
224
212
}
225
- }()
226
- return
213
+
214
+ // We only accept the first connection which should be Astilectron, close the next one and stop
215
+ // the app
216
+ if i > 0 {
217
+ astilog .Errorf ("Too many TCP connections" )
218
+ a .dispatcher .dispatch (Event {Name : EventNameAppTooManyAccept , TargetID : mainTargetID })
219
+ a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
220
+ conn .Close ()
221
+ return
222
+ }
223
+
224
+ // Let the timer know a connection has been accepted
225
+ chanAccepted <- true
226
+
227
+ // Create reader and writer
228
+ a .writer = newWriter (conn )
229
+ a .reader = newReader (a .dispatcher , conn )
230
+ go a .reader .read ()
231
+ }
227
232
}
228
233
229
234
// execute executes Astilectron in Electron
@@ -239,7 +244,15 @@ func (a *Astilectron) execute() (err error) {
239
244
cmd .Stderr = a .stderrWriter
240
245
cmd .Stdout = a .stdoutWriter
241
246
242
- // Start command
247
+ // Execute command
248
+ if err = a .executeCmd (cmd ); err != nil {
249
+ return errors .Wrap (err , "executing cmd failed" )
250
+ }
251
+ return
252
+ }
253
+
254
+ // executeCmd executes the command
255
+ func (a * Astilectron ) executeCmd (cmd * exec.Cmd ) (err error ) {
243
256
var e = synchronousFunc (a .canceller , a , func () {
244
257
// Start command
245
258
astilog .Debugf ("Starting cmd %s" , strings .Join (cmd .Args , " " ))
@@ -249,20 +262,7 @@ func (a *Astilectron) execute() (err error) {
249
262
}
250
263
251
264
// Watch command
252
- go func () {
253
- // Wait
254
- cmd .Wait ()
255
-
256
- // Check the canceller to check whether it was a crash
257
- if ! a .canceller .Cancelled () {
258
- astilog .Debug ("App has crashed" )
259
- a .dispatcher .dispatch (Event {Name : EventNameAppCrash , TargetID : mainTargetID })
260
- } else {
261
- astilog .Debug ("App has closed" )
262
- a .dispatcher .dispatch (Event {Name : EventNameAppClose , TargetID : mainTargetID })
263
- }
264
- a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
265
- }()
265
+ go a .watchCmd (cmd )
266
266
}, EventNameAppEventReady )
267
267
268
268
// Update display pool
@@ -272,6 +272,22 @@ func (a *Astilectron) execute() (err error) {
272
272
return
273
273
}
274
274
275
+ // watchCmd watches the cmd execution
276
+ func (a * Astilectron ) watchCmd (cmd * exec.Cmd ) {
277
+ // Wait
278
+ cmd .Wait ()
279
+
280
+ // Check the canceller to check whether it was a crash
281
+ if ! a .canceller .Cancelled () {
282
+ astilog .Debug ("App has crashed" )
283
+ a .dispatcher .dispatch (Event {Name : EventNameAppCrash , TargetID : mainTargetID })
284
+ } else {
285
+ astilog .Debug ("App has closed" )
286
+ a .dispatcher .dispatch (Event {Name : EventNameAppClose , TargetID : mainTargetID })
287
+ }
288
+ a .dispatcher .dispatch (Event {Name : EventNameAppCmdStop , TargetID : mainTargetID })
289
+ }
290
+
275
291
// Displays returns the displays
276
292
func (a * Astilectron ) Displays () []* Display {
277
293
return a .displayPool .all ()
@@ -287,7 +303,9 @@ func (a *Astilectron) Close() {
287
303
astilog .Debug ("Closing..." )
288
304
a .canceller .Cancel ()
289
305
a .dispatcher .close ()
290
- a .listener .Close ()
306
+ if a .listener != nil {
307
+ a .listener .Close ()
308
+ }
291
309
if a .reader != nil {
292
310
a .reader .close ()
293
311
}
0 commit comments