Some clean up in network code.

This commit is contained in:
joris 2013-03-14 14:34:29 +01:00
parent 844cf7f97c
commit d73c670b9a
4 changed files with 43 additions and 52 deletions

View file

@ -272,13 +272,10 @@ static void KickIdlers(void)
/**
** Read data
*/
static int ReadData(void)
static int ReadData()
{
Session *session;
Session *next;
int result;
int result = Pool->Sockets->Select(0);
result = NetSocketSetReady(Pool->Sockets, 0);
if (result == 0) {
// No sockets ready
return 0;
@ -289,14 +286,12 @@ static int ReadData(void)
}
// ready sockets
for (session = Pool->First; session; ) {
next = session->Next;
if (NetSocketSetSocketReady(Pool->Sockets, session->Sock)) {
int clen;
for (Session *session = Pool->First; session; ) {
Session *next = session->Next;
if (Pool->Sockets->HasDataToRead(session->Sock)) {
// socket ready
session->Idle = time(0);
clen = strlen(session->Buffer);
int clen = strlen(session->Buffer);
result = NetRecvTCP(session->Sock, session->Buffer + clen,
sizeof(session->Buffer) - clen);
if (result < 0) {

View file

@ -96,12 +96,20 @@ typedef int Socket;
-- Declarations
----------------------------------------------------------------------------*/
struct SocketSet {
class SocketSet
{
public:
SocketSet() : MaxSockFD(0) {}
void AddSocket(Socket socket);
void DelSocket(Socket socket);
/// Wait for socket set ready.
int Select(int timeout);
/// Check if a socket in a socket set is ready.
int HasDataToRead(Socket socket) const;
private:
std::vector<Socket> Sockets;
std::vector<int> SocketReady;
Socket MaxSockFD;
@ -123,35 +131,29 @@ extern unsigned long NetLocalAddrs[]; /// Local IP-Addrs of this host (net form
extern int NetInit();
/// Hardware dependend network exit.
extern void NetExit();
/// Resolve host in name or or colon dot notation.
extern unsigned long NetResolveHost(const std::string &host);
/// Get local IP from network file descriptor
extern int NetSocketAddr(const Socket sock);
/// Open a UDP Socket port.
extern Socket NetOpenUDP(const char *addr, int port);
/// Open a TCP Socket port.
extern Socket NetOpenTCP(const char *addr, int port);
/// Close a UDP socket port.
extern void NetCloseUDP(Socket sockfd);
/// Close a TCP socket port.
extern void NetCloseTCP(Socket sockfd);
/// Set socket to non-blocking
extern int NetSetNonBlocking(Socket sockfd);
/// Open a TCP connection.
extern int NetConnectTCP(Socket sockfd, unsigned long addr, int port);
/// Send through a UPD socket to a host:port.
extern int NetSendUDP(Socket sockfd, unsigned long host, int port,
const void *buf, int len);
/// Send through a TCP socket
extern int NetSendTCP(Socket sockfd, const void *buf, int len);
/// Wait for socket ready.
extern int NetSocketReady(Socket sockfd, int timeout);
/// Wait for socket set ready.
extern int NetSocketSetReady(SocketSet *sockfd, int timeout);
/// Check if a socket in a socket set is ready.
extern int NetSocketSetSocketReady(SocketSet *set, Socket socket);
extern int NetSendUDP(Socket sockfd, unsigned long host, int port, const void *buf, int len);
/// Receive from a UDP socket.
extern int NetRecvUDP(Socket sockfd, void *buf, int len);
/// Open a TCP Socket port.
extern Socket NetOpenTCP(const char *addr, int port);
/// Close a TCP socket port.
extern void NetCloseTCP(Socket sockfd);
/// Open a TCP connection.
extern int NetConnectTCP(Socket sockfd, unsigned long addr, int port);
/// Send through a TCP socket
extern int NetSendTCP(Socket sockfd, const void *buf, int len);
/// Receive from a TCP socket.
extern int NetRecvTCP(Socket sockfd, void *buf, int len);
/// Listen for connections on a TCP socket
@ -159,10 +161,10 @@ extern int NetListenTCP(Socket sockfd);
/// Accept a connection on a TCP socket
extern Socket NetAcceptTCP(Socket sockfd);
/// Add a socket to a socket set
extern void NetAddSocket(SocketSet *set, Socket socket);
/// Delete a socket from a socket set
extern void NetDelSocket(SocketSet *set, Socket socket);
/// Set socket to non-blocking
extern int NetSetNonBlocking(Socket sockfd);
/// Wait for socket ready.
extern int NetSocketReady(Socket sockfd, int timeout);
//@}

View file

@ -83,7 +83,7 @@ enum _message_type_ {
MessageCommandAttack, /// Unit command attack
MessageCommandGround, /// Unit command attack ground
MessageCommandPatrol, /// Unit command patrol
MessageCommandBoard, /// Unit command borad
MessageCommandBoard, /// Unit command board
MessageCommandUnload, /// Unit command unload
MessageCommandBuild, /// Unit command build building
MessageCommandDismiss, /// Unit command dismiss unit

View file

@ -37,8 +37,6 @@
#include "net_lowlevel.h"
#include "network.h"
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
@ -497,58 +495,54 @@ int NetSocketReady(Socket sockfd, int timeout)
/**
** Wait for socket set ready.
**
** @param set Socket set to probe.
** @param timeout Timeout in 1/1000 seconds.
**
** @return 1 if data is available, 0 if not, -1 if failure.
*/
int NetSocketSetReady(SocketSet *set, int timeout)
int SocketSet::Select(int timeout)
{
int retval;
struct timeval tv;
fd_set mask;
// Check the file descriptors for available data
do {
// Set up the mask of file descriptors
FD_ZERO(&mask);
for (size_t i = 0; i < set->Sockets.size(); ++i) {
FD_SET(set->Sockets[i], &mask);
for (size_t i = 0; i < this->Sockets.size(); ++i) {
FD_SET(this->Sockets[i], &mask);
}
// Set up the timeout
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
// Data available?
retval = select(set->MaxSockFD + 1, &mask, NULL, NULL, &tv);
retval = select(this->MaxSockFD + 1, &mask, NULL, NULL, &tv);
#ifdef USE_WINSOCK
} while (retval == SOCKET_ERROR && WSAGetLastError() == WSAEINTR);
#else
} while (retval == -1 && errno == EINTR);
#endif
for (size_t i = 0; i < set->Sockets.size(); ++i)
{
set->SocketReady[i] = FD_ISSET(set->Sockets[i], &mask);
for (size_t i = 0; i != this->Sockets.size(); ++i) {
this->SocketReady[i] = FD_ISSET(this->Sockets[i], &mask);
}
return retval;
}
/**
** Check if a socket in a socket set is ready.
**
** @param set Socket set
** @param socket Socket to check
**
** @return Non-zero if socket is ready
*/
int NetSocketSetSocketReady(SocketSet *set, Socket socket)
int SocketSet::HasDataToRead(Socket socket) const
{
for (size_t i = 0; i < set->Sockets.size(); ++i) {
if (set->Sockets[i] == socket) {
return set->SocketReady[i];
for (size_t i = 0; i < this->Sockets.size(); ++i) {
if (this->Sockets[i] == socket) {
return this->SocketReady[i];
}
}
DebugPrint("Socket not found in socket set\n");