summaryrefslogtreecommitdiff
path: root/src/netsession.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2005-03-19 01:20:57 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2005-03-19 01:20:57 +0000
commit49443185e13d937eec0b106aa5d381087a5188c2 (patch)
tree44d57619279de02a951c1e54b5e3ae4cfc482787 /src/netsession.cpp
parenta768f84e87980a48eb25ad95707a44a7eda7f791 (diff)
downloadmanaserv-49443185e13d937eec0b106aa5d381087a5188c2.tar.gz
manaserv-49443185e13d937eec0b106aa5d381087a5188c2.tar.bz2
manaserv-49443185e13d937eec0b106aa5d381087a5188c2.tar.xz
manaserv-49443185e13d937eec0b106aa5d381087a5188c2.zip
Added start of NetSession implementation and basics of initialization, timer
and loop.
Diffstat (limited to 'src/netsession.cpp')
-rw-r--r--src/netsession.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/netsession.cpp b/src/netsession.cpp
index 1c60990e..603ea7f7 100644
--- a/src/netsession.cpp
+++ b/src/netsession.cpp
@@ -22,4 +22,61 @@
*/
#include "netsession.h"
+#include <SDL_net.h>
+NetSession::NetSession()
+{
+}
+
+NetSession::~NetSession()
+{
+ // Stop listening to any ports
+}
+
+void NetSession::startListen(ConnectionHandler *handler, Uint16 port)
+{
+ // Here we will probably need the creation of a listening thread, which
+ // will call connect/disconnect events on the given ConnectionHandler and
+ // will cut incoming data into Packets and send them there too.
+
+ IPaddress address;
+ address.host = INADDR_ANY;
+ address.port = port;
+
+ TCPsocket tcpsock = SDLNet_TCP_Open(address);
+ if (!tcpsock) {
+ printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+ exit(3);
+ }
+}
+
+void NetSession::stopListen(Uint16 port)
+{
+ // Here all open connections on this port will be closed, and listening
+ // thread is stopped.
+
+ // void SDLNet_TCP_Close(TCPsocket sock);
+}
+
+NetComputer *NetSession::connect(const std::string &host, Uint16 port)
+{
+ // Try to connect to given host:port, and return NetComputer objects that
+ // can be used to send messages that way, or NULL when failing to connect.
+ //
+ // An asynchroneous wrapper could be created around this method.
+
+ IPaddress address;
+
+ if (!SDLNet_ResolveHost(&address, host.c_str(), port))
+ {
+ TCPsocket tcpsock = SDLNet_TCP_Open(IPaddress *ip);
+ if (!tcpsock) {
+ printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+ exit(3);
+ }
+ }
+ else {
+ printf("SDLNet_ResolveHost: Could not resolve %s\n", host.c_str());
+ exit(4);
+ }
+}