*
This commit is contained in:
parent
4990c6d26a
commit
9830065f5c
4 changed files with 50 additions and 21 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,2 +1,4 @@
|
|||
/nanite
|
||||
/exp/
|
||||
/.envrc
|
||||
/result
|
||||
|
|
|
|||
6
event.go
6
event.go
|
|
@ -96,8 +96,10 @@ func (_ HangupEvent) HandleOutgoing(app *App) error {
|
|||
app.conn.stop()
|
||||
app.conn.Write([]byte("QUIT\n"))
|
||||
app.conn.Close()
|
||||
app.conn.ticker.Stop()
|
||||
app.conn.ticker = nil
|
||||
if app.conn.ticker != nil {
|
||||
app.conn.ticker.Stop()
|
||||
app.conn.ticker = nil
|
||||
}
|
||||
app.conn = nil
|
||||
app.incoming <- HangupEvent{host, port}
|
||||
|
||||
|
|
|
|||
55
main.go
55
main.go
|
|
@ -102,6 +102,41 @@ func (app *App) EnsureConfigDir() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (app *App) Error(err error) {
|
||||
if app.conn != nil {
|
||||
host := app.conn.host
|
||||
port := app.conn.port
|
||||
app.conn.stop()
|
||||
app.conn.Close()
|
||||
if app.conn.ticker != nil {
|
||||
app.conn.ticker.Stop()
|
||||
}
|
||||
app.conn = nil
|
||||
app.incoming <- SystemMessageEvent(fmt.Sprintf("disconnected from %s:%s: %v", host, port, err))
|
||||
} else {
|
||||
app.incoming <- SystemMessageEvent(fmt.Sprintf("error: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
func (app *App) Run() {
|
||||
for {
|
||||
select {
|
||||
case ev := <-app.outgoing:
|
||||
_, dial := ev.(DialEvent)
|
||||
if dial || app.conn != nil {
|
||||
if err := ev.HandleOutgoing(app); err != nil {
|
||||
app.Error(err)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
app.incoming <- SystemMessageEvent("not connected to any server")
|
||||
}
|
||||
case <-app.ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewApp() *App {
|
||||
app := &App{}
|
||||
app.ctx, app.stop = context.WithCancel(context.Background())
|
||||
|
|
@ -117,25 +152,7 @@ func NewApp() *App {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case ev := <-app.outgoing:
|
||||
_, dial := ev.(DialEvent)
|
||||
if dial || app.conn != nil {
|
||||
if err := ev.HandleOutgoing(app); err != nil {
|
||||
app.error <- err
|
||||
return
|
||||
}
|
||||
} else {
|
||||
app.incoming <- SystemMessageEvent("not connected to any server")
|
||||
}
|
||||
case <-app.ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go app.Run()
|
||||
app.InitUI()
|
||||
|
||||
return app
|
||||
|
|
|
|||
8
shell.nix
Normal file
8
shell.nix
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{ pkgs ? import <nixpkgs> {} }:
|
||||
|
||||
pkgs.mkShell {
|
||||
buildInputs = [
|
||||
pkgs.go
|
||||
pkgs.gopls
|
||||
];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue