Пример #1
0
void joynet_poll_client(JOYNET_CLIENT * cp)
{
	ENetEvent event;
	
	if(cp && cp->host && cp->peer)
	{
		while(enet_host_service(cp->host, &event, 0))
		{
			switch(event.type)
			{
				/* data has been received */
				case ENET_EVENT_TYPE_RECEIVE:
				{
					JOYNET_MESSAGE message;
					joynet_decode_message(&message, &event);
				
					switch(event.channelID)
					{
						case JOYNET_CHANNEL_SYSTEM:
						{
							break;
						}
						case JOYNET_CHANNEL_CHAT:
						{
							joynet_handle_client_chat_message(cp, &message);
							break;
						}
						case JOYNET_CHANNEL_GAME:
						{
							joynet_handle_client_game_message(cp, &message);
							break;
						}
					}
					if(cp->channel_callback[event.channelID])
					{
						cp->channel_callback[event.channelID](&message);
					}
					break;
				}
			   
				case ENET_EVENT_TYPE_DISCONNECT:
				{
					break;
				}
				default:
				{
					break;
				}
			}
			if(cp->global_callback)
			{
				cp->global_callback(&event);
			}
		}
	}
}
Пример #2
0
void joynet_poll_server(JOYNET_SERVER * sp)
{
	if(sp && sp->host)
	{
		while(enet_host_service(sp->host, &sp->event, 0))
		{
			switch(sp->event.type)
			{
				/* a connection has been received */
				case ENET_EVENT_TYPE_CONNECT:
				{
					int client = joynet_server_find_free_client(sp);
					
					if(client >= 0)
					{
						sp->client[client]->peer = sp->event.peer;
					}
					sp->client[client]->peer->data = malloc(sizeof(int));
					if(!sp->client[client]->peer->data)
					{
						/* need error system */
					}
					else
					{
						joynet_serialize(sp->serial_data, sp->client[client]->peer->data);
						joynet_putl(sp->serial_data, client);
					}
					sp->client[client]->user = client;
					strcpy(sp->client[client]->screen_name, "");
					break;
				}
				
				/* client disconnected */
				case ENET_EVENT_TYPE_DISCONNECT:
				{
					sp->client[joynet_get_client_from_peer(sp, sp->event.peer)]->peer = NULL;
					break;
				}
				
				/* packet received */
				case ENET_EVENT_TYPE_RECEIVE:
				{
					JOYNET_MESSAGE message;
					joynet_decode_message(&message, &sp->event);
					switch(sp->event.channelID)
					{
						case JOYNET_CHANNEL_SYSTEM:
						{
							break;
						}
						case JOYNET_CHANNEL_CHAT:
						{
							joynet_handle_server_chat_message(sp, &message);
							break;
						}
						case JOYNET_CHANNEL_GAME:
						{
							joynet_handle_server_game_message(sp, &message);
							break;
						}
					}
					
					if(sp->channel_callback[sp->event.channelID])
					{
						sp->channel_callback[sp->event.channelID](&message);
					}
					break;
				}
				default:
				{
					break;
				}
			}
			if(sp->internal_callback)
			{
				sp->internal_callback(&sp->event);
			}
			if(sp->global_callback)
			{
				sp->global_callback(&sp->event);
			}
		}
	}
}