Changeset 196 for code/branches
- Timestamp:
- Nov 11, 2007, 5:11:36 PM (17 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 3 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/ConnectionManager.cc
r173 r196 7 7 // 8 8 // 9 // Author: Dumeni, Oli9 // Author: Oliver Scheuss 10 10 // 11 11 12 // enet library for networking support13 #include enet/enet.h14 // boost.thread library for multithreading support15 #include boost/thread/thread.hpp16 // headerfile17 12 #include "network/ConnectionManager.h" 18 13 19 14 namespace network{ 15 16 boost::thread_group network_threads; 17 18 void test(){ 19 return; 20 } 21 20 22 ConnectionManager::ConnectionManager(){ 21 23 quit=false; 22 24 client=NULL; 23 bindAddress.host = std::ENET_HOST_ANY;25 bindAddress.host = ENET_HOST_ANY; 24 26 bindAddress.port = NETWORK_PORT; 25 27 } … … 32 34 } 33 35 34 void ConnectionManager::loop(){ 35 if(!quit && !buffer.isEmpty()){ 36 // to be implemented ================= 37 38 } 39 } 40 41 bool ConnectionManager::createListener(){ 42 threads.create_thread(&peerLoop); 43 return true; 44 } 45 46 bool ConnectionManager::quitServer(){ 36 37 ENetPacket *ConnectionManager::getPacket(){ 38 if(!buffer.isEmpty()) 39 return buffer.pop(); 40 else 41 return NULL; 42 } 43 44 bool ConnectionManager::queueEmpty(){ 45 return buffer.isEmpty(); 46 } 47 48 void ConnectionManager::createListener(){ 49 network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); 50 // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); 51 return; 52 } 53 54 bool ConnectionManager::quitListener(){ 47 55 quit=true; 48 threads.join_all(); 49 return true; 50 } 51 52 bool ConnectionManager::peerLoop(){ 56 network_threads.join_all(); 57 return true; 58 } 59 60 bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){ 61 if(client=NULL) 62 return false; 63 ClientList *temp=client; 64 while(peer->host != temp->event->peer->host){ 65 temp=temp->next; 66 if(temp==NULL) 67 return false; 68 } 69 if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) 70 return false; 71 return true; 72 } 73 74 bool ConnectionManager::addPacket(ENetPacket *packet, int ID){ 75 if(client=NULL) 76 return false; 77 ClientList *temp=client; 78 while(ID != temp->ID){ 79 temp=temp->next; 80 if(temp==NULL) 81 return false; 82 } 83 if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) 84 return false; 85 else 86 return true; 87 } 88 89 bool ConnectionManager::addPacketAll(ENetPacket *packet){ 90 ClientList *temp=client; 91 while(temp!=NULL){ 92 if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0) 93 return false; 94 } 95 return true; 96 } 97 98 bool ConnectionManager::sendPackets(ENetEvent *event){ 99 if(server==NULL) 100 return false; 101 if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) 102 return true; 103 else 104 return false; 105 } 106 107 void ConnectionManager::receiverThread(){ 108 // what about some error-handling here ? 109 enet_initialize(); 110 atexit(enet_deinitialize); 53 111 ENetEvent event; 54 112 server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); … … 65 123 switch(event.type){ 66 124 // log handling ================ 67 case std::EVENT_TYPE_CONNECT:68 addClient( event);125 case ENET_EVENT_TYPE_CONNECT: 126 addClient(&event); 69 127 break; 70 case std::EVENT_TYPE_RECEIVE: 71 processData(event); 128 case ENET_EVENT_TYPE_RECEIVE: 129 std::cout << event.packet->data << std::endl; 130 processData(&event); 72 131 break; 73 case std::EVENT_TYPE_DISCONNECT:132 case ENET_EVENT_TYPE_DISCONNECT: 74 133 // add some error/log handling here 75 134 clientDisconnect(event.peer); … … 81 140 } 82 141 83 ConnectionManager::processData(ENetEvent event){ 84 // insert packetdecode and buffer stuff here 85 86 } 87 88 bool ConnectionManager::clientDisconnect(ENetPeer peer){ 89 ClientList *temp=client; 90 if(temp->event.peer==peer){ 142 bool ConnectionManager::processData(ENetEvent *event){ 143 // just add packet to the buffer 144 // this can be extended with some preprocessing 145 return buffer.push(event->packet); 146 } 147 148 bool ConnectionManager::clientDisconnect(ENetPeer *peer){ 149 ClientList *temp=client; 150 // do we have to remove the first item ? 151 if(temp->event->peer->host==peer->host){ 91 152 if(temp->next==NULL){ 92 153 client=NULL; … … 98 159 } else { 99 160 while(temp->next!=NULL){ 100 if(temp->next->event.peer==peer){ 161 if(temp->next->event->peer->host==peer->host){ 162 // do a correct relink and delete the disconnected client 101 163 ClientList *temp2=temp->next; 102 164 temp->next=temp2->next; … … 104 166 return true; 105 167 } else 168 //otherwise keep on searching ;) 106 169 temp=temp->next; 107 170 } … … 110 173 } 111 174 112 bool ConnectionManager::addClient(ENetEvent event){ 175 bool ConnectionManager::addClient(ENetEvent *event){ 176 177 // first client? 113 178 if(client==NULL){ 114 179 client =new ClientList; … … 117 182 client->event = event; 118 183 } else { 119 ClienList *temp = client; 184 // otherwise add a new element to clientlist 185 ClientList *temp = client; 120 186 int i=1; 121 187 while(temp->next != NULL){ … … 132 198 } 133 199 134 void ConnectionManager::test(){ 135 while(!quit) 136 137 138 } 200 139 201 } -
code/branches/network/src/network/ConnectionManager.h
r187 r196 5 5 // 6 6 // 7 // Author: <>, (C) 20077 // Author: Oliver Scheuss, (C) 2007 8 8 // 9 9 // Copyright: See COPYING file that comes with this distribution … … 13 13 #define NETWORK_CONNECTIONMANAGER_H 14 14 15 #include PacketBuffer.h 15 #include <iostream> 16 // enet library for networking support 17 #include <enet/enet.h> 18 // boost.thread library for multithreading support 19 #include <boost/thread/thread.hpp> 20 #include <boost/bind.hpp> 21 // headerfile 22 #include "network/ConnectionManager.h" 23 #include "network/PacketBuffer.h" 16 24 17 25 namespace network{ … … 20 28 #define NETWORK_MAX_CONNECTIONS 50 21 29 #define NETWORK_WAIT_TIMEOUT 5000 30 #define NETWORK_SEND_WAIT 5 22 31 //just some defines to make life easier ;) 23 #define ENetEvent std::ENetEvent24 #define ENetHost std::ENetHost25 #define ENetAddress std::ENetAddress26 #define ENetPeer std::ENetPeer32 // #define ENetEvent std::ENetEvent 33 // #define ENetHost std::ENetHost 34 // #define ENetAddress std::ENetAddress 35 // #define ENetPeer std::ENetPeer 27 36 28 37 struct ClientList{ 29 ENetEvent event;38 ENetEvent *event; 30 39 int ID; 31 40 ClientList *next; … … 35 44 public: 36 45 ConnectionManager(); 37 ~ConnectionManager(); 38 void loop(); // thread1 39 bool createListener(); 40 bool quitServer(); 41 bool sendEventAll(/*.....*/); 42 bool sendEvent(int clientID/*.....*/); 43 void test(); 46 ConnectionManager(int port, int address); 47 ENetPacket *getPacket(); // thread1 48 // check wheter the packet queue is empty 49 bool queueEmpty(); 50 // create a new listener thread 51 void createListener(); 52 bool quitListener(); 53 // add a packet to queue for a specific client peer 54 bool addPacket(ENetPacket *packet, ENetPeer *peer); 55 // add ad packet to queue for a specific client ID 56 bool addPacket(ENetPacket *packet, int ID); 57 // add a packet to queue for all clients 58 bool addPacketAll(ENetPacket *packet); 59 // send out all queued packets 60 bool sendPackets(ENetEvent *event); 44 61 private: 45 void peerLoop(int clientNumber); //thread2 46 bool clientDisconnect(ENetPeer peer); 47 bool processData(ENetEvent event); 48 bool addClient(ENetEvent event); 62 bool clientDisconnect(ENetPeer *peer); 63 bool processData(ENetEvent *event); 64 bool addClient(ENetEvent *event); 65 // implementation of the listener 66 void receiverThread(); //thread2 49 67 //packetbuffer 50 68 PacketBuffer buffer; … … 54 72 // quit-variable (communication with threads) 55 73 bool quit; 56 // thread group57 boost::thread_group threads;58 74 // clientlist 59 75 ClientList *client; 76 // thread group 77 // boost::thread_group threads; 60 78 }; 61 79 -
code/branches/network/src/network/PacketBuffer.cc
r188 r196 1 1 // C++ PacketBuffer 2 // 2 // d 3 3 // Author Oliver Scheuss 4 5 #ifndef NETWORK_PACKETBUFFER_CC 6 #define NETWORK_PACKETBUFFER_CC 7 4 8 #include <iostream> 5 9 #include "network/PacketBuffer.h" 6 10 7 11 namespace network{ 8 12 13 boost::mutex networkPacketBufferMutex; 14 9 15 PacketBuffer::PacketBuffer(){ 10 16 closed=false; … … 12 18 last=NULL; 13 19 } 20 //this is needed in order to make the packetbuffer threadsafe 21 14 22 15 23 bool PacketBuffer::push(ENetPacket *pck){ … … 79 87 80 88 }// namespace network 89 90 #endif -
code/branches/network/src/network/PacketBuffer.h
r188 r196 19 19 #include <boost/bind.hpp> 20 20 #include <boost/thread/mutex.hpp> 21 #include <boost/thread/mutex.hpp>22 21 23 //this is needed in order to make the packetbuffer threadsafe24 boost::mutex networkPacketBufferMutex;25 22 26 23 namespace network{ 24 27 25 28 26 struct PacketEnvelope{ … … 47 45 // pushs a packet to the queue 48 46 bool push(ENetPacket *pck); 47 49 48 private: 50 49 QueueItem *first; … … 52 51 bool closed; 53 52 54 //make it threadsafe55 // boost::mutex mutex;56 53 }; 57 54
Note: See TracChangeset
for help on using the changeset viewer.