If the timeout is set to NoTimeout, make Read wait forever.
Otherwise the maximul allowed timeout is 0x7FFFFFFF milliseconds, equivalent to about 24.85 days. This patch will make a transparent repeated read in case this long timeout should ever happen.
This commit is contained in:
@@ -30,6 +30,7 @@ import (
|
||||
type windowsPort struct {
|
||||
mu sync.Mutex
|
||||
handle windows.Handle
|
||||
hasTimeout bool
|
||||
}
|
||||
|
||||
func nativeGetPortsList() ([]string, error) {
|
||||
@@ -72,6 +73,7 @@ func (port *windowsPort) Read(p []byte) (int, error) {
|
||||
}
|
||||
defer windows.CloseHandle(ev.HEvent)
|
||||
|
||||
for {
|
||||
err = windows.ReadFile(port.handle, p, &readed, ev)
|
||||
if err == windows.ERROR_IO_PENDING {
|
||||
err = windows.GetOverlappedResult(port.handle, ev, &readed, true)
|
||||
@@ -91,8 +93,14 @@ func (port *windowsPort) Read(p []byte) (int, error) {
|
||||
}
|
||||
|
||||
// Timeout
|
||||
port.mu.Lock()
|
||||
hasTimeout := port.hasTimeout
|
||||
port.mu.Unlock()
|
||||
if hasTimeout {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (port *windowsPort) Write(p []byte) (int, error) {
|
||||
var writed uint32
|
||||
@@ -304,9 +312,12 @@ func (port *windowsPort) SetReadTimeout(timeout time.Duration) error {
|
||||
commTimeouts.ReadTotalTimeoutConstant = uint32(ms)
|
||||
}
|
||||
|
||||
port.mu.Lock()
|
||||
defer port.mu.Unlock()
|
||||
if err := windows.SetCommTimeouts(port.handle, commTimeouts); err != nil {
|
||||
return &PortError{code: InvalidTimeoutValue, causedBy: err}
|
||||
}
|
||||
port.hasTimeout = (timeout != NoTimeout)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user