Imported more code from the old engine.
[peakengine.git] / engine / include / network / Connection.h
blobaebf3ac7db4a6f59f61bf746745c31b3a5781f46
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef _CONNECTION_H_
23 #define _CONNECTION_H_
25 #include "core/Buffer.h"
27 #include <enet/enet.h>
28 #include <queue>
30 //tolua_begin
31 namespace peak
33 /**
34 * \brief Base class for any client/server connection
36 class Connection
38 public:
39 Connection(ENetPeer *peer);
40 virtual ~Connection();
42 /**
43 * \brief Destroys the connection and frees all data.
45 virtual void destroy(void);
47 /**
48 * \brief Closes the connection.
50 virtual void disconnect(void);
51 /**
52 * \brief Reads whether the connection is established or not.
53 * \return Returns false if the connection was closed.
55 virtual bool isConnected(void);
57 /**
58 * \brief Returns whether there is incoming data waiting to be read.
59 * \return Returns true if there is incoming data.
61 virtual bool hasNewData(void);
62 /**
63 * \brief Reads incoming data.
65 * This function returns a buffer which has to be deleted by the user.
66 * \return Incoming data
68 virtual Buffer *readData(void);
69 /**
70 * \brief Sends data.
72 * You must not delete the buffer for the connection takes care of it.
73 * \param data Pointer to a Buffer created via new
74 * \param reliable If set to true, a reliable connection is used
75 * \return Returns false if the data could not be sent.
77 virtual bool sendData(Buffer *data, bool reliable = true);
79 /**
80 * \brief Injects incoming data.
82 * Because of the design of enet, connections do not receive the data
83 * themselves.
85 * For internal use only.
86 * \param data Incoming data
88 virtual void injectData(Buffer *data);
89 /**
90 * \brief Returns the peer structure used by enet.
92 * For internal use only.
93 * \return Peer structure
95 virtual ENetPeer *getPeer(void);
96 private:
97 //tolua_end
98 ENetPeer *peer;
100 std::queue<Buffer*> received;
101 //tolua_begin
104 //tolua_end
106 #endif