Changeset 5a64bea in network-game


Ignore:
Timestamp:
Jul 14, 2013, 6:44:29 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
9b5d30b
Parents:
1f1eb58
Message:

Completed initial version of MessageProcessor

Location:
common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • common/Message.h

    r1f1eb58 r5a64bea  
    2121typedef struct
    2222{
    23    short type;
     23   unsigned int id;
     24   unsigned short type;
    2425   char buffer[256];
    2526} NETWORK_MSG;
  • common/MessageProcessor.cpp

    r1f1eb58 r5a64bea  
    11#include "MessageProcessor.h"
    22
    3 int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
    4    return 0;
     3#include <iostream>
     4
     5MessageProcessor::MessageProcessor() {
     6   lastUsedId = 0;
    57}
    68
    7 int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
    8    return 0;
     9MessageProcessor::~MessageProcessor() {
    910}
    1011
    11 void MessageProcessor::resendUnackedMessages() {
     12int MessageProcessor::sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest) {
     13   MessageContainer message(*msg, *dest);
     14   message.id = ++lastUsedId;
     15   sentMessages[message.id] = message;
     16
     17   int ret =  sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
     18
     19   cout << "Send a message of type " << msg->type << endl;
     20
     21   return ret;
     22}
     23
     24int MessageProcessor::receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *source) {
     25   socklen_t socklen = sizeof(struct sockaddr_in);
     26
     27   // assume we don't care about the value of socklen
     28   int ret =  recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, &socklen);
     29
     30   // add id to the NETWORK_MSG struct
     31   if (msg->type == MSG_TYPE_ACK) {
     32      sentMessages.erase(msg->id);
     33   }else {
     34      NETWORK_MSG ack;
     35      ack.id = msg->id;
     36
     37      sendto(sock, (char*)&ack, sizeof(NETWORK_MSG), 0, (struct sockaddr *)source, sizeof(struct sockaddr_in));
     38   }
     39
     40   return ret;
     41}
     42
     43void MessageProcessor::resendUnackedMessages(int sock) {
     44   map<int, MessageContainer>::iterator it;
     45
     46   for(it = sentMessages.begin(); it != sentMessages.end(); it++) {
     47      sendto(sock, (char*)&it->second.msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)&it->second.clientAddr, sizeof(struct sockaddr_in));
     48   }
    1249}
    1350
    1451void MessageProcessor::cleanAckedMessages() {
     52   // shouldn't be needed since I can just remove messages when I get their ACKs
    1553}
  • common/MessageProcessor.h

    r1f1eb58 r5a64bea  
    22#define _MESSAGE_PROCESSOR_H
    33
     4#include <map>
     5
     6#include "Compiler.h"
     7
    48#include "Message.h"
     9
     10#if defined WINDOWS
     11   #include <winsock2.h>
     12   #include <WS2tcpip.h>
     13#elif defined LINUX
     14   #include <netinet/in.h>
     15#endif
    516
    617/*
     
    2839*/
    2940
     41using namespace std;
     42
    3043class MessageProcessor {
    3144public:
     45   MessageProcessor();
     46   ~MessageProcessor();
     47
    3248   int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
    3349   int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
    34    void resendUnackedMessages();
     50   void resendUnackedMessages(int sock);
    3551   void cleanAckedMessages();
    3652
    3753private:
     54   // this should eventually just replace the Message struct
     55   class MessageContainer {
     56   public:
     57      MessageContainer() {
     58      }
     59
     60      MessageContainer(const MessageContainer& mc) {
     61         this->id = mc.id;
     62         this->clientAddr = mc.clientAddr;
     63         this->msg = mc.msg;
     64         this->ackReceived = mc.ackReceived;
     65      }
     66
     67      MessageContainer(NETWORK_MSG msg, struct sockaddr_in clientAddr) {
     68         this->clientAddr = clientAddr;
     69         this->msg = msg;
     70      }
     71
     72      ~MessageContainer() {
     73      }
     74
     75      int id;
     76      struct sockaddr_in clientAddr;
     77      NETWORK_MSG msg;
     78      bool ackReceived;
     79   };
     80
     81   int lastUsedId;
     82   map<int, MessageContainer> sentMessages;
    3883};
    3984
Note: See TracChangeset for help on using the changeset viewer.