summaryrefslogtreecommitdiff
path: root/src/net/network.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/network.h')
-rw-r--r--src/net/network.h86
1 files changed, 61 insertions, 25 deletions
diff --git a/src/net/network.h b/src/net/network.h
index db95d7c3..75bde584 100644
--- a/src/net/network.h
+++ b/src/net/network.h
@@ -24,41 +24,77 @@
#ifndef _TMW_NETWORK_
#define _TMW_NETWORK_
-#define NET_ERROR -1
-#define NET_CONNECTED 0
-#define NET_IDLE 1
-#define NET_CONNECTING 2
-#define NET_DATA 3
+#include <map>
+#include <SDL_net.h>
+#include <SDL_thread.h>
+class MessageHandler;
class MessageIn;
-/** Convert an address from int format to string */
-char *iptostring(int address);
+class Network;
-/** Open a session with a server */
-void openConnection(const char* address, short port);
+class Network
+{
+ public:
+ friend int networkThread(void *data);
+ friend class MessageOut;
-/** Returns the status of the current connection attempt. */
-int pollConnection();
+ Network();
+ ~Network();
-/** Close a session */
-void closeConnection();
+ bool connect(const char *address, short port);
+ void disconnect();
-/** Send and receive data waiting in the buffers */
-void flush();
+ void registerHandler(MessageHandler *handler);
+ void unregisterHandler(MessageHandler *handler);
+ void clearHandlers();
-/** Check if a packet is complete */
-bool packetReady();
+ int getState() const { return mState; }
+ bool isConnected() const { return mState == CONNECTED; }
-/**
- * Returns the next arriving message, waiting for it if necessary.
- */
-MessageIn get_next_message();
-extern char *out;
+ int getInSize() const { return mInSize; }
+
+ void skip(int len);
+
+ bool messageReady();
+ MessageIn getNextMessage();
+
+ void dispatchMessages();
+ void flush();
+
+ enum {
+ IDLE,
+ CONNECTED,
+ CONNECTING,
+ DATA,
+ ERROR
+ };
+
+ protected:
+ Uint16 readWord(int pos);
+
+ TCPsocket mSocket;
-void skip(int len);
+ char *mAddress;
+ short mPort;
-extern unsigned int in_size; /**< Amount of data in input buffer. */
-extern unsigned int out_size; /**< Amount of data in output buffer. */
+ char *mInBuffer, *mOutBuffer;
+ unsigned int mInSize, mOutSize;
+
+ unsigned int mToSkip;
+
+ int mState;
+
+ SDL_Thread *mWorkerThread;
+ SDL_mutex *mMutex;
+
+ std::map<Uint16, MessageHandler*> mMessageHandlers;
+
+ bool realConnect();
+ void receive();
+};
+
+/** Convert an address from int format to string */
+char *iptostring(int address);
#endif