Switch to using goroutines rather than a select()

More complex code since now we have to synchronize between threads,
but a good way to teach myself more about goroutines.
This commit is contained in:
Kovid Goyal
2022-08-26 08:37:14 +05:30
parent ee12349a50
commit 4a49c3940a
5 changed files with 418 additions and 387 deletions

View File

@@ -70,9 +70,9 @@ func wrap_in_escape_code(data []byte) []byte {
const prefix = "\x1bP@kitty-cmd"
const suffix = "\x1b\\"
ans := make([]byte, len(prefix)+len(data)+len(suffix))
n := copy(ans, []byte(prefix))
n := copy(ans, prefix)
n += copy(ans[n:], data)
copy(ans[n:], []byte(suffix))
copy(ans[n:], suffix)
return ans
}

View File

@@ -17,9 +17,10 @@ func do_chunked_io(io_data *rc_io_data) (serialized_response []byte, err error)
}
var last_received_data_at time.Time
var check_for_timeout func(loop *tui.Loop, timer_id tui.TimerId) error
var final_write_id tui.IdType
var check_for_timeout func(loop *tui.Loop, timer_id tui.IdType) error
check_for_timeout = func(loop *tui.Loop, timer_id tui.TimerId) error {
check_for_timeout = func(loop *tui.Loop, timer_id tui.IdType) error {
time_since_last_received_data := time.Now().Sub(last_received_data_at)
if time_since_last_received_data >= io_data.timeout {
return os.ErrDeadlineExceeded
@@ -46,23 +47,25 @@ func do_chunked_io(io_data *rc_io_data) (serialized_response []byte, err error)
if err != nil {
return "", err
}
write_id := loop.QueueWriteBytesDangerous(chunk)
if len(chunk) == 0 {
transition_to_read()
} else {
loop.QueueWriteBytes(chunk)
final_write_id = write_id
}
return "", nil
}
loop.OnWriteComplete = func(loop *tui.Loop) error {
loop.OnWriteComplete = func(loop *tui.Loop, completed_write_id tui.IdType) error {
if completed_write_id == final_write_id {
transition_to_read()
return nil
}
chunk, err := io_data.next_chunk(true)
if err != nil {
return err
}
write_id := loop.QueueWriteBytesDangerous(chunk)
if len(chunk) == 0 {
transition_to_read()
} else {
loop.QueueWriteBytes(chunk)
final_write_id = write_id
}
return nil
}