Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualTispe

Posted 03 October 2013 - 11:16 AM

Right. The tutorial is very misleading here. It sets event.peer->data = "Client information";. Where this string is constant inside the binary...

I am trying to use a std::shared_ptr and attach it to event.peer->data but I have some difficulties. Mainly casting between void* and

std::shared_pr<mystruct>. Got any input?

 

I want to stay away from raw pointers, don't want to risk leaks if a disconnect does not get recieved such that the delete operator does not get called or something buggy like that.

case ENET_EVENT_TYPE_CONNECT:
	{
		char buffer[64];
		in_addr addr;
		long long ipandport = (netevent.peer->address.host << 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);						//assigning, might be bad....
		cout << "Registered user: " << ipandport << endl;
		break;
	}
case ENET_EVENT_TYPE_RECEIVE:
	{
		std::shared_ptr<PeerData> spPeer(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;
	}

#3Tispe

Posted 03 October 2013 - 11:15 AM

Right. The tutorial is very misleading here. It sets event.peer->data = "Client information";. Where this string is constant inside the binary...

I am trying to use a std::shared_ptr and attach it to event.peer->data but I have some difficulties. Mainly casting between void* and

std::shared_pr<mystruct>. Got any input?

 

I want to stay away from raw pointers, don't want to risk leaks if a disconnect does get recieved such that the delete operator does not get called or something buggy like that.

case ENET_EVENT_TYPE_CONNECT:
	{
		char buffer[64];
		in_addr addr;
		long long ipandport = (netevent.peer->address.host << 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);						//assigning, might be bad....
		cout << "Registered user: " << ipandport << endl;
		break;
	}
case ENET_EVENT_TYPE_RECEIVE:
	{
		std::shared_ptr<PeerData> spPeer(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 - 11:14 AM

Right. The tutorial is very misleading here. It sets event.peer->data = "Client information";. Where this string is constant inside the binary...

I am trying to use a std::shared_ptr and attach it to event.peer->data but I have some difficulties. Mainly casting between void* and

std::shared_pr<mystruct>. Got any input?

 

I want to stay away from raw pointers, don't want to risk leaks if a disconnect does get recieved such that the delete operator does not get called or something buggy like that.

case ENET_EVENT_TYPE_CONNECT:
				{
					char buffer[64];
					in_addr addr;
					long long ipandport = (netevent.peer->address.host << 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);						//assigning, might be bad....
					cout << "Registered user: " << ipandport << endl;
					break;
				}
			case ENET_EVENT_TYPE_RECEIVE:
				{
					std::shared_ptr<PeerData> spPeer(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 - 11:12 AM

Right. The tutorial is very misleading here. It sets event.peer->data = "Client information";. Where this string is constant inside the binary...

I am trying to use a std::shared_ptr on attach it to event.peer->data but I have some difficulties. Mainly casting between void* and

std::shared_pr<mystruct>.

case ENET_EVENT_TYPE_CONNECT:
				{
					char buffer[64];
					in_addr addr;
					long long ipandport = (netevent.peer->address.host << 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);						//assigning, might be bad....
					cout << "Registered user: " << ipandport << endl;
					break;
				}
			case ENET_EVENT_TYPE_RECEIVE:
				{
					std::shared_ptr<PeerData> spPeer(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