add timeout for socket operations

This commit is contained in:
lilydjwg 2013-11-14 11:49:24 +08:00
parent c77e732eac
commit cf7e2ab555
2 changed files with 6 additions and 3 deletions

View file

@ -4,19 +4,22 @@ import os
import vim import vim
import socket import socket
import struct import struct
FCITX_STATUS = struct.pack('i', 0) FCITX_STATUS = struct.pack('i', 0)
FCITX_OPEN = struct.pack('i', 1 | (1 << 16)) FCITX_OPEN = struct.pack('i', 1 | (1 << 16))
FCITX_CLOSE = struct.pack('i', 1) FCITX_CLOSE = struct.pack('i', 1)
INT_SIZE = struct.calcsize('i') INT_SIZE = struct.calcsize('i')
fcitxsocketfile = vim.eval('s:fcitxsocketfile') fcitxsocketfile = vim.eval('s:fcitxsocketfile')
if fcitxsocketfile[0] == '@': # abstract socket if fcitxsocketfile[0] == '@': # abstract socket
fcitxsocketfile = '\x00' + fcitxsocketfile[1:] fcitxsocketfile = '\x00' + fcitxsocketfile[1:]
def fcitxtalk(command=None): def fcitxtalk(command=None):
sock = socket.socket(socket.AF_UNIX) sock = socket.socket(socket.AF_UNIX)
sock.settimeout(0.5)
try: try:
sock.connect(fcitxsocketfile) sock.connect(fcitxsocketfile)
except socket.error: except (socket.error, socket.timeout):
vim.command('echohl WarningMsg | echo "fcitx.vim: socket connection error" | echohl NONE') vim.command('echohl WarningMsg | echo "fcitx.vim: socket connection error" | echohl NONE')
return return
try: try:
@ -29,7 +32,7 @@ def fcitxtalk(command=None):
sock.send(FCITX_OPEN) sock.send(FCITX_OPEN)
else: else:
raise ValueError('unknown fcitx command') 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 # if there's a proxy of some kind, connect and send *will* succeed when
# fcitx isn't there. # fcitx isn't there.
vim.command('echohl WarningMsg | echo "fcitx.vim: socket error" | echohl NONE') vim.command('echohl WarningMsg | echo "fcitx.vim: socket error" | echohl NONE')

View file

@ -1,7 +1,7 @@
scriptencoding utf-8 scriptencoding utf-8
" fcitx.vim remember fcitx's input state for each buffer " fcitx.vim remember fcitx's input state for each buffer
" Author: lilydjwg " Author: lilydjwg
" Version: 1.2.4 " Version: 1.2.5
" URL: http://www.vim.org/scripts/script.php?script_id=3764 " URL: http://www.vim.org/scripts/script.php?script_id=3764
" --------------------------------------------------------------------- " ---------------------------------------------------------------------
" Load Once: " Load Once: