Exemplo n.º 1
0
void Connection::send_packet_reliable(void *data,int ID){
	ENetPacket *packet = enet_packet_create (data, sizeof(data), ENET_PACKET_FLAG_RELIABLE);
	if(!client)
		enet_peer_send (&connS->peers[ID], 0, packet);
	else
		enet_peer_send(peer,0,packet);
}
Exemplo n.º 2
0
void joynet_handle_server_chat_message(JOYNET_SERVER * sp, JOYNET_MESSAGE * mp)
{
	switch(mp->type)
	{
		case JOYNET_CHAT_MESSAGE_SET_NAME:
		{
			int client = joynet_get_client_from_peer(sp, mp->event->peer);
			ENetPacket * packet;
			
			/* send SET_NAME message back to client to finalize the change */
			strcpy(sp->client[client]->screen_name, mp->data);
			packet = joynet_build_packet(JOYNET_CHAT_MESSAGE_SET_NAME, mp->data, strlen(mp->data) + 1);
			enet_peer_send(sp->client[client]->peer, JOYNET_CHANNEL_CHAT, packet);
			break;
		}
		case JOYNET_CHAT_MESSAGE_SET_GROUP:
		{
			int client = joynet_get_client_from_peer(sp, mp->event->peer);
			ENetPacket * packet;
			
			joynet_serialize(sp->serial_data, mp->data);
			joynet_getw(sp->serial_data, &sp->client[client]->group);
			
			/* send SET_GROUP message back to client to finalize the change */
			packet = joynet_build_packet(JOYNET_CHAT_MESSAGE_SET_GROUP, mp->data, sizeof(short));
			enet_peer_send(sp->client[client]->peer, JOYNET_CHANNEL_CHAT, packet);
			break;
		}
		case JOYNET_CHAT_MESSAGE_TALK:
		case JOYNET_CHAT_MESSAGE_GROUP_TALK:
		{
			int client = joynet_get_client_from_peer(sp, mp->event->peer);
			char data[1024] = {0};
			short name_length = strlen(sp->client[client]->screen_name) + 1;
			short message_length = strlen(mp->data) + 1;
			ENetPacket * packet;
			int i;

			/* TALK message is different for client, contains user name and message */
			joynet_serialize(sp->serial_data, data);
			joynet_putw(sp->serial_data, name_length);
			joynet_putw(sp->serial_data, message_length);
			joynet_write(sp->serial_data, sp->client[client]->screen_name, name_length);
			joynet_write(sp->serial_data, mp->data, message_length);
			packet = joynet_create_packet(JOYNET_CHAT_MESSAGE_TALK, sp->serial_data);
			for(i = 0; i < sp->max_clients; i++)
			{
				if((sp->client[i]->peer) && ((mp->type == JOYNET_CHAT_MESSAGE_GROUP_TALK) || (sp->client[i]->group == sp->client[client]->group)))
				{
					enet_peer_send(sp->client[i]->peer, JOYNET_CHANNEL_CHAT, packet);
				}
			}
			break;
		}
	}
}
Exemplo n.º 3
0
void connection_send_to(uint32_t id, uint8_t channel, const char *data, size_t size, ENetPeer* target) {
    ENetPacket *packet = enet_packet_create(NULL, sizeof(user_id) +sizeof(id) + sizeof(uint32_t) + size, 0);
    pack_format((char *)packet->data, "uua", user_id, id, data, size);

    if (target) {
        enet_peer_send(target, channel, packet);
        return;
    }

    if (connection_is_host()) {
        broadcast_except(NULL, packet, channel);
    } else if (server) {
        enet_peer_send(server, channel, packet);
    }
}
Exemplo n.º 4
0
bool Game::send(const Packet & packet, ENetPeer * peer)
{
	ENetPacket * enetPacket = enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE);
	if (!enetPacket)
		return false;
	return enet_peer_send(peer, 0, enetPacket) == 0;
}
Exemplo n.º 5
0
Arquivo: main.c Projeto: atheros/svc
/**
 * Handle input command.
 *
 * This function should return 0, even if the command failed.
 * Return of != 0 means the event loop will quit.
 */
static int handle_input_command(const dstring* string) {
	dstrlist* list;
	dstring** argv;
	unsigned int argc;
	int error;
	ENetPacket* packet;


	if (string->len == 0) {
		/* empty command */
		return 0;
	} else if (string->data[0] == '/') {
		/* local command */
		/* parse the command */
		list = dstrlex_parse(string, &error);
		if (list == NULL) {
			fprintf(stderr, "Failed to parse command string '%s': %s\n", string->data, dstrlex_errstr(error));
			return 0;
		}

		/* convert list to vector */
		argc = list->size;
		argv = dlist_tovector(list);
		dlist_free(list);

		/* select command to execute */
		if (dcmpcs(argv[0], "/connect") == 0) {
			error = cmd_connect(argc, argv);
		} else if (dcmpcs(argv[0], "/disconnect") == 0) {
			error = cmd_disconnect(argc, argv);
		} else if (dcmpcs(argv[0], "/quit") == 0) {
			error = cmd_quit(argc, argv);
		} else if (dcmpcs(argv[0], "/mute") == 0) {
			error = cmd_mute(argc, argv);
		} else if (dcmpcs(argv[0], "/deafen") == 0) {
			error = cmd_deafen(argc, argv);
		} else {
			fprintf(stderr, "Unknown command '%s'\n", argv[0]->data);
			error = 0;
		}

		dvec_free(argv);
		return error;
	} else if (client.state == SVCECLIENT_STATE_CONNECTED) {
		/* send to server if connected */
		mutex_lock(&client.network_lock);
		packet = enet_packet_create(string->data, string->len, ENET_PACKET_FLAG_RELIABLE);
		enet_peer_send(client.client, 0, packet);
		mutex_unlock(&client.network_lock);
	} else if (client.state == SVCECLIENT_STATE_CONNECTING) {
		/* server command but still connecting */
		fprintf(stderr, "Can't send command to server, still connecting.\n");
	} else {
		/* server command but not connected */
		fprintf(stderr, "Can't send command to server, not connected.\n");
	}


	return 0;
}
Exemplo n.º 6
0
void ENetServer::sendMessage(uint32_t id, DeliveryType type, Message::Shared msg) const
{
    auto client = getClient(id);
    if (!client) {
        // no client to send to
        LOG_WARN("No connected client with id: " << id);
        return;
    }
    uint32_t channel = 0;
    uint32_t flags = 0;
    if (type == DeliveryType::RELIABLE) {
        channel = RELIABLE_CHANNEL;
        flags = ENET_PACKET_FLAG_RELIABLE;
    } else {
        channel = UNRELIABLE_CHANNEL;
        flags = ENET_PACKET_FLAG_UNSEQUENCED;
    }

    // get bytes
    auto data = msg->serialize();
    // create the packet
    ENetPacket* p = enet_packet_create(
        &data[0],
        data.size(),
        flags);

    // send the packet to the peer
    enet_peer_send(client, channel, p);
    // flush / send the packet queue
    enet_host_flush(host_);
}
Exemplo n.º 7
0
void GDNetHost::send_messages() {
	while (!_message_queue.is_empty()) {
		GDNetMessage* message = _message_queue.pop();

		int flags = 0;

		switch (message->get_type()) {
			case GDNetMessage::UNSEQUENCED:
				flags |= ENET_PACKET_FLAG_UNSEQUENCED;
				break;

			case GDNetMessage::RELIABLE:
				flags |= ENET_PACKET_FLAG_RELIABLE;
				break;

			default:
				break;
		}

		ByteArray::Read r = message->get_packet().read();
		ENetPacket * enet_packet = enet_packet_create(r.ptr(), message->get_packet().size(), flags);

		if (enet_packet != NULL) {
			if (message->is_broadcast()) {
				enet_host_broadcast(_host, message->get_channel_id(), enet_packet);
			} else {
				enet_peer_send(&_host->peers[message->get_peer_id()], message->get_channel_id(), enet_packet);
			}
		}

		memdelete(message);
	}
}
Exemplo n.º 8
0
/**
        udpr_send_char : peer -> data:int -> chan:int -> reliable:bool -> void
        Send a character [ over a connected socket. Must be in the range 0..255</doc>
**/
static value udpr_send_char( value p, value data, value chan, value reliable ) {
	ENetPeer *peer;
	ENetPacket *packet;
	int c,d;
	char buf[2];
	val_check_kind(p,k_udprpeer);
	val_check(data,int);
	val_check(chan,int);
	val_check(reliable,bool);

	peer = (ENetPeer *)val_data(p);
	c = val_int(chan);
    d = val_int(data);

    if( peer == NULL || c < 0 || c > 255 || d < 0 || d > 255 )
        neko_error();
   	buf[0] = (unsigned char)d;
   	buf[1] = '\0';
	packet = enet_packet_create(
		buf,
		1,
		val_bool(reliable) ? ENET_PACKET_FLAG_RELIABLE : 0);
    if(enet_peer_send(peer, c, packet))
		neko_error();
    return val_true;
}
Exemplo n.º 9
0
void joynet_select_player(JOYNET_GAME * gp, int player)
{
	char data[2];
	ENetPacket * pp;
	if(gp->client)
	{
		if(gp->client->master && gp->state == JOYNET_GAME_STATE_PLAYING)
		{
			joynet_serialize(gp->client->serial_data, data);
			joynet_putw(gp->client->serial_data, player);
			pp = joynet_create_packet(JOYNET_GAME_MESSAGE_SELECT_PLAYER, gp->client->serial_data);
			enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
		}
	}
	else
	{
		JOYNET_MESSAGE message;
		
		if(gp->callback)
		{
			message.type = JOYNET_GAME_MESSAGE_SELECT_PLAYER;
			message.data = NULL;
			message.data_size = 0;
			message.event = NULL;
			gp->callback(&message);
			gp->current_player = player;
		}
	}
}
Exemplo n.º 10
0
void joynet_end_game(JOYNET_GAME * gp)
{
	ENetPacket * pp;
	if(gp->client)
	{
		if(gp->client->master && (gp->state == JOYNET_GAME_STATE_PAUSED || gp->state == JOYNET_GAME_STATE_PLAYING))
		{
			pp = joynet_create_packet(JOYNET_GAME_MESSAGE_END, NULL);
			enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
		}
	}
	else
	{
		JOYNET_MESSAGE message;
		
		if(gp->state == JOYNET_GAME_STATE_PLAYING || gp->state == JOYNET_GAME_STATE_PAUSED)
		{
			gp->state = JOYNET_GAME_STATE_OFF;
			if(gp->callback)
			{
				message.type = JOYNET_GAME_MESSAGE_END;
				message.data = NULL;
				message.data_size = 0;
				message.event = NULL;
				gp->callback(&message);
			}
		}
	}
}
Exemplo n.º 11
0
/* high level functions */
void joynet_update_game_option(JOYNET_GAME * gp, int * option)
{
	char data[1024] = {0};
	ENetPacket * pp;
	int i, c = 0;
	
	if(gp->client && gp->client->master)
	{
		for(i = 0; i < gp->options; i++)
		{
			if(gp->option[i] == option)
			{
				c = i;
				break;
			}
		}
		if(i < gp->options)
		{
			joynet_serialize(gp->client->serial_data, data);
			joynet_putw(gp->client->serial_data, c);
			joynet_putl(gp->client->serial_data, *gp->option[c]);
			pp = joynet_create_packet(JOYNET_GAME_MESSAGE_UPDATE_OPTION, gp->client->serial_data);
			enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
		}
	}
	else
	{
	}
}
Exemplo n.º 12
0
/* if process is true, input will be sent (online play) */
void joynet_update_game(JOYNET_GAME * gp, int process)
{
	ENetPacket * pp;
	char data[1024];
	if(gp->client)
	{
		if(gp->player[gp->current_player]->local && process)
		{
			if(joynet_encode_game_input(gp, data))
			{
				pp = joynet_create_packet(JOYNET_GAME_MESSAGE_INPUT, gp->serial_data);
				enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
			}
		}
	}
	else
	{
		if(joynet_encode_game_input(gp, data))
		{
            joynet_write_input_buffer_frame(gp->input_buffer, data);
		}
		else
		{
			printf("failed to encode\n");
		}
	}
}
Exemplo n.º 13
0
void networkEngine::sendPacket(std::string packetData)
{
    //conversion *convert = conversion::Instance();
    boost::shared_ptr<conversion> convert = conversion::Instance();
    //gameEngine *gameE = gameEngine::Instance();
    boost::shared_ptr<gameEngine> gameE = gameEngine::Instance();

    if (gameE->getServerRunning())
    {
        while (enet_host_service (server, &event, 0) > 0)
        {
        }
    }
    else if (gameE->getClientRunning())
    {
        while (enet_host_service (client, &event, 0) > 0)
        {
        }
    }
    std::string host = convert->toString(peer->address.host);
    logMsg("packetData == " + packetData);
    logMsg("Peer host == " +host);
    exit(0);
    packet = enet_packet_create(packetData.c_str(),strlen(packetData.c_str())+1,ENET_PACKET_FLAG_RELIABLE);
    enet_peer_send (peer, 0, packet);

}
Exemplo n.º 14
0
void NetServer::SendTimePacket(ENetPacket *pPacketRec, ENetPeer * pPeer)
{
	GfLogTrace("Sending Time Packet\n");

	double time = GfTimeClock();
	GfLogTrace("\nServer time is %lf",time);

        PackedBuffer msg;

        try
        {
                msg.pack_ubyte(SERVER_TIME_SYNC_PACKET);
                msg.pack_double(time);
        }
        catch (PackedBufferException &e)
        {
                GfLogFatal("SendTimePacket: packed buffer error\n");
        }
        GfLogTrace("SendTimePacket: packed data length=%d\n",
                msg.length());

	//TODO change to peer send
	ENetPacket * pPacket = enet_packet_create (msg.buffer(), 
                                              msg.length(), 
                                              ENET_PACKET_FLAG_UNSEQUENCED);

	enet_peer_send (pPeer, UNRELIABLECHANNEL, pPacket);
}
Exemplo n.º 15
0
bool NetServer::SendPlayerRejectedPacket(ENetPeer * pPeer,std::string strReason)
{
	//Send to client requesting connection

        PackedBuffer msg;

        try
        {
                msg.pack_ubyte(PLAYERREJECTED_PACKET);	
                msg.pack_stdstring(strReason);
        }
        catch (PackedBufferException &e)
        {
                GfLogFatal("SendPlayerRejectedPacket: packed buffer error\n");
        }
        GfLogTrace("SendPlayerRejectedPacket: packed data length=%d\n",
                msg.length());

	ENetPacket * pPacket = enet_packet_create (msg.buffer(), 
                                              msg.length(), 
                                              ENET_PACKET_FLAG_RELIABLE);

	if (enet_peer_send (pPeer, RELIABLECHANNEL, pPacket)==0)
		return true;

	return false;
}
Exemplo n.º 16
0
/**
        udpr_send : peer -> data:string -> pos:int -> len:int -> chan:int -> reliable:bool -> int
        Send up to [len] bytes from [buf] starting at [pos] over a connected socket on channel [chan]
        using reliable setting [reliable]
        Return the number of bytes sent.
**/
static value udpr_send( value p, value data, value pos, value len, value chan, value reliable ) {
	ENetPeer *peer;
	ENetPacket *packet;
	int pp,l,sLen,c;
	val_check_kind(p,k_udprpeer);
	val_check(data,string);
	val_check(pos,int);
	val_check(len,int);
	val_check(chan,int);
	val_check(reliable,bool);

	peer = (ENetPeer *)val_data(p);
	pp = val_int(pos);
	l = val_int(len);
	sLen = val_strlen(data);
	c = val_int(chan);
	if( peer == NULL || c < 0 || c > 255 || pp < 0 || l < 0 || pp > sLen || pp + l > sLen )
			neko_error();
	packet = enet_packet_create(
		val_string(data) + pp,
		l,
		val_bool(reliable) ? ENET_PACKET_FLAG_RELIABLE : 0);
	if(enet_peer_send(peer, c, packet))
		neko_error();
	return alloc_int(l);
}
Exemplo n.º 17
0
/* if process is true, input will be sent (online play) */
void joynet_update_game(JOYNET_GAME * gp, int process)
{
	ENetPacket * pp;
	char data[1024];
	if(gp->client)
	{
		if(process)
		{
			if(joynet_encode_game_input(gp, data))
			{
				pp = joynet_create_packet(JOYNET_GAME_MESSAGE_INPUT, gp->serial_data);
				enet_peer_send(gp->client->peer, JOYNET_CHANNEL_GAME, pp);
			}
		}
	}
	else
	{
		if(joynet_encode_game_input(gp, data))
		{
			memcpy(&(gp->input_buffer->data[gp->input_buffer->write_pos]), data, gp->input_buffer->frame_size);
//			printf("data - %d - %d\n", data[0], data[1]);
			gp->input_buffer->write_pos += gp->input_buffer->frame_size;
			if(gp->input_buffer->write_pos >= gp->input_buffer->frame_size * gp->max_buffer_frames)
			{
				gp->input_buffer->write_pos = 0;
			}
			gp->input_buffer->frames++;
		}
	}
}
Exemplo n.º 18
0
void Connection::send(u16 peer_id, u8 channelnum,
		SharedBuffer<u8> data, bool reliable)
{
	{
		//MutexAutoLock peerlock(m_peers_mutex);
		if (m_peers.find(peer_id) == m_peers.end())
			return;
	}
	//dout_con<<getDesc()<<" sending to peer_id="<<peer_id<<std::endl;

	assert(channelnum < CHANNEL_COUNT);

	ENetPacket *packet = enet_packet_create(*data, data.getSize(), reliable ? ENET_PACKET_FLAG_RELIABLE : 0);

	ENetPeer *peer = getPeer(peer_id);
	if(!peer) {
		deletePeer(peer_id, false);
		return;
	}
	if (enet_peer_send(peer, channelnum, packet) < 0) {
		infostream<<"enet_peer_send failed peer="<<peer_id<<" reliable="<<reliable<< " size="<<data.getSize()<<std::endl;
		if (reliable)
			deletePeer(peer_id, false);
		return;
	}
}
Exemplo n.º 19
0
void net_controller_har_hook(int action, void *cb_data) {
    controller *ctrl = cb_data;
    wtf *data = ctrl->data;
    serial ser;
    ENetPeer *peer = data->peer;
    ENetHost *host = data->host;
    ENetPacket *packet;
    if (action == ACT_STOP && data->last_action == ACT_STOP) {
        data->last_action = -1;
        return;
    }
    if (action == ACT_FLUSH) {
        enet_host_flush(host);
        return;
    }
    data->last_action = action;
    serial_create(&ser);
    serial_write_int8(&ser, EVENT_TYPE_ACTION);
    serial_write_int16(&ser, action);
    /*DEBUG("controller hook fired with %d", action);*/
    /*sprintf(buf, "k%d", action);*/
    packet = enet_packet_create(ser.data, ser.len, ENET_PACKET_FLAG_RELIABLE);
    if (peer) {
        enet_peer_send(peer, 1, packet);
        /*enet_host_flush (host);*/
    } else {
        DEBUG("peer is null~");
    }
}
Exemplo n.º 20
0
pbool PNetworkServer::send(PNetworkPeer *peer, const puint8 *message, pint32 length)
{
    if (m_state == NETWORK_CONNECTED)
    {
        PMap<puint32, PNetworkPeer*>::iterator it = m_peers.find(peer->id());
        if (it != m_peers.end())
        {
            if (length == -1)
            {
                m_data->packet->dataLength = pstrlen((const pchar *)message) + 1;
            }
            else
            {
                m_data->packet->dataLength = length;
            }
            
            pmemcpy(m_data->packet->data, message, m_data->packet->dataLength);
            if (enet_peer_send(peer->data()->peer, 0, m_data->packet) < 0)
            {
                PLOG_ERROR("Failed to send message to peer (%d).", peer->id());
                return false;
            }

            return true;
        }
    }

    return false;
}
Exemplo n.º 21
0
VALUE renet_connection_send_packet(VALUE self, VALUE data, VALUE flag, VALUE channel)
{
  Connection* connection;
  Data_Get_Struct(self, Connection, connection);
  VALUE lock = rb_iv_get(self, "@lock");
  rb_mutex_lock(lock);

  Check_Type(data, T_STRING);
  char* cdata = StringValuePtr(data);
  ENetPacket* packet;
  if (connection->online != 0)
  {
    if (flag == Qtrue)
    {
      packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, ENET_PACKET_FLAG_RELIABLE);
    }
    else
    {
      packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, 0);
    }
    enet_peer_send(connection->peer, NUM2UINT(channel), packet);
  }

  rb_mutex_unlock(lock);
  return Qnil;
}
Exemplo n.º 22
0
void sendclientpacket(ENetPacket *packet, int chan, int cn) // INTENSITY: added cn
{
    if(curpeer) enet_peer_send(curpeer, chan, packet);
    else localclienttoserver(chan, packet, cn); // INTENSITY: added cn

    NetworkSystem::Cataloger::packetSent(chan, packet->dataLength); // INTENSITY
}
Exemplo n.º 23
0
void* ThreadSend() {
    while(1) {
        CCLOG("wait mutex to send");

        mutexMapPlay.lock();
//		pthread_mutex_lock(&mutexMapPlay);
        mapLayerToString(playLayer, packetBuffer);
//		strcpy(packetBuffer, "0000000030000003000000000000000000000010000000000\1");
        ENetPacket * packet = enet_packet_create (packetBuffer,
                              150,
                              ENET_PACKET_FLAG_RELIABLE);
        CCLOG(packet->data);
        /* Send the packet to the peer over channel id 0. */
        /* One could also broadcast the packet by */
        /* enet_host_broadcast (host, 0, packet); */
        enet_peer_send (peer, 0, packet);
        /* One could just use enet_host_service() instead. */
        enet_host_flush (server);
        CCLOG("finish send packet");
    }

    enet_peer_disconnect (peer, 0);
    enet_peer_reset (peer);
    CCLOG("exit thread send");
    return NULL;
}
Exemplo n.º 24
0
Arquivo: main.c Projeto: atheros/svc
/**
 * audio callback.
 */
void send_callback(svc_network_packet_t* packet){
	enet_uint32 src;
	ENetPacket* epacket;

	unsigned char* data;

	/* get ID */
	mutex_lock(&client.network_lock);
	src = client.my_id;

	/* this client didn't get it's ID yet (this also handles connection state as my_id==-1 if not connected)
	 * or is muted. */
	if (src == -1 || client.muted) {
		mutex_unlock(&client.network_lock);
		return;
	}

	/* allocate data */
	data = (unsigned char*)malloc(packet->data_len+10);
	*(enet_uint32*)data = ENET_HOST_TO_NET_32(src);
	*(enet_uint32*)(data + 4) = ENET_HOST_TO_NET_32(packet->data_len+2);
	*(enet_uint16*)(data + 8) = ENET_HOST_TO_NET_16(packet->time);

	/* copy audio data */
	memcpy(data + 10, packet->data, packet->data_len);

	epacket = enet_packet_create(data, packet->data_len + 10, ENET_PACKET_FLAG_UNSEQUENCED);
	enet_peer_send(client.client, 1, epacket);
	enet_host_flush(client.host);
	mutex_unlock(&client.network_lock);

	free(data);
}
Exemplo n.º 25
0
/**
	udpr_write : ENetPeer-> data:string -> Channel->Reliable:Bool->void
	Send a full string [data]
**/
static value udpr_write(value p, value data, value chan, value reliable)
{
	ENetPeer *peer;
	ENetPacket *packet;
	int c;
	val_check_kind(p,k_udprpeer);
	val_check(data,string);
	val_check(chan,int);
	val_check(reliable,bool);

	c = val_int(chan);
	peer = (ENetPeer *)val_data(p);
#ifdef ENET_DEBUG_FULL
	fprintf(stderr, "udpr_write: Writing packet '%s' to peer %x on channel %d\n", val_string(data), peer, c);
	fprintf(stderr, "peer state: %d peer channels: %d\n", peer -> state, peer->channelCount);
#endif
	if(peer == NULL || c < 0 || c > 255)
		neko_error();

	packet = enet_packet_create(
		val_string(data),
		val_strlen(data),
		val_bool(reliable) ? ENET_PACKET_FLAG_RELIABLE : 0);
    if(enet_peer_send(peer, c, packet)) {
#ifdef ENET_DEBUG
		fprintf(stderr, "ERROR: udpr_write: enet_peer_send error\n");
		fprintf(stderr, "peer state: %d peer channels: %d\n", peer -> state, peer->channelCount);
#endif
    	neko_error();
    }
	return val_true;
}
Exemplo n.º 26
0
void NetClient::SendServerTimeRequest()
{
    m_packetsendtime = GfTimeClock(); 

    PackedBuffer msg;

    try
    {
        msg.pack_ubyte(SERVER_TIME_REQUEST_PACKET);
    }
//    catch (PackedBufferException &e)
    catch (PackedBufferException)
    {
        GfLogFatal("SendServerTimeRequest: packed buffer error\n");
    }
    GfLogTrace("SendServerTimeRequest: packed data length=%d\n",
            msg.length());


    ENetPacket *pPacket = enet_packet_create (msg.buffer(), 
            msg.length(), 
            ENET_PACKET_FLAG_UNSEQUENCED);

    if (enet_peer_send (m_pServer, UNRELIABLECHANNEL, pPacket))
        GfLogError("SendServerTimeRequest : enet_peer_send failed\n");
}
Exemplo n.º 27
0
bool Peer::sendPacket(ChannelID channel,
                      PacketType type,
                      const PacketData& data)
{
  uint32 flags = 0;

  if (type == RELIABLE)
    flags |= ENET_PACKET_FLAG_RELIABLE;
  else
  {
    if (type == UNSEQUENCED)
      flags |= ENET_PACKET_FLAG_UNSEQUENCED;

    flags |= ENET_PACKET_FLAG_NO_ALLOCATE;
  }

  ENetPacket* packet = enet_packet_create(data.data(), data.size(), flags);
  if (!packet)
  {
    logError("Failed to create ENet packet");
    return false;
  }

  if (enet_peer_send((ENetPeer*) m_peer, channel, packet) < 0)
  {
    logError("Failed to send ENet packet to peer %s", m_name.c_str());
    return false;
  }

  return true;
}
Exemplo n.º 28
0
void NetClient::SetDriverReady(bool bReady)
{
    // Get local driver index in the race driver list
    int idx = GetDriverIdx();

    NetMutexData *pNData = LockNetworkData();
    pNData->m_vecReadyStatus[idx-1] = bReady;
    UnlockNetworkData();

    PackedBuffer msg;

    try
    {
        msg.pack_ubyte(DRIVERREADY_PACKET);
        msg.pack_int(idx);
        msg.pack_int(bReady);
    }
//    catch (PackedBufferException &e)
    catch (PackedBufferException)
    {
        GfLogFatal("SetDriverReady: packed buffer error\n");
    }
    GfLogTrace("SetDriverReady: packed data length=%d\n", msg.length());

    ENetPacket *pPacket = enet_packet_create (msg.buffer(), 
            msg.length(), 
            ENET_PACKET_FLAG_RELIABLE);

    if (enet_peer_send (m_pServer, RELIABLECHANNEL, pPacket)==0)
        return;
}
Exemplo n.º 29
0
void NetClient::SendReadyToStartPacket()
{

    std::string strDName = GetDriverName();
    GfLogTrace("Sending ready to start packet\n");

    PackedBuffer msg;

    try
    {
        msg.pack_ubyte(CLIENTREADYTOSTART_PACKET);
        msg.pack_stdstring(strDName);
    }
//    catch (PackedBufferException &e)
    catch (PackedBufferException)
    {
        GfLogFatal("SendReadyToStartPacket: packed buffer error\n");
    }
    GfLogTrace("SendReadyToStartPacket: packed data length=%d\n",
            msg.length());

    ENetPacket *pPacket = enet_packet_create (msg.buffer(), 
            msg.length(), 
            ENET_PACKET_FLAG_RELIABLE);

    if (enet_peer_send (m_pServer, RELIABLECHANNEL, pPacket))
        GfLogError("SendReadyToStartPacket : enet_peer_send failed\n");
}
Exemplo n.º 30
0
void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
    Packet in_packet;
    in_packet.Append(event->packet->data, event->packet->dataLength);
    in_packet.IgnoreBytes(sizeof(u8));         // Message type
    in_packet.IgnoreBytes(sizeof(u8));         // WifiPacket Type
    in_packet.IgnoreBytes(sizeof(u8));         // WifiPacket Channel
    in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address
    MacAddress destination_address;
    in_packet >> destination_address;

    Packet out_packet;
    out_packet.Append(event->packet->data, event->packet->dataLength);
    ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
                                                 ENET_PACKET_FLAG_RELIABLE);

    if (destination_address == BroadcastMac) { // Send the data to everyone except the sender
        std::lock_guard<std::mutex> lock(member_mutex);
        bool sent_packet = false;
        for (const auto& member : members) {
            if (member.peer != event->peer) {
                sent_packet = true;
                enet_peer_send(member.peer, 0, enet_packet);
            }
        }

        if (!sent_packet) {
            enet_packet_destroy(enet_packet);
        }
    } else { // Send the data only to the destination client
        std::lock_guard<std::mutex> lock(member_mutex);
        auto member = std::find_if(members.begin(), members.end(),
                                   [destination_address](const Member& member) -> bool {
                                       return member.mac_address == destination_address;
                                   });
        if (member != members.end()) {
            enet_peer_send(member->peer, 0, enet_packet);
        } else {
            LOG_ERROR(Network,
                      "Attempting to send to unknown MAC address: "
                      "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
                      destination_address[0], destination_address[1], destination_address[2],
                      destination_address[3], destination_address[4], destination_address[5]);
            enet_packet_destroy(enet_packet);
        }
    }
    enet_host_flush(server);
}