[client] fix improper spice socket shutdown

This commit is contained in:
Geoffrey McRae 2018-01-29 17:27:12 +11:00
parent dc6932a9ba
commit 78a100135b
3 changed files with 53 additions and 23 deletions

View File

@ -49,6 +49,7 @@ struct AppState
{ {
bool running; bool running;
bool started; bool started;
bool keyDown[SDL_NUM_SCANCODES];
TTF_Font * font; TTF_Font * font;
SDL_Point srcSize; SDL_Point srcSize;
@ -404,7 +405,6 @@ int spiceThread(void * arg)
break; break;
} }
spice_disconnect();
state.running = false; state.running = false;
return 0; return 0;
} }
@ -420,18 +420,10 @@ static inline const uint32_t mapScancode(SDL_Scancode scancode)
return ps2; return ps2;
} }
struct eventState
{
bool serverMode;
bool realignGuest;
bool keyDown[SDL_NUM_SCANCODES];
};
int eventFilter(void * userdata, SDL_Event * event) int eventFilter(void * userdata, SDL_Event * event)
{ {
static bool serverMode = false; static bool serverMode = false;
static bool realignGuest = true; static bool realignGuest = true;
static bool keyDown[SDL_NUM_SCANCODES] = {false};
if (event->type == SDL_WINDOWEVENT) if (event->type == SDL_WINDOWEVENT)
{ {
@ -530,13 +522,16 @@ int eventFilter(void * userdata, SDL_Event * event)
if (scancode == 0) if (scancode == 0)
break; break;
if (!state.keyDown[sc])
{
if (spice_key_down(scancode)) if (spice_key_down(scancode))
keyDown[sc] = true; state.keyDown[sc] = true;
else else
{ {
DEBUG_ERROR("SDL_KEYDOWN: failed to send message"); DEBUG_ERROR("SDL_KEYDOWN: failed to send message");
break; break;
} }
}
break; break;
} }
@ -547,7 +542,7 @@ int eventFilter(void * userdata, SDL_Event * event)
break; break;
// avoid sending key up events when we didn't send a down // avoid sending key up events when we didn't send a down
if (!keyDown[sc]) if (!state.keyDown[sc])
break; break;
uint32_t scancode = mapScancode(sc); uint32_t scancode = mapScancode(sc);
@ -555,7 +550,7 @@ int eventFilter(void * userdata, SDL_Event * event)
break; break;
if (spice_key_up(scancode)) if (spice_key_up(scancode))
keyDown[sc] = false; state.keyDown[sc] = false;
else else
{ {
DEBUG_ERROR("SDL_KEYUP: failed to send message"); DEBUG_ERROR("SDL_KEYUP: failed to send message");
@ -840,6 +835,8 @@ int run()
SDL_Thread *t_spice = NULL; SDL_Thread *t_spice = NULL;
SDL_Thread *t_main = NULL; SDL_Thread *t_main = NULL;
SDL_Thread *t_frame = NULL;
SDL_Thread *t_render = NULL;
while(1) while(1)
{ {
@ -903,13 +900,13 @@ int run()
break; break;
} }
if (!(t_main = SDL_CreateThread(frameThread, "frameThread", NULL))) if (!(t_frame = SDL_CreateThread(frameThread, "frameThread", NULL)))
{ {
DEBUG_ERROR("frame create thread failed"); DEBUG_ERROR("frame create thread failed");
break; break;
} }
if (!(t_main = SDL_CreateThread(renderThread, "renderThread", NULL))) if (!(t_render = SDL_CreateThread(renderThread, "renderThread", NULL)))
{ {
DEBUG_ERROR("render create thread failed"); DEBUG_ERROR("render create thread failed");
break; break;
@ -939,12 +936,34 @@ int run()
state.running = false; state.running = false;
if (t_render)
SDL_WaitThread(t_render, NULL);
if (t_frame)
SDL_WaitThread(t_frame, NULL);
if (t_main) if (t_main)
SDL_WaitThread(t_main, NULL); SDL_WaitThread(t_main, NULL);
// if spice is still connected send key up events for any pressed keys
if (params.useSpice && spice_ready())
{
for(int i = 0; i < SDL_NUM_SCANCODES; ++i)
if (state.keyDown[i])
{
uint32_t scancode = mapScancode(i);
if (scancode == 0)
continue;
state.keyDown[i] = false;
spice_key_up(scancode);
}
if (t_spice) if (t_spice)
SDL_WaitThread(t_spice, NULL); SDL_WaitThread(t_spice, NULL);
spice_disconnect();
}
if (state.lgr) if (state.lgr)
state.lgr->deinitialize(state.lgrData); state.lgr->deinitialize(state.lgrData);

View File

@ -646,7 +646,17 @@ bool spice_connect_channel(struct SpiceChannel * channel)
void spice_disconnect_channel(struct SpiceChannel * channel) void spice_disconnect_channel(struct SpiceChannel * channel)
{ {
if (channel->connected) if (channel->connected)
{
shutdown(channel->socket, SHUT_WR);
char buffer[1024];
ssize_t len = 0;
do
len = read(channel->socket, buffer, sizeof(buffer));
while(len > 0);
close(channel->socket); close(channel->socket);
}
channel->connected = false; channel->connected = false;
LG_LOCK_FREE(channel->lock); LG_LOCK_FREE(channel->lock);
} }

View File

@ -22,6 +22,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <stdint.h> #include <stdint.h>
bool spice_connect(const char * host, const short port, const char * password); bool spice_connect(const char * host, const short port, const char * password);
void spice_drain();
void spice_disconnect(); void spice_disconnect();
bool spice_process(); bool spice_process();
bool spice_ready(); bool spice_ready();