当前位置: 首页>>代码示例>>C++>>正文


C++ TCPSocket::fromAddr方法代码示例

本文整理汇总了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);
            }
        }
    }
}
开发者ID:GalProject,项目名称:Network-Programming-Project,代码行数:47,代码来源:Dispatcher.cpp

示例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;
                    }
                }

            }
    }
}
开发者ID:GalProject,项目名称:Network-Programming-Project,代码行数:68,代码来源:Dispatcher.cpp


注:本文中的TCPSocket::fromAddr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。