able to communicate with client

This commit is contained in:
lxsang 2020-08-05 20:10:44 +02:00
parent c2ca2734dd
commit bec82e3d55
3 changed files with 71 additions and 3 deletions

View File

@ -14,6 +14,8 @@
#define CHANNEL_OPEN (uint8_t)0x4
#define CHANNEL_CLOSE (uint8_t)0x5
#define CHANNEL_DATA (uint8_t)0x6
#define CHANNEL_UNSUBSCRIBE (uint8_t)0x3
#define CHANNEL_SUBSCRIBE (uint8_t)0x2
typedef struct {
uint8_t type;

Binary file not shown.

View File

@ -1,16 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <signal.h>
#include <errno.h>
#include "tunnel.h"
#define MODULE_NAME "vterm"
static volatile int running = 1;
void int_handler(int dummy) {
(void) dummy;
running = 0;
}
int main(int argc, char** argv)
{
int fd;
tunnel_msg_t msg;
fd_set fd_in;
int status;
struct timeval timeout;
char buff[MAX_CHANNEL_NAME+1];
LOG_INIT(MODULE_NAME);
if(argc != 2)
@ -18,6 +31,7 @@ int main(int argc, char** argv)
printf("Usage: %s path/to/hotline/socket\n", argv[0]);
return -1;
}
signal(SIGINT, int_handler);
LOG(MODULE_NAME, "Hotline is: %s", argv[1]);
// now try to request new channel from hotline
fd = open_unix_socket(argv[1]);
@ -53,19 +67,71 @@ int main(int argc, char** argv)
if(msg.data)
free(msg.data);
}
// now read data
while(running)
{
timeout.tv_sec = 0;
timeout.tv_usec = 500;
FD_ZERO(&fd_in);
FD_SET(fd, &fd_in);
status = select(fd + 1, &fd_in, NULL, NULL, &timeout);
switch (status)
{
case -1:
LOG(MODULE_NAME, "Error %d on select()\n", errno);
running = 0;
break;
case 0:
timeout.tv_sec = 0;
timeout.tv_usec = 10000; // 5 ms
select(0, NULL, NULL, NULL, &timeout);
break;
// we have data
default:
if(msg_read(fd, &msg) == -1)
{
ERROR(MODULE_NAME, "Unable to read message from channel. quit");
(void) close(fd);
running = 0;
}
else
{
switch (msg.header.type)
{
case CHANNEL_SUBSCRIBE:
LOG(MODULE_NAME, "Client %d subscribes to the chanel", msg.header.client_id);
break;
case CHANNEL_UNSUBSCRIBE:
LOG(MODULE_NAME, "Client %d unsubscribes to the chanel", msg.header.client_id);
break;
case CHANNEL_DATA:
LOG(MODULE_NAME, "Got data");
if(msg_write(fd, &msg) == -1)
{
LOG(MODULE_NAME,"Unable to write data back");
}
break;
default:
LOG(MODULE_NAME, "Client %d send message of type %d", msg.header.client_id, msg.header.type);
break;
}
}
}
}
// close the channel
LOG(MODULE_NAME, "Close the channel %s (%d)", MODULE_NAME, fd);
msg.header.type = CHANNEL_CLOSE;
msg.header.size = 0;
msg.data = NULL;
sleep(5);
if( msg_write(fd, &msg) == -1)
{
ERROR(MODULE_NAME, "Unable to request channel close");
}
(void)msg_read(fd, &msg);
shutdown(fd, SHUT_WR);
(void) close(fd);
printf("Main application\n");
return 0;
}