예제 #1
0
/**
 * @brief ServerThreaded::processNewConnection
 */
void ServerThreaded::processNewConnection()
{
	std::cout << QObject::tr("Client connected").toStdString() << std::endl;

	// Get the connecting socket
	QtWebsocket::QWsSocket* socket = server->nextPendingConnection();

	// Create a new thread and giving to him the socket
	SocketThread* thread = new SocketThread(socket);
	
	// connect for message broadcast
	// QObject::connect(socket, SIGNAL(frameReceived(QString)), this, SIGNAL(broadcastMessage(QString)));
	QObject::connect(this, SIGNAL(broadcastMessage(QString)), thread, SLOT(sendMessage(QString)));

	// Starting the thread
	thread->start();
}
예제 #2
0
void ServerThreaded::processNewConnection()
{
	Log::display("Client connected");

	// Get the connecting socket
	QWsSocket * socket = server->nextPendingConnection();

	// Create a new thread and giving to him the socket
	SocketThread * thread = new SocketThread( socket );
	
	// connect for message broadcast
	connect( socket, SIGNAL(frameReceived(QString)), this, SIGNAL(broadcastMessage(QString)) );
	connect( this, SIGNAL(broadcastMessage(QString)), thread, SLOT(sendMessage(QString)) );

	// connect for message display in log
	connect( socket, SIGNAL(frameReceived(QString)), this, SLOT(displayMessage(QString)) );

	// Starting the thread
	thread->start();
}
예제 #3
0
	void HandleEvent(EventType et, int errornum)
	{
		if (et == EVENT_READ)
		{
			eventfd_t dummy;
			eventfd_read(fd, &dummy);
			parent->OnNotify();
		}
		else
		{
			ServerInstance->GlobalCulls.AddItem(this);
		}
	}
예제 #4
0
	void HandleEvent(EventType et, int errornum)
	{
		if (et == EVENT_READ)
		{
			char dummy[128];
			read(fd, dummy, 128);
			parent->OnNotify();
		}
		else
		{
			ServerInstance->GlobalCulls.AddItem(this);
		}
	}
예제 #5
0
void Server::processNewWSConnection()
{
  std::cout << QObject::tr("Client connected").toStdString() << std::endl;

  // Get the connecting socket
  QWebSocket* socket = wsServer_->nextPendingConnection();

  // Create a new thread and giving to him the socket
  SocketThread* thread = new SocketThread(socket);

  // connect for message broadcast
  //    QObject::connect(socket, SIGNAL(frameReceived(QString)), this, SIGNAL(broadcastMessage(QString)));
  QObject::connect(this, SIGNAL(broadcastMessage(QString)), thread, SLOT(sendMessage(QString)));

  connect(thread,
          &SocketThread::newFEMPRequest,
          this,
          &Server::newFEMPRequest,
          Qt::DirectConnection);

  // Starting the thread
  thread->start();
}
예제 #6
0
	void OnDataReady()
	{
		recvq.clear();
		parent->OnNotify();
	}
예제 #7
0
void CollaborativeServer::initConnection(int id, CVRSocket * sock)
{
    if(!sock->send(&id,sizeof(int)))
    {
        delete sock;
        return;
    }
    /*int namesize;
     if(!sock->recv(&namesize,sizeof(int)))
     {
     delete sock;
     return;
     }
     std::cerr << "Got name size: " << namesize << std::endl;
     char name[255];
     if(!sock->recv(name,namesize))
     {
     delete sock;
     return;
     }*/

    ClientInitInfo cii;
    if(!sock->recv(&cii,sizeof(struct ClientInitInfo)))
    {
        delete sock;
        return;
    }

    std::cerr << "Got name: " << cii.name << " numHands: " << cii.numHands
            << " numHeads: " << cii.numHeads << std::endl;

    _serverLock.lock();

    ServerInitInfo sii;
    sii.numUsers = _clientInitMap.size();

    ClientInitInfo * activeClients = NULL;

    if(sii.numUsers)
    {
        activeClients = new ClientInitInfo[sii.numUsers];
        int acindex = 0;
        for(std::map<int,struct ClientInitInfo>::iterator it =
                _clientInitMap.begin(); it != _clientInitMap.end(); it++)
        {
            activeClients[acindex] = it->second;
            std::cerr << "Adding client init info to send name: "
                    << activeClients[acindex].name << " id: "
                    << activeClients[acindex].id << " heads: "
                    << activeClients[acindex].numHeads << " hands: "
                    << activeClients[acindex].numHands << std::endl;
            acindex++;
        }
    }

    // send other users' id and and name size
    /*int index = 0;
     int totalsize = 0;
     int * userinfo = NULL;
     char * usernames = NULL;
     if(sii.numUsers)
     {
     userinfo = new int[sii.numUsers * 2];
     for(std::map<int, std::string>::iterator it = _nameMap.begin(); it != _nameMap.end(); it++)
     {
     userinfo[index++] = it->first;
     userinfo[index++] = it->second.size()+1;
     totalsize += it->second.size()+1;
     }
     usernames = new char[totalsize];
     index = 0;
     for(int i = 0; i < _nameMap.size(); i++)
     {
     strcpy(usernames + index, _nameMap[userinfo[i*2]].c_str());
     index += userinfo[(i*2)+1];
     }
     }*/

    _serverLock.unlock();

    if(!sock->send(&sii,sizeof(struct ServerInitInfo)))
    {
        delete sock;
        return;
    }

    if(sii.numUsers)
    {
        /*if(!sock->send(userinfo,sizeof(int)*sii.numUsers*2))
         {
         delete[] userinfo;
         delete[] usernames;
         delete sock;
         return;
         }

         if(!sock->send(usernames,sizeof(char)*totalsize))
         {
         delete[] userinfo;
         delete[] usernames;
         delete sock;
         return;
         }

         delete[] userinfo;
         delete[] usernames;*/

        if(!sock->send(activeClients,
                sizeof(struct ClientInitInfo) * sii.numUsers))
        {
            delete[] activeClients;
            delete sock;
            return;
        }

        delete[] activeClients;
    }

    ClientUpdate * cu = &_clientMap[id];
    memset(cu,0,sizeof(struct ClientUpdate));
    cu->objScale = 1.0;
    cu->objTrans[0] = cu->objTrans[5] = cu->objTrans[10] = cu->objTrans[15] =
            1.0;
    cu->numMes = id;

    cii.id = id;
    _clientInitMap[id] = cii;

    _clientHeadList[id] = std::vector<BodyUpdate>();
    _clientHandList[id] = std::vector<BodyUpdate>();

    BodyUpdate bu;
    bu.pos[0] = bu.pos[1] = bu.pos[2] = 0.0;
    bu.rot[0] = bu.rot[1] = bu.rot[2] = 0.0;
    bu.rot[3] = 1.0;

    for(int i = 0; i < cii.numHeads; i++)
    {
        _clientHeadList[id].push_back(bu);
    }

    for(int i = 0; i < cii.numHands; i++)
    {
        _clientHandList[id].push_back(bu);
    }

    if(_threadList.size())
    {

        /*int msize = strlen(cii.name) + 1;
         msize += sizeof(int);
         char * mdata = new char[msize];

         *((int *)mdata) = id;
         strcpy(mdata+sizeof(int), cii.name);*/
        ClientInitInfo * newcii = new ClientInitInfo;
        *newcii = cii;

        CollaborativeMessageHeader cmh;
        cmh.type = ADD_CLIENT;
        strcpy(cmh.target,"Collaborative");
        cmh.size = sizeof(struct ClientInitInfo);

        CollaborativeMessage * cmessage = new CollaborativeMessage(
                _threadList.size(),cmh,(char*)newcii);

        _messageAddLock.lock();

        for(int i = 0; i < _threadList.size(); i++)
        {
            _threadList[i]->addMessage(cmessage);
        }

        _messageAddLock.unlock();

    }

    SocketThread * st;
    st = new SocketThread(sock,cii.name,id,this);

    _serverLock.lock();

    _threadList.push_back(st);

    _serverLock.unlock();

    st->start();
    std::cerr << "Added socket from host: " << cii.name << std::endl;
}