golanggohlsrtmpwebrtcmedia-serverobs-studiortcprtmp-proxyrtmp-serverrtprtsprtsp-proxyrtsp-relayrtsp-serversrtstreamingwebrtc-proxy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1010 B
77 lines
1010 B
package externalcmd |
|
|
|
import ( |
|
"time" |
|
) |
|
|
|
const ( |
|
retryPause = 5 * time.Second |
|
) |
|
|
|
// Environment is a Cmd environment. |
|
type Environment struct { |
|
Path string |
|
Port string |
|
} |
|
|
|
// Cmd is an external command. |
|
type Cmd struct { |
|
cmdstr string |
|
restart bool |
|
env Environment |
|
|
|
// in |
|
terminate chan struct{} |
|
|
|
// out |
|
done chan struct{} |
|
} |
|
|
|
// New allocates an Cmd. |
|
func New(cmdstr string, restart bool, env Environment) *Cmd { |
|
e := &Cmd{ |
|
cmdstr: cmdstr, |
|
restart: restart, |
|
env: env, |
|
terminate: make(chan struct{}), |
|
done: make(chan struct{}), |
|
} |
|
|
|
go e.run() |
|
|
|
return e |
|
} |
|
|
|
// Close closes an Cmd. |
|
func (e *Cmd) Close() { |
|
close(e.terminate) |
|
<-e.done |
|
} |
|
|
|
func (e *Cmd) run() { |
|
defer close(e.done) |
|
|
|
for { |
|
ok := func() bool { |
|
ok := e.runInner() |
|
if !ok { |
|
return false |
|
} |
|
|
|
if !e.restart { |
|
<-e.terminate |
|
return false |
|
} |
|
|
|
select { |
|
case <-time.After(retryPause): |
|
return true |
|
case <-e.terminate: |
|
return false |
|
} |
|
}() |
|
if !ok { |
|
break |
|
} |
|
} |
|
}
|
|
|