|
From: <mrv...@us...> - 2007-06-02 09:09:10
|
Revision: 27
http://svn.sourceforge.net/dawnofinfinity/?rev=27&view=rev
Author: mrvacbob
Date: 2007-06-02 02:09:06 -0700 (Sat, 02 Jun 2007)
Log Message:
-----------
more net
Modified Paths:
--------------
trunk/net.cpp
trunk/net.h
trunk/set.h
Modified: trunk/net.cpp
===================================================================
--- trunk/net.cpp 2007-06-02 08:57:18 UTC (rev 26)
+++ trunk/net.cpp 2007-06-02 09:09:06 UTC (rev 27)
@@ -21,14 +21,52 @@
#include "net.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <sys/errno.h>
+#include <sys/time.h>
+#include <stdio.h>
+
+Connection::Connection(int fd, ConnectionGroup *parent) : sock(fd), parent(parent)
+{
+ curp = curp_udp = NULL;
+ recv_remain = recv_done = send_remain = send_done = 0;
+ recievebuf = (Packet)GC_malloc(2);
+ ccontext = NULL;
+
+ fcntl(sock,F_SETFL,O_NONBLOCK);
+
+ sock_udp = socket(AF_INET6,SOCK_DGRAM,IPPROTO_UDP);
+
+ if (sock_udp == -1) {
+ sock_udp = socket(AF_INET,SOCK_STREAM,IPPROTO_UDP);
+ }
+
+ // XXX here we getpeerinfo the tcp peer and connect() udp to it
+}
+
+Connection::~Connection()
+{
+ close(sock);
+ close(sock_udp);
+
+ FD_CLR(sock, &parent->parent->active_fds);
+ FD_CLR(sock_udp, &parent->parent->active_fds);
+}
+
void Connection::enqueue(Packet p, enum tcp_udp pt)
{
(pt == p_tcp ? transmitq_tcp : transmitq_udp).push(p);
FD_SET((pt == p_tcp ? sock : sock_udp), &parent->parent->writing_fds);
}
-void Connection::task()
+int Connection::task()
{
+ return 0;
}
ConnectionGroup::ConnectionGroup(ConnCallback *cc, Net *parent) : callback(cc), parent(parent)
@@ -41,6 +79,9 @@
void ConnectionGroup::broadcastexceptfor(Packet p, Connection *c, enum tcp_udp pt)
{
+ SETPERFORM_START(&connections, Connection, cc)
+ if (c != &cc) cc.enqueue(p, pt);
+ SETPERFORM_END();
}
void ConnectionGroup::broadcast(Packet p, enum tcp_udp pt)
@@ -48,12 +89,48 @@
broadcastexceptfor(p, NULL, pt);
}
-Net::Net(const char *port, NewConnCallback *cc) : port(port), ncc(cc)
+void Net::setup_listener(const char *port)
{
+ static const struct addrinfo hints = {AI_PASSIVE|AI_NUMERICHOST,PF_INET6,SOCK_STREAM,IPPROTO_TCP,0,NULL,NULL,NULL};
+ static const struct addrinfo hints_v4 = {AI_PASSIVE|AI_NUMERICHOST,PF_INET,SOCK_STREAM,IPPROTO_TCP,0,NULL,NULL,NULL};
+
+ bool v6 = true;
+ int so = socket(AF_INET6,SOCK_STREAM,IPPROTO_TCP);
+ if (so == -1) {
+ so = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
+ v6 = false;
+ }
+
+ int x = 1;
+ setsockopt(so,IPPROTO_TCP,TCP_NODELAY,&x,sizeof(x));
+
+ struct addrinfo *res;
+ getaddrinfo(v6?"::":NULL, port, v6 ? (&hints) : (&hints_v4), &res);
+
+ if (bind(so,res->ai_addr,res->ai_addrlen) == -1) {
+ perror("bind");
+ abort();
+ };
+
+ freeaddrinfo(res);
+
+ fcntl(so,F_SETFL,O_NONBLOCK);
+ listen(so,SOMAXCONN);
+ lsock = highfd = so;
+ FD_SET(so, &active_fds);
}
+Net::Net(const char *port, NewConnCallback *cc) : ncc(cc)
+{
+ bzero(&active_fds, sizeof(fd_set));
+ bzero(&writing_fds, sizeof(fd_set));
+
+ setup_listener(port);
+}
+
Net::~Net()
{
+ close(lsock);
}
void Net::addgroup(ConnCallback *cc)
Modified: trunk/net.h
===================================================================
--- trunk/net.h 2007-06-02 08:57:18 UTC (rev 26)
+++ trunk/net.h 2007-06-02 09:09:06 UTC (rev 27)
@@ -44,9 +44,9 @@
typedef struct packetinternals *Packet;
-struct ConnCallbackContext {};
+struct ConnCallbackContext : public Object {};
-struct ConnCallback {
+struct ConnCallback : public Object {
virtual void handlepacket(Packet *p) = 0;
};
@@ -66,13 +66,16 @@
Packet recievebuf;
- struct ConnCallbacKContext *ccontext;
+ struct ConnCallbackContext *ccontext;
+ Connection(int fd, ConnectionGroup *parent);
+ ~Connection();
+
void enqueue(Packet p, enum tcp_udp pt = p_tcp);
- void task();
+ int task();
};
-struct NewConnCallback {
+struct NewConnCallback : public Object {
virtual void handleconn(Connection *c) = 0;
};
@@ -100,7 +103,7 @@
int lsock;
long highfd;
- const char *port; // what does this do
+ //const char *port; // what does this do
NewConnCallback *ncc;
@@ -111,6 +114,9 @@
void addgroup(ConnCallback *cc);
void task();
+
+private:
+ void Net::setup_listener(const char *port);
};
#endif
\ No newline at end of file
Modified: trunk/set.h
===================================================================
--- trunk/set.h 2007-06-02 08:57:18 UTC (rev 26)
+++ trunk/set.h 2007-06-02 09:09:06 UTC (rev 27)
@@ -36,7 +36,7 @@
{\
Set<type> *_ins2 = (Set<type> *)_inset;\
for (size_t _set_i = 0;_set_i<_ins2->filled;) {\
- const type varname = _ins2->data[_set_i++]; \
+ type &varname = _ins2->data[_set_i++]; \
/// A hack to allow the same block of code to operate on two sets without code duplication.
#define TWOSETSPERFORM_START(_inset, _inset2, type, varname) \
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|