Changeset 46d6469 in network-game
- Timestamp:
- Jul 26, 2013, 6:28:39 PM (11 years ago)
- Branches:
- master
- Children:
- 9fe1807
- Parents:
- 411c1ae
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
common/MessageProcessor.cpp
r411c1ae r46d6469 13 13 14 14 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) { 15 cout << "Sending message of type " << msg->type << endl; 16 15 17 msg->id = ++lastUsedId; 16 18 MessageContainer message(*msg, *dest); 17 19 sentMessages[msg->id][dest->sin_addr.s_addr] = message; 18 20 19 cout << "Sending message" << endl; 20 cout << "id: " << msg->id << endl; 21 cout << "type: " << msg->type << endl; 22 cout << "buffer: " << msg->buffer << endl; 21 sentMessages[msg->id][dest->sin_addr.s_addr] = message; 23 22 24 23 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in)); 25 26 cout << "Send a message of type " << msg->type << endl;27 24 28 25 return ret; … … 38 35 return ret; 39 36 40 cout << "Received message" << endl;41 42 37 // add id to the NETWORK_MSG struct 43 38 if (msg->type == MSG_TYPE_ACK) { 44 cout << "Received ack" << endl; 45 if (!sentMessages[msg->id][source->sin_addr.s_addr].isAcked) { 46 cout << "Received new ack" << endl; 47 sentMessages[msg->id][source->sin_addr.s_addr].isAcked = true; 48 sentMessages[msg->id][source->sin_addr.s_addr].timeAcked = getCurrentMillis(); 39 if (!sentMessages[msg->id][source->sin_addr.s_addr].getAcked()) { 40 sentMessages[msg->id][source->sin_addr.s_addr].setAcked(true); 41 sentMessages[msg->id][source->sin_addr.s_addr].setTimeAcked(getCurrentMillis()); 49 42 } 50 43 … … 52 45 }else { 53 46 bool isDuplicate = false; 54 55 cout << "Received real message" << endl;56 cout << "id: " << msg->id << endl;57 cout << "type: " << msg->type << endl;58 cout << "buffer: " << msg->buffer << endl;59 47 60 48 if (ackedMessages.find(msg->id) != ackedMessages.end()) … … 84 72 sentMsg = it->second; 85 73 for (it2 = sentMsg.begin(); it2 != sentMsg.end(); it2++) { 86 sendto(sock, (char*)&it2->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in)); 74 if (!(it2->second.getAcked())) { 75 sendto(sock, it2->second.getMessage(), sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it2->first, sizeof(struct sockaddr_in)); 76 } 87 77 } 88 78 } … … 95 85 while (it != sentMessages.end()) { 96 86 it2 = it->second.begin(); 97 while (it2 != it->second. begin()) {98 if (it2->second. isAcked) {99 if ((getCurrentMillis() - it2->second. timeAcked) > 1000)87 while (it2 != it->second.end()) { 88 if (it2->second.getAcked()) { 89 if ((getCurrentMillis() - it2->second.getTimeAcked()) > 1000) 100 90 it->second.erase(it2++); 101 91 else … … 114 104 115 105 while (it3 != ackedMessages.end()) { 116 if ((getCurrentMillis() - it3->second) > 500) {106 if ((getCurrentMillis() - it3->second) > 500) 117 107 ackedMessages.erase(it3++); 118 cout << "Deleting ack record" << endl; 119 }else 108 else 120 109 it3++; 121 110 } -
common/MessageProcessor.h
r411c1ae r46d6469 4 4 #include <map> 5 5 6 #include "Compiler.h" 7 8 #if defined WINDOWS 9 #include <winsock2.h> 10 #include <WS2tcpip.h> 11 #elif defined LINUX 12 #include <netinet/in.h> 13 #endif 14 15 #define MSG_TYPE_ACK 1 16 #define MSG_TYPE_REGISTER 2 17 #define MSG_TYPE_LOGIN 3 18 #define MSG_TYPE_LOGOUT 4 19 #define MSG_TYPE_CHAT 5 20 #define MSG_TYPE_PLAYER 6 // server sends this to update player positions 21 #define MSG_TYPE_PLAYER_MOVE 7 // client sends this when a player wants to move 22 #define MSG_TYPE_OBJECT 8 23 #define MSG_TYPE_REMOVE_OBJECT 9 24 #define MSG_TYPE_PICKUP_FLAG 10 25 #define MSG_TYPE_DROP_FLAG 11 26 #define MSG_TYPE_SCORE 12 27 #define MSG_TYPE_START_ATTACK 13 28 #define MSG_TYPE_ATTACK 14 29 #define MSG_TYPE_PROJECTILE 15 30 #define MSG_TYPE_REMOVE_PROJECTILE 16 31 32 typedef struct 33 { 34 unsigned int id; 35 unsigned short type; 36 char buffer[256]; 37 } NETWORK_MSG; 6 #include "MessageContainer.h" 38 7 39 8 using namespace std; 40 9 41 10 class MessageProcessor { 11 private: 12 int lastUsedId; 13 14 // map from message ids to maps from player addresses to message info 15 map<int, map<unsigned long, MessageContainer> > sentMessages; 16 17 // map from message ids to the time each mesage was acked 18 map<unsigned int, unsigned long long> ackedMessages; 19 20 unsigned long pid; 21 42 22 public: 43 23 MessageProcessor(); … … 48 28 void resendUnackedMessages(int sock); 49 29 void cleanAckedMessages(); 50 51 private:52 // this should eventually just replace the Message struct53 class MessageContainer {54 public:55 MessageContainer() {56 }57 58 MessageContainer(const MessageContainer& mc) {59 this->msg = mc.msg;60 this->clientAddr = mc.clientAddr;61 }62 63 MessageContainer(NETWORK_MSG msg, struct sockaddr_in clientAddr) {64 this->clientAddr = clientAddr;65 this->msg = msg;66 }67 68 ~MessageContainer() {69 }70 71 NETWORK_MSG msg;72 struct sockaddr_in clientAddr;73 bool isAcked;74 unsigned long long timeAcked;75 };76 77 int lastUsedId;78 79 // map from message ids to maps from player addresses to message info80 map<int, map<unsigned long, MessageContainer> > sentMessages;81 82 // map from message ids to the time each mesage was acked83 map<unsigned int, unsigned long long> ackedMessages;84 30 }; 85 31 -
server/makefile
r411c1ae r46d6469 3 3 FLAGS = $(LIB_FLAGS) 4 4 COMMON_PATH = ../common 5 DEPENDENCIES = Common.o Message Processor.o Player.o WorldMap.o DataAccess.o Projectile.o5 DEPENDENCIES = Common.o MessageContainer.o MessageProcessor.o Player.o WorldMap.o DataAccess.o Projectile.o 6 6 7 7 server : server.cpp $(DEPENDENCIES) … … 9 9 10 10 Common.o : $(COMMON_PATH)/Common.cpp 11 $(CC) -c -o $@ $? 12 13 MessageContainer.o : $(COMMON_PATH)/MessageContainer.cpp 11 14 $(CC) -c -o $@ $? 12 15
Note:
See TracChangeset
for help on using the changeset viewer.