1 | // |
---|
2 | // C++ Implementation: ClientInformation |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: <>, (C) 2007 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | #include "ClientInformation.h" |
---|
13 | |
---|
14 | namespace network { |
---|
15 | |
---|
16 | ClientInformation::ClientInformation() |
---|
17 | { |
---|
18 | gamestateID_=GAMESTATEID_INITIAL; |
---|
19 | preve=0; |
---|
20 | nexte=0; |
---|
21 | this->head=false; |
---|
22 | } |
---|
23 | |
---|
24 | ClientInformation::ClientInformation(bool head) |
---|
25 | { |
---|
26 | gamestateID_=GAMESTATEID_INITIAL; |
---|
27 | preve=0; |
---|
28 | nexte=0; |
---|
29 | this->head=head; |
---|
30 | } |
---|
31 | // |
---|
32 | |
---|
33 | // ClientInformation::ClientInformation(ClientInformation *prev){ |
---|
34 | // if(prev->next()!=0){ |
---|
35 | // this->nexte=prev->next(); |
---|
36 | // this->nexte->setPrev(this); |
---|
37 | // } |
---|
38 | // else |
---|
39 | // this->nexte = 0; |
---|
40 | // prev->setNext(this); |
---|
41 | // this->preve = pref; |
---|
42 | // } |
---|
43 | // |
---|
44 | // ClientInformation::ClientInformation(ClientInformation *prev, ClientInformation *next){ |
---|
45 | // this->nexte = next; |
---|
46 | // this->preve = prev; |
---|
47 | // this->preve->setNext(this); |
---|
48 | // this->nexte->setPrev(this); |
---|
49 | // } |
---|
50 | |
---|
51 | ClientInformation::~ClientInformation() |
---|
52 | { |
---|
53 | if(preve!=0) |
---|
54 | preve->setNext(this->nexte); |
---|
55 | if(nexte!=0) |
---|
56 | nexte->setPrev(this->preve); |
---|
57 | } |
---|
58 | |
---|
59 | ClientInformation *ClientInformation::next(){ |
---|
60 | if(this!=0) |
---|
61 | return this->nexte; |
---|
62 | else |
---|
63 | return 0; |
---|
64 | } |
---|
65 | ClientInformation *ClientInformation::prev(){ |
---|
66 | if(this!=0) |
---|
67 | return this->preve; |
---|
68 | else |
---|
69 | return 0; |
---|
70 | } |
---|
71 | |
---|
72 | bool ClientInformation::setPrev(ClientInformation *prev){ |
---|
73 | if(!head) |
---|
74 | this->preve = prev; |
---|
75 | else |
---|
76 | return false; |
---|
77 | return true; |
---|
78 | } |
---|
79 | |
---|
80 | bool ClientInformation::setNext(ClientInformation *next){ |
---|
81 | this->nexte = next; |
---|
82 | return true; |
---|
83 | } |
---|
84 | |
---|
85 | ClientInformation *ClientInformation::insertAfter(ClientInformation *ins){ |
---|
86 | this->nexte->setPrev(ins); |
---|
87 | ins->setNext(this->nexte); |
---|
88 | ins->setPrev(this); |
---|
89 | this->nexte = ins; |
---|
90 | return ins; |
---|
91 | } |
---|
92 | |
---|
93 | ClientInformation *ClientInformation::insertBefore(ClientInformation *ins){ |
---|
94 | this->prev()->setNext(ins); |
---|
95 | ins->setPrev(this->preve); |
---|
96 | this->preve=ins; |
---|
97 | ins->setNext(this); |
---|
98 | return ins; |
---|
99 | } |
---|
100 | |
---|
101 | void ClientInformation::setID(int clientID){ |
---|
102 | clientID_ = clientID; |
---|
103 | } |
---|
104 | void ClientInformation::setPeer(ENetPeer *peer){ |
---|
105 | peer_ = peer; |
---|
106 | } |
---|
107 | |
---|
108 | void ClientInformation::setGamestateID(int id){ |
---|
109 | gamestateID_=id; |
---|
110 | } |
---|
111 | |
---|
112 | int ClientInformation::getID(){ |
---|
113 | return clientID_; |
---|
114 | } |
---|
115 | ENetPeer *ClientInformation::getPeer(){ |
---|
116 | return peer_; |
---|
117 | } |
---|
118 | |
---|
119 | int ClientInformation::getGamestateID(){ |
---|
120 | return gamestateID_; |
---|
121 | } |
---|
122 | |
---|
123 | ClientInformation *ClientInformation::insertBack(ClientInformation *ins){ |
---|
124 | ClientInformation *temp = this; |
---|
125 | while(temp->next()!=0){ |
---|
126 | temp = temp->next(); |
---|
127 | } |
---|
128 | temp->setNext(ins); |
---|
129 | ins->setPrev(temp); |
---|
130 | return ins; |
---|
131 | } |
---|
132 | |
---|
133 | bool ClientInformation::removeClient(int clientID){ |
---|
134 | ClientInformation *temp = this; |
---|
135 | while(temp!=0 && temp->getID()!=clientID) |
---|
136 | temp = temp->next(); |
---|
137 | if(temp==0) |
---|
138 | return false; |
---|
139 | delete temp; |
---|
140 | return true; |
---|
141 | } |
---|
142 | |
---|
143 | bool ClientInformation::removeClient(ENetPeer *peer){ |
---|
144 | ClientInformation *temp = this; |
---|
145 | while(temp!=0 && (temp->getPeer()->address.host!=peer->address.host || temp->getPeer()->address.port!=peer->address.port)) |
---|
146 | temp = temp->next(); |
---|
147 | if(temp==0) |
---|
148 | return false; |
---|
149 | delete temp; |
---|
150 | return true; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * This function goes forward through the list and looks for an element with clientID |
---|
155 | * This function should only be applied to the head of the list |
---|
156 | * @param clientID id to look for |
---|
157 | * @return pointer to the element in the list or 0 if the search was unsuccessfull |
---|
158 | */ |
---|
159 | ClientInformation *ClientInformation::findClient(int clientID, bool look_backwards){ |
---|
160 | ClientInformation *temp = this; |
---|
161 | while(temp!=0 && temp->getID()!=clientID) |
---|
162 | temp = temp->next(); |
---|
163 | // returns 0 if nothing has been found |
---|
164 | return temp; |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * This function goes forward through the list and looks for an element with clientID |
---|
169 | * This function should only be applied to the head of the list |
---|
170 | * @param peer peer to look for |
---|
171 | * @return pointer to the element in the list |
---|
172 | */ |
---|
173 | ClientInformation *ClientInformation::findClient(ENetAddress *address, bool look_backwards){ |
---|
174 | ClientInformation *temp = this; |
---|
175 | while(temp!=0 && (temp->getPeer()->address.host!=address->host || temp->getPeer()->address.port != address->port)) |
---|
176 | temp = temp->next(); |
---|
177 | // returns 0 if nothing has been found |
---|
178 | return temp; |
---|
179 | } |
---|
180 | |
---|
181 | } |
---|