本文整理汇总了C++中TCPSocket::fromAddr方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket::fromAddr方法的具体用法?C++ TCPSocket::fromAddr怎么用?C++ TCPSocket::fromAddr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TCPSocket
的用法示例。
在下文中一共展示了TCPSocket::fromAddr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: openSession
void Dispatcher::openSession(TCPSocket* peer, const string& data){
if (isLoggedIn(peer)){
if (loggedInUsers.find(data) != loggedInUsers.end()) {
TCPSocket *peerB = loggedInUsers[data]; // find second peer according to the data
//if peer was found and client did not open session with himself then allow session
if ((peerB != NULL && peer != peerB) && !(std::find(peers.begin(),peers.end(),peerB) == peers.end()))
{
//get the username from the peers
string peerName, peerBName;
for (map<string, TCPSocket *>::iterator itr = loggedInUsers.begin();
itr != loggedInUsers.end(); ++itr) {
if (itr->second == peer) {
peerName = itr->first;
}
if (itr->second == peerB) {
peerBName = itr->first;
}
}
//tell peerB to change sessionIsActive=true
TCPMessengerProtocol::sendToServer(SESSION_ESTABLISHED,
peerBName + " " + peerB->fromAddr(), peer);
TCPMessengerProtocol::sendToServer(SESSION_ESTABLISHED,
peerName + " " + peer->fromAddr(), peerB);
//remove peers from the dispatcher responsibility
this->removePeer(peer);
this->removePeer(peerB);
//give responsibility of the peers to a new brocker
Brocker *broker = new Brocker(this, peer, peerB, peerName, peerBName);
//keep reference of brocker in brockers vector
brockers.push_back(broker);
}
else {
//get peer name
string peerName;
for (map<string, TCPSocket *>::iterator itr = loggedInUsers.begin();
itr != loggedInUsers.end(); ++itr) {
if (itr->second == peer) {
peerName = itr->first;
}
}
//if peer does not exist in peers list - refuse the session
TCPMessengerProtocol::sendToServer(SESSION_REFUSED, peerName, peer);
}
}
}
}
示例2: run
void Dispatcher::run(){
int command;
string data;
while(running){
if(peers.size() == 0){
running = false;
break;
}
TCPSocket* peer = listener->listen(TIMEOUT);
if(peer!=NULL){
command = 0;
data.clear();
TCPMessengerProtocol::readFromServer(command, data, peer);
//cout<<"read command from peer: "<< command << " " << data << endl;
switch(command){
case LOGIN: {
login(peer,data);
break;
}
case REGISTER:{
reg(peer,data);
break;
}
case OPEN_OR_CONNECT_TO_ROOM: {
openOrConnectRoom(peer,data);
break;
}
case OPEN_SESSION_WITH_PEER: {
openSession(peer,data);
break;
}
case LIST_USERS:{
listUsers(peer,data);
break;
}
case LIST_CONNECTED_USERS:{
listConnectedUsers(peer,data);
break;
}
case LIST_ROOMS:{
listRooms(peer,data);
break;
}
case LIST_ROOM_USERS:{
listRoomUsers(peer,data);
break;
}
case EXIT: {
exit(peer,data);
break;
}
default: {
cout << BOLDRED<< "Problems with client: " << peer->fromAddr() <<". disconnecting"<< RESET<<endl;
removePeer(peer);
for (map<string, TCPSocket *>::iterator itr = loggedInUsers.begin();
itr != loggedInUsers.end(); ++itr){
if(itr->second==peer){
loggedInUsers.erase(itr->first);
}
}
break;
}
}
}
}
}