00001 /* 00002 * netThread.h 00003 * 00004 * Created on: Jan 10, 2012 00005 * Author: sushil 00006 */ 00007 00008 #ifndef NETTHREAD_H_ 00009 #define NETTHREAD_H_ 00010 00011 #include <deque> 00012 #include <map> 00013 00014 //#include <engine.h> 00015 #include <boost/asio.hpp> 00016 #include <boost/thread.hpp> 00017 //#include <boost/date_time/posix_time/posix_time.hpp> 00018 00019 #include <messages.h> 00020 #include <listener.h> 00021 #include <sender.h> 00022 #include <ent.h> 00023 #include <boost/date_time/posix_time/posix_time.hpp> 00024 #include <netCommon.h> 00025 00026 using namespace boost::posix_time; 00027 using boost::asio::ip::udp; 00028 00029 namespace FastEcslent { 00030 00031 class Engine; 00032 00033 class NetThread { 00034 00035 private: 00036 void runThread(); 00037 void sendUnknownEntQueries(); 00038 boost::thread netThread; 00039 int TICK_SLEEP_TIME; 00040 00041 ptime startTime; 00042 bool quit; 00043 00044 std::map<int, State*> unknows; 00045 std::map<int, Entity*> netIdToEntMap; 00046 std::map<Entity*, int> netEntToIdMap; 00047 00048 std::deque<CommandEntity*> commandQueue; 00049 std::deque<SquelchEntity*> squelchQueue; 00050 00051 udp::endpoint myIP; 00052 NetCommon *netCommon; 00053 00054 public: 00055 Engine* engine; 00056 00057 Listener* listener; 00058 Sender* sender; 00059 00060 bool insideLobby; 00061 bool isServer; 00062 int port; 00063 00064 std::string myIPAddress; 00065 boost::posix_time::milliseconds *sleepTime; 00066 00067 NetThread(Engine* eng) { 00068 engine = eng; 00069 } 00070 void run(); 00071 void init(); 00072 void stopAndJoin(); 00073 void stop(); 00074 00075 int getNetId(Entity* ent); 00076 00077 //helpers 00078 void clientTick(); 00079 void handleServerMessages(); 00080 void updateState(Message *m); 00081 void updateState(State *s); 00082 void createEnt(Message *m); 00083 void createEnt(Info *info); 00084 void sendCommands(); 00085 void sendCommand(int id, float dh, float ds); 00086 void squelchEnts(Message *m); 00087 00088 void serverTick(); 00089 void handleClientMessages(); 00090 void handleMessage(Message *m); 00091 void sendInfo(Message *m); 00092 void propagateCommand(Message *m); 00093 00094 void combineCommandsIntoNetMessage(); 00095 void combineSquelchesIntoNetMessage(); 00096 00097 void sendLobbyClientResponse(std::string clientIP, int port, int side, int player); 00098 void lobbyStart(); 00099 00100 void addCommand(CommandEntity *m); 00101 void addSquelch(SquelchEntity *m); 00102 00103 void serve(); 00104 00105 void gameTick(); 00106 }; 00107 00108 } 00109 00110 #endif /* NETTHREAD_H_ */