本文整理汇总了C++中enet_peer_send函数的典型用法代码示例。如果您正苦于以下问题:C++ enet_peer_send函数的具体用法?C++ enet_peer_send怎么用?C++ enet_peer_send使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了enet_peer_send函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: enet_packet_create
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);
}
示例2: joynet_handle_server_chat_message
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;
}
}
}
示例3: connection_send_to
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);
}
}
示例4: enet_packet_create
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;
}
示例5: handle_input_command
/**
* 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;
}
示例6: getClient
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_);
}
示例7: while
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);
}
}
示例8: udpr_send_char
/**
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;
}
示例9: joynet_select_player
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;
}
}
}
示例10: joynet_end_game
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);
}
}
}
}
示例11: joynet_update_game_option
/* 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
{
}
}
示例12: joynet_update_game
/* 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");
}
}
}
示例13: while
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);
}
示例14: udpr_send
/**
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);
}
示例15: joynet_update_game
/* 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++;
}
}
}