frodo-wii/Src/NetworkUnix.h

170 lines
3.1 KiB
C
Raw Normal View History

2009-01-19 07:44:19 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
2009-01-24 16:48:43 +01:00
#include <netdb.h>
2009-01-19 07:44:19 +01:00
/* From glibc docs */
static int make_socket (uint16_t port)
{
int sock;
struct sockaddr_in name;
/* Create the socket. */
sock = socket (PF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("socket");
exit (EXIT_FAILURE);
}
/* Give the socket a name. */
name.sin_family = AF_INET;
name.sin_port = htons (port);
name.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
{
perror ("bind");
exit (1);
}
return sock;
}
2009-01-24 16:48:43 +01:00
bool init_sockaddr (struct sockaddr_in *name,
const char *hostname, uint16_t port)
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.\n", hostname);
return false;
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
return true;
}
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
NetworkServer::NetworkServer(int port)
2009-01-19 07:44:19 +01:00
{
this->n_clients = 0;
2009-01-24 16:48:43 +01:00
this->listen_sock = make_socket(port);
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
if (listen(this->listen_sock, MAX_NETWORK_CLIENTS) < 0)
2009-01-19 07:44:19 +01:00
{
perror("listen");
exit(1);
}
}
2009-01-24 16:48:43 +01:00
bool NetworkServer::CheckNewConnection()
2009-01-19 07:44:19 +01:00
{
struct timeval tv;
struct sockaddr_in client_name;
size_t size;
int client_sock;
2009-01-24 16:48:43 +01:00
fd_set listen_fds;
/* No more than that thanks... */
if (this->n_clients >= MAX_NETWORK_CLIENTS)
return false;
FD_ZERO(&listen_fds);
FD_SET(this->listen_sock, &listen_fds);
2009-01-19 07:44:19 +01:00
/* If something connects, create a new client */
memset(&tv, 0, sizeof(tv));
2009-01-24 16:48:43 +01:00
int v = select(this->listen_sock + 1, &listen_fds, NULL, NULL, &tv);
if ( v < 0)
{
perror("select");
exit(1);
}
else if ( v == 0 )
return false;
2009-01-19 07:44:19 +01:00
client_sock = accept(this->listen_sock, (struct sockaddr*)&client_name, &size);
if (client_sock < 0)
{
fprintf(stderr, "Accepting client failed\n");
2009-01-24 16:48:43 +01:00
return false;
2009-01-19 07:44:19 +01:00
}
2009-01-24 16:48:43 +01:00
printf("Nej men vobb! En klient har konnektat!\n");
/* And add the new one! */
this->AddClient(client_sock);
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
return true;
}
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
NetworkClient::NetworkClient(const char *hostname, int port)
2009-01-19 07:44:19 +01:00
{
2009-01-24 16:48:43 +01:00
/* Again from glibc docs */
struct sockaddr_in servername;
/* Create the socket. */
this->sock = socket (PF_INET, SOCK_STREAM, 0);
if (this->sock < 0)
{
perror ("socket (client)");
return;
}
/* Connect to the server. */
init_sockaddr (&servername, hostname, port);
if (connect(sock, (struct sockaddr *) &servername,
sizeof (servername)) != 0)
{
perror ("connect (client)");
return;
}
2009-01-19 07:44:19 +01:00
2009-01-24 16:48:43 +01:00
NetworkClient::NetworkClient(this->sock);
2009-01-19 07:44:19 +01:00
}
bool Network::ReceiveUpdate(NetworkUpdate *dst, int sock, struct timeval *tv)
{
fd_set fds;
int sz;
int v;
FD_ZERO(&fds);
FD_SET(sock, &fds);
v = select(1, &fds, NULL, NULL, tv);
if (v < 0)
{
fprintf(stderr, "Select failed\n");
return false;
}
sz = read(sock, (void*)dst, NETWORK_UPDATE_SIZE);
if (sz < 0)
return false;
/* Byte swap stuff */
this->DeMarshalData(dst);
return true;
}
2009-01-24 16:48:43 +01:00
bool Network::SendUpdate(int sock)
2009-01-19 07:44:19 +01:00
{
2009-01-24 16:48:43 +01:00
NetworkUpdate *src = this->ud;
2009-01-19 07:44:19 +01:00
int sz = src->size;
2009-01-24 16:48:43 +01:00
bool out = true;
2009-01-19 07:44:19 +01:00
this->MarshalData(src);
sz = write(sock, (void*)src, sz);
if (sz < src->size)
2009-01-24 16:48:43 +01:00
out = false;
return out;
2009-01-19 07:44:19 +01:00
}