mirror of
https://github.com/lilydjwg/fcitx.vim.git
synced 2025-12-30 13:25:00 +08:00
add timeout for socket operations
This commit is contained in:
parent
c77e732eac
commit
cf7e2ab555
2 changed files with 6 additions and 3 deletions
|
|
@ -4,19 +4,22 @@ import os
|
|||
import vim
|
||||
import socket
|
||||
import struct
|
||||
|
||||
FCITX_STATUS = struct.pack('i', 0)
|
||||
FCITX_OPEN = struct.pack('i', 1 | (1 << 16))
|
||||
FCITX_CLOSE = struct.pack('i', 1)
|
||||
INT_SIZE = struct.calcsize('i')
|
||||
fcitxsocketfile = vim.eval('s:fcitxsocketfile')
|
||||
|
||||
if fcitxsocketfile[0] == '@': # abstract socket
|
||||
fcitxsocketfile = '\x00' + fcitxsocketfile[1:]
|
||||
|
||||
def fcitxtalk(command=None):
|
||||
sock = socket.socket(socket.AF_UNIX)
|
||||
sock.settimeout(0.5)
|
||||
try:
|
||||
sock.connect(fcitxsocketfile)
|
||||
except socket.error:
|
||||
except (socket.error, socket.timeout):
|
||||
vim.command('echohl WarningMsg | echo "fcitx.vim: socket connection error" | echohl NONE')
|
||||
return
|
||||
try:
|
||||
|
|
@ -29,7 +32,7 @@ def fcitxtalk(command=None):
|
|||
sock.send(FCITX_OPEN)
|
||||
else:
|
||||
raise ValueError('unknown fcitx command')
|
||||
except struct.error:
|
||||
except (struct.error, socket.timeout):
|
||||
# if there's a proxy of some kind, connect and send *will* succeed when
|
||||
# fcitx isn't there.
|
||||
vim.command('echohl WarningMsg | echo "fcitx.vim: socket error" | echohl NONE')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue