Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualTispe

Posted 03 October 2013 - 01:30 PM

Added an std::map to own the shared_ptr but on the second packet the application crashes. It works once, then crash .... Getting an access violation.

The statement std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data); results in an spPeer with unvalid members.

std::map<long long, std::shared_ptr<PeerData>> AllPeers;
bool loop = true;
while(loop){
	ENetEvent netevent;
	/* Wait up to 1000 milliseconds for an event. */
	if(enet_host_service (server, &netevent, 10)){
		switch (netevent.type)    {    
		case ENET_EVENT_TYPE_CONNECT:
			{
				char buffer[64];
				in_addr addr;
				long long ipandport = netevent.peer->address.host;
				ipandport = (ipandport << 16) | netevent.peer->address.port;		//Unique ID
				std::shared_ptr<PeerData> spNewPeer(new PeerData);
				spNewPeer->ipandport = ipandport;
				spNewPeer->peer = netevent.peer;
				addr.S_un.S_addr = netevent.peer->address.host;
				sprintf_s(buffer, "%s:%d", inet_ntoa(addr), netevent.peer->address.port);
				printf("A new client connected from %s.\n", buffer);
				spNewPeer->sipandport = string(buffer);
				netevent.peer->data = spNewPeer.get();								//assigning, might be bad....
				cout << "Registered user: " << ipandport << endl;
				AllPeers[ipandport] = spNewPeer;
				break;
			}
		case ENET_EVENT_TYPE_RECEIVE:
			{
				std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);
				cout << "A packet of length " << netevent.packet->dataLength 
					<< " containing " << netevent.packet->data 
					<< " was received from " << spPeer->sipandport			//causes compile errors, cannot convert parameter 1 from 'void *' to 'PeerData *'
					<< " on channel" << netevent.channelID << "." << endl;

				if(netevent.packet != NULL){
					enet_packet_destroy (netevent.packet);
				}
				break;
			}

#2Tispe

Posted 03 October 2013 - 01:26 PM

Added an std::map to own the shared_ptr but on the second packet the application crashes. It works once, then crash .... Getting an access violation.

std::map<long long, std::shared_ptr<PeerData>> AllPeers;
bool loop = true;
while(loop){
	ENetEvent netevent;
	/* Wait up to 1000 milliseconds for an event. */
	if(enet_host_service (server, &netevent, 10)){
		switch (netevent.type)    {    
		case ENET_EVENT_TYPE_CONNECT:
			{
				char buffer[64];
				in_addr addr;
				long long ipandport = netevent.peer->address.host;
				ipandport = (ipandport << 16) | netevent.peer->address.port;		//Unique ID
				std::shared_ptr<PeerData> spNewPeer(new PeerData);
				spNewPeer->ipandport = ipandport;
				spNewPeer->peer = netevent.peer;
				addr.S_un.S_addr = netevent.peer->address.host;
				sprintf_s(buffer, "%s:%d", inet_ntoa(addr), netevent.peer->address.port);
				printf("A new client connected from %s.\n", buffer);
				spNewPeer->sipandport = string(buffer);
				netevent.peer->data = spNewPeer.get();								//assigning, might be bad....
				cout << "Registered user: " << ipandport << endl;
				AllPeers[ipandport] = spNewPeer;
				break;
			}
		case ENET_EVENT_TYPE_RECEIVE:
			{
				std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);
				cout << "A packet of length " << netevent.packet->dataLength 
					<< " containing " << netevent.packet->data 
					<< " was received from " << spPeer->sipandport			//causes compile errors, cannot convert parameter 1 from 'void *' to 'PeerData *'
					<< " on channel" << netevent.channelID << "." << endl;

				if(netevent.packet != NULL){
					enet_packet_destroy (netevent.packet);
				}
				break;
			}

#1Tispe

Posted 03 October 2013 - 01:22 PM

Added an std::map to own the shared_ptr but on the second packet the application crashes. It works once, then crash ....

std::map<long long, std::shared_ptr<PeerData>> AllPeers;
bool loop = true;
while(loop){
	ENetEvent netevent;
	/* Wait up to 1000 milliseconds for an event. */
	if(enet_host_service (server, &netevent, 10)){
		switch (netevent.type)    {    
		case ENET_EVENT_TYPE_CONNECT:
			{
				char buffer[64];
				in_addr addr;
				long long ipandport = netevent.peer->address.host;
				ipandport = (ipandport << 16) | netevent.peer->address.port;		//Unique ID
				std::shared_ptr<PeerData> spNewPeer(new PeerData);
				spNewPeer->ipandport = ipandport;
				spNewPeer->peer = netevent.peer;
				addr.S_un.S_addr = netevent.peer->address.host;
				sprintf_s(buffer, "%s:%d", inet_ntoa(addr), netevent.peer->address.port);
				printf("A new client connected from %s.\n", buffer);
				spNewPeer->sipandport = string(buffer);
				netevent.peer->data = spNewPeer.get();								//assigning, might be bad....
				cout << "Registered user: " << ipandport << endl;
				AllPeers[ipandport] = spNewPeer;
				break;
			}
		case ENET_EVENT_TYPE_RECEIVE:
			{
				std::shared_ptr<PeerData> spPeer((PeerData*)netevent.peer->data);
				cout << "A packet of length " << netevent.packet->dataLength 
					<< " containing " << netevent.packet->data 
					<< " was received from " << spPeer->sipandport			//causes compile errors, cannot convert parameter 1 from 'void *' to 'PeerData *'
					<< " on channel" << netevent.channelID << "." << endl;

				if(netevent.packet != NULL){
					enet_packet_destroy (netevent.packet);
				}
				break;
			}

PARTNERS