aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Weisse <[email protected]>2004-10-17 20:33:18 +0000
committerCarsten Weisse <[email protected]>2004-10-17 20:33:18 +0000
commitacd23027524869949409a8f84b1c13dcd4bc9c97 (patch)
treec396a857220c99804e9566f553bdde1beb3b55b1
parentfc066112b645e79463b914e4c633f793fdba487d (diff)
nonblocking network and code cleanup
-rw-r--r--src/jake2/qcommon/netadr_t.java25
-rw-r--r--src/jake2/sys/NET.java447
2 files changed, 102 insertions, 370 deletions
diff --git a/src/jake2/qcommon/netadr_t.java b/src/jake2/qcommon/netadr_t.java
index 47ec5a8..72473d7 100644
--- a/src/jake2/qcommon/netadr_t.java
+++ b/src/jake2/qcommon/netadr_t.java
@@ -19,9 +19,11 @@
*/
// Created on 27.11.2003 by RST.
-// $Id: netadr_t.java,v 1.3 2004-10-17 20:20:09 cawe Exp $
+// $Id: netadr_t.java,v 1.4 2004-10-17 20:33:18 cawe Exp $
package jake2.qcommon;
+import jake2.Defines;
+
import java.net.InetAddress;
import java.net.UnknownHostException;
@@ -31,16 +33,27 @@ public class netadr_t {
public int port;
- public byte ip[] = { 0, 0, 0, 0 };
-
- //public byte ipx[] = new byte[10];
+ public byte ip[];
InetAddress ia = null;
+ public netadr_t() {
+ this.type = Defines.NA_LOOPBACK;
+ this.port = 0; // any
+ try {
+ this.ip = InetAddress.getByName("localhost").getAddress();
+ } catch (UnknownHostException e) {
+ }
+ }
+
public InetAddress getInetAddress() throws UnknownHostException {
- if (ia == null)
+ if (type == Defines.NA_BROADCAST) {
+ ia = InetAddress.getByName("255.255.255.255");
+ } else if (type == Defines.NA_LOOPBACK) {
+ ia = InetAddress.getByName("localhost");
+ } else if (ia == null) {
ia = InetAddress.getByAddress(ip);
-
+ }
return ia;
}
diff --git a/src/jake2/sys/NET.java b/src/jake2/sys/NET.java
index 8202318..86c6e29 100644
--- a/src/jake2/sys/NET.java
+++ b/src/jake2/sys/NET.java
@@ -1,7 +1,7 @@
/*
* NET.java Copyright (C) 2003
*
- * $Id: NET.java,v 1.3 2004-10-17 20:20:09 cawe Exp $
+ * $Id: NET.java,v 1.4 2004-10-17 20:33:17 cawe Exp $
*/
/*
* Copyright (C) 1997-2001 Id Software, Inc.
@@ -29,17 +29,17 @@ import jake2.Globals;
import jake2.game.cvar_t;
import jake2.qcommon.*;
+import java.io.IOException;
import java.net.*;
-import java.util.Arrays;
+import java.nio.ByteBuffer;
+import java.nio.channels.DatagramChannel;
-public final class NET extends Defines {
+public final class NET {
- public static netadr_t net_local_adr = new netadr_t();
+ private final static int MAX_LOOPBACK = 4;
- // 127.0.0.1
- public final static int LOOPBACK = 0x7f000001;
-
- public final static int MAX_LOOPBACK = 4;
+ // local loopback adress
+ private static netadr_t net_local_adr = new netadr_t();
public static class loopmsg_t {
byte data[] = new byte[Defines.MAX_MSGLEN];
@@ -66,47 +66,15 @@ public final class NET extends Defines {
loopbacks[1] = new loopback_t();
}
- public static DatagramSocket ip_sockets[] = { null, null };
-
- //public static DatagramSocket ipx_sockets[] = new int[2];
+ private static DatagramChannel[] ip_channels = { null, null };
- // we dont need beschissene sockaddr_in structs in java !
+ private static DatagramSocket[] ip_sockets = { null, null };
//=============================================================================
- // void NetadrToSockadr (netadr_t *a, struct sockaddr_in *s)
- // {
- // memset (s, 0, sizeof(*s));
- //
- // if (a.type == NA_BROADCAST)
- // {
- // s.sin_family = AF_INET;
- //
- // s.sin_port = a.port;
- // *(int *)&s.sin_addr = -1;
- // }
- // else if (a.type == NA_IP)
- // {
- // s.sin_family = AF_INET;
- //
- // *(int *)&s.sin_addr = *(int *)&a.ip;
- // s.sin_port = a.port;
- // }
- // }
- //
- // void SockadrToNetadr (struct sockaddr_in *s, netadr_t *a)
- // {
- // *(int *)&a.ip = *(int *)&s.sin_addr;
- // a.port = s.sin_port;
- // a.type = NA_IP;
- // }
- //
-
public static boolean CompareAdr(netadr_t a, netadr_t b) {
- if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2]
- && a.ip[3] == b.ip[3] && a.port == b.port)
- return true;
- return false;
+ return (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2]
+ && a.ip[3] == b.ip[3] && a.port == b.port);
}
/*
@@ -122,132 +90,37 @@ public final class NET extends Defines {
return true;
if (a.type == Defines.NA_IP) {
- if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2]
- && a.ip[3] == b.ip[3])
- return true;
- return false;
+ return (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1]
+ && a.ip[2] == b.ip[2] && a.ip[3] == b.ip[3]);
}
-
- /*
- * if (a.type == Defines.NA_IPX) { for (int n = 0; n < 10; n++) if
- * (a.ipx[n] != b.ipx[n]) return false;
- *
- * //was: //if ((memcmp(a.ipx, b.ipx, 10) == 0)) return true; return
- * true; }
- */
-
return false;
}
public static String AdrToString(netadr_t a) {
- //was:
- //static char s[64];
- //Com_sprintf (s, sizeof(s), "%i.%i.%i.%i:%i", a.ip[0], a.ip[1],
- // a.ip[2], a.ip[3], ntohs(a.port));
-
return "" + (a.ip[0] & 0xff) + "." + (a.ip[1] & 0xff) + "."
+ (a.ip[2] & 0xff) + "." + (a.ip[3] & 0xff) + ":" + a.port;
}
public static String NET_BaseAdrToString(netadr_t a) {
- //was:
- //static char s[64];
- //Com_sprintf (s, sizeof(s), "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2],
- // a.ip[3]);
return "" + (a.ip[0] & 0xff) + "." + (a.ip[1] & 0xff) + "."
+ (a.ip[2] & 0xff) + "." + (a.ip[3] & 0xff);
}
- /*
- * ============= NET_StringToAdr
- *
- * localhost idnewt idnewt:28000 192.246.40.70 192.246.40.70:28000
- * =============
- */
- // boolean NET_StringToSockaddr (char *s, struct sockaddr *sadr)
- // {
- // struct hostent *h;
- // char *colon;
- // char copy[128];
- //
- // memset (sadr, 0, sizeof(*sadr));
- // ((struct sockaddr_in *)sadr).sin_family = AF_INET;
- //
- // ((struct sockaddr_in *)sadr).sin_port = 0;
- //
- // strcpy (copy, s);
- // // strip off a trailing :port if present
- // for (colon = copy ; *colon ; colon++)
- // if (*colon == ':')
- // {
- // *colon = 0;
- // ((struct sockaddr_in *)sadr).sin_port = htons((short)atoi(colon+1));
- // }
- //
- // if (copy[0] >= '0' && copy[0] <= '9')
- // {
- // *(int *)&((struct sockaddr_in *)sadr).sin_addr = inet_addr(copy);
- // }
- // else
- // {
- // if (! (h = gethostbyname(copy)) )
- // return 0;
- // *(int *)&((struct sockaddr_in *)sadr).sin_addr = *(int
- // *)h.h_addr_list[0];
- // }
- //
- // return true;
- // }
- //
- // /*
- // =============
- // NET_StringToAdr
- //
- // localhost
- // idnewt
- // idnewt:28000
- // 192.246.40.70
- // 192.246.40.70:28000
- // =============
- // */
- // boolean NET_StringToAdr (char *s, netadr_t *a)
- // {
- // struct sockaddr_in sadr;
- //
- // if (!strcmp (s, "localhost"))
- // {
- // memset (a, 0, sizeof(*a));
- // a.type = NA_LOOPBACK;
- // return true;
- // }
- //
- // if (!NET_StringToSockaddr (s, (struct sockaddr *)&sadr))
- // return false;
- //
- // SockadrToNetadr (&sadr, a);
- //
- // return true;
- // }
- //
public static boolean StringToAdr(String s, netadr_t a) {
-
- // bugfix bzw. hack cwei
if (s.equalsIgnoreCase("localhost")) {
- a.type = Defines.NA_LOOPBACK;
- Arrays.fill(a.ip, (byte) 0);
- a.port = 0;
+ a.set(net_local_adr);
return true;
}
try {
String[] address = s.split(":");
InetAddress ia = InetAddress.getByName(address[0]);
a.ip = ia.getAddress();
- a.type = NA_IP;
+ a.type = Defines.NA_IP;
if (address.length == 2)
a.port = Integer.parseInt(address[1]);
return true;
} catch (Exception e) {
- e.printStackTrace();
+ Com.Println(e.getMessage());
return false;
}
}
@@ -264,12 +137,9 @@ public final class NET extends Defines {
* =============================================================================
*/
- // trivial! this SHOULD work !
public static boolean NET_GetLoopPacket(int sock, netadr_t net_from,
sizebuf_t net_message) {
- int i;
loopback_t loop;
-
loop = loopbacks[sock];
if (loop.send - loop.get > MAX_LOOPBACK)
@@ -278,23 +148,17 @@ public final class NET extends Defines {
if (loop.get >= loop.send)
return false;
- i = loop.get & (MAX_LOOPBACK - 1);
+ int i = loop.get & (MAX_LOOPBACK - 1);
loop.get++;
- //memcpy (net_message.data, loop.msgs[i].data, loop.msgs[i].datalen);
System.arraycopy(loop.msgs[i].data, 0, net_message.data, 0,
loop.msgs[i].datalen);
net_message.cursize = loop.msgs[i].datalen;
- net_from.ip = net_local_adr.ip;
- net_from.port = net_local_adr.port;
- net_from.type = net_local_adr.type;
-
+ net_from.set(net_local_adr);
return true;
-
}
- // trivial! this SHOULD work !
public static void NET_SendLoopPacket(int sock, int length, byte[] data,
netadr_t to) {
int i;
@@ -306,160 +170,77 @@ public final class NET extends Defines {
i = loop.send & (MAX_LOOPBACK - 1);
loop.send++;
- //memcpy (loop.msgs[i].data, data, length);
-
System.arraycopy(data, 0, loop.msgs[i].data, 0, length);
loop.msgs[i].datalen = length;
}
- private static DatagramPacket receivedatagrampacket = new DatagramPacket(
- new byte[65507], 65507);
-
//=============================================================================
public static boolean GetPacket(int sock, netadr_t net_from,
sizebuf_t net_message) {
- DatagramSocket net_socket;
- // int ret;
- // struct sockaddr_in from;
- // int fromlen;
- //
- // int protocol;
- // int err;
if (NET_GetLoopPacket(sock, net_from, net_message)) {
- //Com.DPrintf("received packet on sock=" + sock + " len=" +
- // net_message.cursize+"\n");
return true;
}
- net_socket = ip_sockets[sock];
- if (net_socket == null)
+ if (ip_sockets[sock] == null)
return false;
try {
- net_socket.receive(receivedatagrampacket);
+ ByteBuffer receiveBuffer = ByteBuffer.wrap(net_message.data);
- // no timeout...
+ InetSocketAddress srcSocket = (InetSocketAddress) ip_channels[sock]
+ .receive(receiveBuffer);
+ if (srcSocket == null)
+ return false;
- net_from.ip = receivedatagrampacket.getAddress().getAddress();
- net_from.port = receivedatagrampacket.getPort();
- net_from.type = NA_IP;
+ net_from.ip = srcSocket.getAddress().getAddress();
+ net_from.port = srcSocket.getPort();
+ net_from.type = Defines.NA_IP;
- if (receivedatagrampacket.getLength() > net_message.maxsize) {
- Com.Printf("Oversize packet from " + AdrToString(net_from)
- + "\n");
+ int packetLength = receiveBuffer.position();
+
+ if (packetLength > net_message.maxsize) {
+ Com.Println("Oversize packet from " + AdrToString(net_from));
return false;
}
- int length = receivedatagrampacket.getLength();
- System.arraycopy(receivedatagrampacket.getData(), 0,
- net_message.data, 0, length);
-
- // bugfix cwei
- net_message.cursize = length; // set the size
- net_message.data[length] = 0; // sentinel
-
- //Com.DPrintf(Lib.hexDump(net_message.data, Math.max(length, length
- // - (length % 16) + 16), false));
- //Com.DPrintf("\n");
+ // set the size
+ net_message.cursize = packetLength;
+ // set the sentinel
+ net_message.data[packetLength] = 0;
return true;
- } catch (SocketTimeoutException e1) {
- return false;
- }
-
- catch (Exception e) {
+ } catch (IOException e) {
Com.DPrintf("NET_GetPacket: " + e + " from "
+ AdrToString(net_from) + "\n");
return false;
}
-
- //fromlen = sizeof(from);
- //ret = recvfrom (net_socket, net_message.data, net_message.maxsize
- // , 0, (struct sockaddr *)&from, &fromlen);
-
- //SockadrToNetadr (&from, net_from);
-
- // if (ret == -1)
- // {
- // err = errno;
- //
- // if (err == EWOULDBLOCK || err == ECONNREFUSED)
- // continue;
- // Com_Printf ("NET_GetPacket: %s from %s\n", NET_ErrorString(),
- // NET_AdrToString(*net_from));
- // continue;
- // }
- //
- // if (ret == net_message.maxsize)
- // {
- // Com_Printf ("Oversize packet from %s\n", NET_AdrToString
- // (*net_from));
- // continue;
- // }
- //
- // net_message.cursize = ret;
- // return true;
-
}
// =============================================================================
public static void SendPacket(int sock, int length, byte[] data, netadr_t to) {
- //Com.Printf("NET_SendPacket: sock=" + sock + " len=" + length + "\n");
- //Com.DPrintf(Lib.hexDump(data, Math.max(length, length - (length % 16)
- // + 16), false));
- //Com.DPrintf("\n");
-
- int ret;
- //struct sockaddr_in addr;
-
- DatagramSocket net_socket;
-
- if (to.type == NA_LOOPBACK) {
+ if (to.type == Defines.NA_LOOPBACK) {
NET_SendLoopPacket(sock, length, data, to);
return;
}
- if (to.type == NA_BROADCAST) {
- net_socket = ip_sockets[sock];
- if (net_socket == null)
- return;
- } else if (to.type == NA_IP) {
- net_socket = ip_sockets[sock];
- if (net_socket == null)
- return;
- }
- /*
- * else if (to.type == NA_IPX) { net_socket = ipx_sockets[sock]; if
- * (net_socket==null) return; } else if (to.type == NA_BROADCAST_IPX) {
- * net_socket = ipx_sockets[sock]; if (!net_socket) return; }
- */
- else {
- Com.Error(ERR_FATAL, "NET_SendPacket: bad address type");
+ if (ip_sockets[sock] == null)
return;
- }
- //was:
- //NetadrToSockadr (&to, &addr);
-
- try { //was:
- //ret = sendto (net_socket, data, length, 0, (struct sockaddr
- // *)&addr, sizeof(addr) );
-
- DatagramPacket dp;
-
- if (to.type == NA_BROADCAST) {
- dp = new DatagramPacket(data, length, InetAddress
- .getByAddress(new byte[] { -1, -1, -1, -1 }), to.port);
- } else
- dp = new DatagramPacket(data, length, to.getInetAddress(),
- to.port);
+ if (to.type != Defines.NA_BROADCAST && to.type != Defines.NA_IP) {
+ Com.Error(Defines.ERR_FATAL, "NET_SendPacket: bad address type");
+ return;
+ }
- net_socket.send(dp);
+ try {
+ SocketAddress dstSocket = new InetSocketAddress(
+ to.getInetAddress(), to.port);
+ ip_channels[sock].send(ByteBuffer.wrap(data, 0, length), dstSocket);
} catch (Exception e) {
- Com.Printf("NET_SendPacket ERROR: " + e + " to " + AdrToString(to)
- + "\n");
+ Com
+ .Println("NET_SendPacket ERROR: " + e + " to "
+ + AdrToString(to));
}
}
@@ -471,42 +252,34 @@ public final class NET extends Defines {
public static void NET_OpenIP() {
cvar_t port, ip;
- port = Cvar.Get("port", "" + Defines.PORT_SERVER, CVAR_NOSET);
- ip = Cvar.Get("ip", "localhost", CVAR_NOSET);
+ port = Cvar.Get("port", "" + Defines.PORT_SERVER, Defines.CVAR_NOSET);
+ ip = Cvar.Get("ip", "localhost", Defines.CVAR_NOSET);
- if (ip_sockets[NS_SERVER] == null)
- ip_sockets[NS_SERVER] = NET_Socket(ip.string, (int) port.value);
+ if (ip_sockets[Defines.NS_SERVER] == null)
+ ip_sockets[Defines.NS_SERVER] = NET_Socket(Defines.NS_SERVER,
+ ip.string, (int) port.value);
- if (ip_sockets[NS_CLIENT] == null)
- ip_sockets[NS_CLIENT] = NET_Socket(ip.string, Defines.PORT_ANY);
+ if (ip_sockets[Defines.NS_CLIENT] == null)
+ ip_sockets[Defines.NS_CLIENT] = NET_Socket(Defines.NS_CLIENT,
+ ip.string, Defines.PORT_ANY);
}
/*
- * ==================== NET_OpenIPX ====================
- */
- public static void NET_OpenIPX() {
- }
-
- /*
- * ==================== NET_Config
+ * ==================== NET_Config ====================
*
- * A single player game will only use the loopback code ====================
+ * A single player game will only use the loopback code
*/
public static void Config(boolean multiplayer) {
- int i;
-
- if (!multiplayer) { // shut down any existing sockets
- for (i = 0; i < 2; i++) {
+ if (!multiplayer) {
+ // shut down any existing sockets
+ for (int i = 0; i < 2; i++) {
if (ip_sockets[i] != null) {
ip_sockets[i].close();
ip_sockets[i] = null;
}
- /*
- * if (ipx_sockets[i]) { ipx_sockets[i].close(); ipx_sockets[i] =
- * null; }
- */
}
- } else { // open sockets
+ } else {
+ // open sockets
NET_OpenIP();
}
}
@@ -523,102 +296,49 @@ public final class NET extends Defines {
/*
* ==================== NET_Socket ====================
*/
- public static DatagramSocket NET_Socket(String ip, int port) {
+ public static DatagramSocket NET_Socket(int sock, String ip, int port) {
DatagramSocket newsocket = null;
try {
+ if (ip_channels[sock] == null || !ip_channels[sock].isOpen())
+ ip_channels[sock] = DatagramChannel.open();
+
if (ip == null || ip.length() == 0 || ip.equals("localhost")) {
- if (port == PORT_ANY) {
- newsocket = new DatagramSocket();
+ if (port == Defines.PORT_ANY) {
+ newsocket = ip_channels[sock].socket();
+ newsocket.bind(new InetSocketAddress(0));
} else {
- newsocket = new DatagramSocket(port);
+ newsocket = ip_channels[sock].socket();
+ newsocket.bind(new InetSocketAddress(port));
}
} else {
InetAddress ia = InetAddress.getByName(ip);
- newsocket = new DatagramSocket(port, ia);
+ newsocket = ip_channels[sock].socket();
+ newsocket.bind(new InetSocketAddress(ia, port));
}
+ // nonblocking channel
+ ip_channels[sock].configureBlocking(false);
+ // the socket have to be broadcastable
newsocket.setBroadcast(true);
- // nonblocking (1 ms), 0== neverending infinite timeout
- newsocket.setSoTimeout(1);
} catch (Exception e) {
+ Com.Println(e.getMessage());
newsocket = null;
}
-
return newsocket;
-
- // int newsocket;
- // //struct sockaddr_in address;
- // qboolean _true = true;
- // int i = 1;
- //
- // if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
- // {
- // Com.Printf ("ERROR: UDP_OpenSocket: socket: ", NET_ErrorString());
- // return 0;
- // }
- //
- // // make it non-blocking
- // if (ioctl (newsocket, FIONBIO, &_true) == -1)
- // {
- // Com_Printf ("ERROR: UDP_OpenSocket: ioctl FIONBIO:%s\n",
- // NET_ErrorString());
- // return 0;
- // }
- //
- // // make it broadcast capable
- // if (setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i,
- // sizeof(i)) == -1)
- // {
- // Com_Printf ("ERROR: UDP_OpenSocket: setsockopt SO_BROADCAST:%s\n",
- // NET_ErrorString());
- // return 0;
- // }
- //
- // if (!net_interface || !net_interface[0] || !stricmp(net_interface,
- // "localhost"))
- // address.sin_addr.s_addr = INADDR_ANY;
- // else
- // NET_StringToSockaddr (net_interface, (struct sockaddr *)&address);
- //
- // if (port == PORT_ANY)
- // address.sin_port = 0;
- // else
- // address.sin_port = htons((short)port);
- //
- // address.sin_family = AF_INET;
- //
- // if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
- // {
- // Com_Printf ("ERROR: UDP_OpenSocket: bind: %s\n", NET_ErrorString());
- // close (newsocket);
- // return 0;
- // }
- //
- // return newsocket;
}
/*
* ==================== NET_Shutdown ====================
*/
public static void NET_Shutdown() {
- Config(false); // close sockets
- }
-
- /*
- * ==================== NET_ErrorString ====================
- */
- public static String NET_ErrorString() {
-
- int code;
- //code = errno;
- //return strerror (code);
- return "errno can not yet resolved in java";
+ // close sockets
+ Config(false);
}
// sleeps msec or until net socket is ready
public static void NET_Sleep(int msec) {
- if (ip_sockets[NS_SERVER] == null
+ if (ip_sockets[Defines.NS_SERVER] == null
|| (Globals.dedicated != null && Globals.dedicated.value == 0))
return; // we're not a server, just run full speed
@@ -644,5 +364,4 @@ public final class NET extends Defines {
* &timeout);
*/
}
-
} \ No newline at end of file