2018-11-20 13:21:09 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
2018-11-29 16:04:37 +01:00
|
|
|
#include <fcntl.h>
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2018-11-20 13:21:09 +01:00
|
|
|
int socket;
|
|
|
|
struct sockaddr_in address;
|
|
|
|
} TData;
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
2018-11-20 13:21:09 +01:00
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
2018-11-20 15:02:47 +01:00
|
|
|
} TPosition;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
float w;
|
|
|
|
} TRotation;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
TPosition position;
|
|
|
|
TRotation rotation;
|
2018-11-20 14:41:42 +01:00
|
|
|
char shoot;
|
2018-11-20 15:02:47 +01:00
|
|
|
char hit;
|
2018-11-20 13:21:09 +01:00
|
|
|
} TFloat;
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
void Error(int err);
|
|
|
|
void *dataProcessHandler(void *data);
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2018-11-20 13:21:09 +01:00
|
|
|
int e = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
struct sockaddr_in myAddr;
|
|
|
|
int sockMain;
|
|
|
|
int port = 8080;
|
|
|
|
socklen_t addrLen;
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
int sock[2];
|
2018-11-20 13:21:09 +01:00
|
|
|
struct sockaddr_in myAddrForClients[2];
|
2018-11-20 14:41:42 +01:00
|
|
|
struct sockaddr_in clients[2];
|
2018-11-20 13:21:09 +01:00
|
|
|
struct sockaddr_in tmp;
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
myAddr.sin_family = AF_INET;
|
|
|
|
myAddr.sin_port = htons(port);
|
|
|
|
myAddr.sin_addr.s_addr = INADDR_ANY;
|
2018-11-20 13:21:09 +01:00
|
|
|
|
|
|
|
myAddrForClients[0].sin_family = AF_INET;
|
|
|
|
myAddrForClients[1].sin_family = AF_INET;
|
|
|
|
|
|
|
|
myAddrForClients[0].sin_port = htons(60000);
|
|
|
|
myAddrForClients[1].sin_port = htons(60000);
|
|
|
|
|
|
|
|
myAddrForClients[0].sin_addr.s_addr = INADDR_ANY;
|
|
|
|
myAddrForClients[1].sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
2018-11-29 16:04:37 +01:00
|
|
|
pthread_t video_thread;
|
|
|
|
if (pthread_create(&video_thread, NULL, videoHandler, NULL) < 0)
|
|
|
|
{
|
|
|
|
printf("Error");
|
|
|
|
perror('video_thread');
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if ((sock[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
|
|
{
|
|
|
|
Error(e++);
|
2018-11-20 13:21:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
while (bind(sock[i], (struct sockaddr *)&myAddrForClients[i], sizeof(struct sockaddr_in)) < 0)
|
|
|
|
{
|
2018-11-20 13:21:09 +01:00
|
|
|
myAddrForClients[i].sin_port = htons(ntohs(myAddrForClients[i].sin_port) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
if ((sockMain = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
|
|
{
|
|
|
|
Error(e++);
|
2018-11-20 13:21:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
if (bind(sockMain, (struct sockaddr *)&myAddr, sizeof(struct sockaddr_in)) < 0)
|
|
|
|
{
|
|
|
|
Error(e++);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
addrLen = sizeof(struct sockaddr_in);
|
2018-11-20 14:10:40 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
int flag = 2;
|
|
|
|
int recvLen, sendLen;
|
2018-11-20 13:21:09 +01:00
|
|
|
|
|
|
|
char buf[5];
|
2018-11-20 14:41:42 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
while (flag)
|
|
|
|
{
|
|
|
|
recvLen = recvfrom(sockMain, buf, 5, 0, (struct sockaddr *)&tmp, &addrLen);
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
if (buf[0] != 'U' || buf[1] != 'N' || buf[2] != 'I' || buf[3] != 'T' || buf[4] != 'Y')
|
|
|
|
{
|
|
|
|
Error(e++);
|
2018-11-20 13:21:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2018-11-20 14:41:42 +01:00
|
|
|
clients[2 - flag--] = tmp;
|
|
|
|
}
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
for (i = 0; i < (sizeof(myAddrForClients) / sizeof(myAddrForClients[0])); i++)
|
|
|
|
{
|
2018-11-20 13:21:09 +01:00
|
|
|
|
|
|
|
char port2char[8];
|
2018-11-20 14:41:42 +01:00
|
|
|
sprintf(port2char, "%d\r\n", ntohs(myAddrForClients[i].sin_port));
|
|
|
|
sendLen = sendto(sockMain, port2char, sizeof(port2char), 0, (struct sockaddr *)&clients[i], sizeof(clients[i]));
|
2018-11-20 13:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Unity's got port number to connect
|
|
|
|
//myAddrForClients[x] holds server addresses for each Unity player
|
|
|
|
//clients[x] holds Unity's addresses
|
|
|
|
|
|
|
|
pthread_t handlers[2];
|
|
|
|
TData data[2];
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
int size = (sizeof(handlers) / sizeof(handlers[0]));
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2018-11-20 13:21:09 +01:00
|
|
|
data[i].socket = sock[i];
|
2018-11-20 14:41:42 +01:00
|
|
|
data[i].address = clients[size - 1 - i];
|
2018-11-20 13:21:09 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (pthread_create(&handlers[i], NULL, dataProcessHandler, &data[i]))
|
|
|
|
{
|
|
|
|
Error(e++);
|
2018-11-20 13:21:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (pthread_join(handlers[i], NULL))
|
|
|
|
{
|
|
|
|
Error(e++);
|
2018-11-20 13:21:09 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockMain);
|
|
|
|
close(sock[0]);
|
|
|
|
close(sock[1]);
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
return 0;
|
2018-11-20 13:21:09 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
void *dataProcessHandler(void *data)
|
|
|
|
{
|
2018-11-20 13:21:09 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
TData *info = (TData *)data;
|
2018-11-20 13:21:09 +01:00
|
|
|
|
|
|
|
TFloat floats;
|
2018-11-20 14:41:42 +01:00
|
|
|
memset(&floats, 0, sizeof(floats));
|
2018-11-20 13:21:09 +01:00
|
|
|
|
|
|
|
struct sockaddr_in tmp;
|
|
|
|
socklen_t addrLen;
|
|
|
|
|
|
|
|
printf("HANDLER STARTED..\n");
|
2018-11-20 14:41:42 +01:00
|
|
|
printf("LOOKING AT SOCKET: %d, ON PORT: %d\n", info->socket, ntohs(info->address.sin_port));
|
2018-11-20 14:10:40 +01:00
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2018-11-20 15:02:47 +01:00
|
|
|
if (recvfrom(info->socket, &floats, 30, 0, (struct sockaddr *)&tmp, &addrLen) != 30)
|
2018-11-20 14:41:42 +01:00
|
|
|
continue;
|
2018-11-20 15:02:47 +01:00
|
|
|
sendto(info->socket, &floats, 30, 0, (struct sockaddr *)&info->address, sizeof(info->address));
|
2018-11-20 13:21:09 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:41:42 +01:00
|
|
|
void Error(int err)
|
|
|
|
{
|
|
|
|
printf("Error! Code: %d\n", err);
|
2018-11-20 13:21:09 +01:00
|
|
|
}
|
2018-11-29 00:22:35 +01:00
|
|
|
|
|
|
|
void *videoHandler()
|
|
|
|
{
|
2018-11-29 16:04:37 +01:00
|
|
|
printf("video_handler\n");
|
2018-11-29 00:22:35 +01:00
|
|
|
char video_buffer[1024];
|
2018-11-29 16:04:37 +01:00
|
|
|
struct stat *movieInfo;
|
2018-11-29 00:22:35 +01:00
|
|
|
int counter = 0; //Counts users that request video data
|
|
|
|
int VideoSocketTCP;
|
|
|
|
int new_socket;
|
2018-11-29 16:04:37 +01:00
|
|
|
int read_movie;
|
2018-11-29 00:22:35 +01:00
|
|
|
struct sockaddr_in video_address_tcp;
|
2018-11-29 16:04:37 +01:00
|
|
|
int addrlen = sizeof(video_address_tcp);
|
2018-11-29 00:22:35 +01:00
|
|
|
|
|
|
|
if ((VideoSocketTCP = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
|
|
{
|
|
|
|
perror("tcp video socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
video_address_tcp.sin_family = AF_INET;
|
|
|
|
video_address_tcp.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
video_address_tcp.sin_port = htons(8989);
|
|
|
|
|
|
|
|
if (bind(VideoSocketTCP, (struct sockaddr *)&video_address_tcp,
|
|
|
|
sizeof(video_address_tcp)) < 0)
|
|
|
|
{
|
|
|
|
perror("bind failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (listen(VideoSocketTCP, 3) < 0)
|
|
|
|
{
|
|
|
|
perror("listen");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (counter < 2)
|
|
|
|
{
|
|
|
|
if ((new_socket = accept(VideoSocketTCP, (struct sockaddr *)&video_address_tcp,
|
2018-11-29 16:04:37 +01:00
|
|
|
(socklen_t)&addrlen) < 0))
|
2018-11-29 00:22:35 +01:00
|
|
|
{
|
|
|
|
perror("accept");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-11-29 16:04:37 +01:00
|
|
|
|
|
|
|
int movie_open = open("assets/test3.mp4", O_RDONLY);
|
|
|
|
int movie_file_stat = fstat(movie_open, movieInfo);
|
|
|
|
send(new_socket, &movieInfo->st_size, sizeof(movieInfo->st_size), 0);
|
|
|
|
while ((read_movie = read(movie_open, video_buffer, sizeof(video_buffer))) > 0)
|
|
|
|
{
|
|
|
|
send(new_socket, video_buffer, sizeof(video_buffer), 0);
|
|
|
|
}
|
|
|
|
counter++;
|
2018-11-29 00:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
close(VideoSocketTCP);
|
|
|
|
close(new_socket);
|
|
|
|
return 0;
|
|
|
|
}
|