[client] added ivshmem_process thread and fixed shutdown behaviour

This commit is contained in:
Geoffrey McRae 2017-10-19 18:50:42 +11:00
parent 327a3a77cf
commit 3a7bff77f2
3 changed files with 64 additions and 12 deletions

View File

@ -42,6 +42,7 @@ struct IVSHMEMClient
struct IVSHMEM
{
bool connected;
bool shutdown;
int socket;
struct IVSHMEMServer server;
struct IVSHMEMClient * clients;
@ -53,6 +54,7 @@ struct IVSHMEM
struct IVSHMEM ivshmem =
{
.connected = false,
.shutdown = false,
.socket = -1
};
@ -69,6 +71,7 @@ void ivshmem_remove_client(struct IVSHMEMClient * client);
bool ivshmem_connect(const char * unix_socket)
{
ivshmem.shutdown = false;
ivshmem.socket = socket(AF_UNIX, SOCK_STREAM, 0);
if (ivshmem.socket < 0)
{
@ -177,6 +180,8 @@ void ivshmem_cleanup()
if (ivshmem.socket >= 0)
{
ivshmem.shutdown = true;
shutdown(ivshmem.socket, SHUT_RDWR);
close(ivshmem.socket);
ivshmem.socket = -1;
}
@ -186,7 +191,7 @@ void ivshmem_cleanup()
// ============================================================================
void ivshmem_close()
void ivshmem_disconnect()
{
if (!ivshmem.connected)
{
@ -250,12 +255,14 @@ bool ivshmem_read_msg(int64_t * index, int * fd)
int ret = recvmsg(ivshmem.socket, &msg, 0);
if (ret < sizeof(*index))
{
DEBUG_ERROR("failed ot read message\n");
if (!ivshmem.shutdown)
DEBUG_ERROR("failed to read message\n");
return false;
}
if (ret == 0)
{
if (!ivshmem.shutdown)
DEBUG_ERROR("lost connetion to server\n");
return false;
}
@ -279,6 +286,17 @@ bool ivshmem_read_msg(int64_t * index, int * fd)
return true;
}
uint16_t ivshmem_get_id()
{
if (!ivshmem.connected)
{
DEBUG_ERROR("not connected");
return -1;
}
return ivshmem.server.clientID;
}
// ============================================================================
void * ivshmem_get_map()
@ -378,6 +396,7 @@ bool ivshmem_process()
if (!ivshmem_read_msg(&index, &fd))
{
if (!ivshmem.shutdown)
DEBUG_ERROR("failed to read message");
return false;
}

View File

@ -3,8 +3,9 @@
#include <stdbool.h>
bool ivshmem_connect(const char * unix_socket);
void ivshmem_close();
void ivshmem_disconnect();
bool ivshmem_process();
uint16_t ivshmem_get_id();
void * ivshmem_get_map();
size_t ivshmem_get_map_size();

View File

@ -327,13 +327,32 @@ int renderThread(void * unused)
return 0;
}
int ivshmemThread(void * arg)
{
while(state.running)
if (!ivshmem_process())
{
if (state.running)
{
state.running = false;
DEBUG_ERROR("failed to process ivshmem messages");
}
break;
}
return 0;
}
int spiceThread(void * arg)
{
while(state.running)
if (!spice_process())
{
if (state.running)
{
state.running = false;
DEBUG_ERROR("Failed to process spice messages");
DEBUG_ERROR("failed to process spice messages");
}
break;
}
@ -464,6 +483,7 @@ int main(int argc, char * argv[])
state.running = true;
int shm_fd = 0;
SDL_Thread *t_ivshmem = NULL;
SDL_Thread *t_spice = NULL;
SDL_Thread *t_event = NULL;
@ -475,6 +495,12 @@ int main(int argc, char * argv[])
break;
}
if (!(t_ivshmem = SDL_CreateThread(ivshmemThread, "ivshmemThread", NULL)))
{
DEBUG_ERROR("ivshmem create thread failed");
break;
}
state.shm = (struct KVMGFXHeader *)ivshmem_get_map();
if (!state.shm)
{
@ -519,6 +545,12 @@ int main(int argc, char * argv[])
if (t_event)
SDL_WaitThread(t_event, NULL);
// this needs to happen here to abort any waiting reads
// as ivshmem uses recvmsg which has no timeout
ivshmem_disconnect();
if (t_ivshmem)
SDL_WaitThread(t_ivshmem, NULL);
if (t_spice)
SDL_WaitThread(t_spice, NULL);