Added code to send messages back to clients (untested)

This commit is contained in:
Pierre Pronchery 2014-03-23 01:58:07 +09:00
parent 82ead01089
commit 1ee2e1e8b9
2 changed files with 68 additions and 6 deletions

View File

@ -361,8 +361,31 @@ static void _destroy_server(TCP * tcp)
static int _tcp_client_send(TCP * tcp, AppTransportClient * client,
AppMessage * message)
{
/* FIXME implement */
size_t i;
TCPSocket * s;
Buffer * buffer;
if(tcp->mode != ATM_SERVER)
return -error_set_code(1, "%s", "Not a server");
/* lookup the client */
for(i = 0; i < tcp->u.server.clients_cnt; i++)
{
s = tcp->u.server.clients[i];
if(s->client == client)
break;
}
if(i == tcp->u.server.clients_cnt)
return -error_set_code(1, "%s", "Unknown client");
/* send the message */
if((buffer = buffer_new(0, NULL)) == NULL)
return -1;
if(appmessage_serialize(message, buffer) != 0
|| _tcp_socket_queue(s, buffer) != 0)
{
buffer_delete(buffer);
return -1;
}
return 0;
}
@ -373,6 +396,7 @@ static int _tcp_send(TCP * tcp, AppMessage * message)
if(tcp->mode != ATM_CLIENT)
return -error_set_code(1, "%s", "Not a client");
/* send the message */
if((buffer = buffer_new(0, NULL)) == NULL)
return -1;
if(appmessage_serialize(message, buffer) != 0

View File

@ -162,7 +162,8 @@ static UDP * _udp_init(AppTransportPluginHelper * helper,
res = _init_server(udp, name);
break;
default:
res = -error_set_code(1, "Unknown transport mode");
res = -error_set_code(1, "%s",
"Unknown transport mode");
break;
}
/* check for errors */
@ -292,8 +293,45 @@ static void _destroy_server(UDP * udp)
static int _udp_client_send(UDP * udp, AppTransportClient * client,
AppMessage * message)
{
/* FIXME implement */
int ret;
size_t i;
UDPClient * c;
Buffer * buffer;
/* lookup the client */
for(i = 0; i < udp->u.server.clients_cnt; i++)
{
c = udp->u.server.clients[i];
if(c->client == client)
break;
}
if(i == udp->u.server.clients_cnt)
return -error_set_code(1, "%s", "Unknown client");
/* send the message */
if((buffer = buffer_new(0, NULL)) == NULL)
return -1;
if(appmessage_serialize(message, buffer) != 0)
{
buffer_delete(buffer);
return -1;
}
#ifdef DEBUG
if(udp->aip->ai_family == AF_INET)
{
sa = (struct sockaddr_in *)udp->aip->ai_addr;
fprintf(stderr, "DEBUG: %s() %s (%s:%u) size=%lu\n", __func__,
"sendto()", inet_ntoa(sa->sin_addr),
ntohs(sa->sin_port), buffer_get_size(buffer));
}
else
fprintf(stderr, "DEBUG: %s() %s family=%d size=%lu\n", __func__,
"sendto()", udp->aip->ai_family,
buffer_get_size(buffer));
#endif
ret = sendto(udp->fd, buffer_get_data(buffer), buffer_get_size(buffer),
0, c->sa, c->sa_len);
buffer_delete(buffer);
return ret;
}
@ -308,7 +346,7 @@ static int _udp_send(UDP * udp, AppMessage * message)
if(udp->mode != ATM_CLIENT)
return -error_set_code(1, "%s", "Not a client");
/* FIXME confirm the message will be consistent and readable */
/* send the message */
if((buffer = buffer_new(0, NULL)) == NULL)
return -1;
if(appmessage_serialize(message, buffer) != 0)