indexing description: "[ This class shows how to use the EM_NET_SERVER class of the multiplayer framework. ]" date: "$Date$" revision: "$Revision$" "This class shows how to use the EM_NET_SERVER class of the multiplayer framework." date: "$Date$" revision: "$Revision$" class EM_MP_SERVER_SCENE inherit EM_MP_SAMPLE_SCENE redefine handle_update_event, create_widgets end EM_SHARED_ERROR_HANDLER export {NONE} all end create make feature -- Initialisation make is -- init do -- First make the widget scene make_component_scene -- Setup the client create server.make("em_net_sample",35700) server.set_name ("the cool em sample server") server.enable_discovery_response server.enable_auto_transmit server.enable_sending_heartbeats server.set_net_fps (1) server.create_static_connection (create {EM_INET_SOCKET_ADDRESS}.make_by_hostname_port("eiffelmedia.origo.ethz.ch",35699)) server.last_created_connection.join(server.masterserver_group) server.send_failed_event.subscribe (agent on_network_problem) -- Event subscriptions server.subscribe_by_type_id (server.object_types.em_net_status_message, agent on_status_message(?)) server.subscribe_by_type_id (server.object_types.em_net_status_request, agent on_status_request(?)) server.subscribe_by_type_id (server.object_types.em_net_join_request, agent on_join_request (?)) server.timeout_event.subscribe (agent on_timeout(?)) -- Setup our sample object the_square := server.object_types.create_square_on_circle drawables.container.extend (the_square) server.standard_group.add_object (the_square) the_square.set_is_master_object (true) end create_widgets is -- Create additional widgets local a_grey: EM_COLOR do Precursor create a_grey.make_with_rgb(200,200,200) create netfps_slider.make_from_range_horizontal (1, 24) netfps_slider.set_background_color (a_grey) netfps_slider.set_current_value (5) netfps_slider.set_position (590, 90) netfps_slider.set_dimension (200, 50) netfps_slider.position_changed_event.subscribe (agent on_netfps_changed(?)) add_component(netfps_slider) create netfps_label.make_from_text ("netfps: 0") netfps_label.set_background_color (a_grey) netfps_label.set_x (590); netfps_label.set_y (120); netfps_label.set_width (200) add_component (netfps_label) end feature -- Events handle_update_event is -- outside event do Precursor -- Update every thing with the selected network framerate. -- We only do this to give a good visual impression. -- Normally the game runs with higher fps than the network... if server.time > next_frame then next_frame := server.time + 1000//server.net_fps the_square.move time_label.set_text("time: " + server.time.out) server.publish (server.object_types.create_em_net_status_request) end end on_timeout (a_connection: EM_NET_CONNECTION) is -- Handle timeout event do server.delete_connection (a_connection) end on_status_message(a_message : EM_NET_STATUS_MESSAGE) is -- Handle status message. do status_message.set_text (a_message.item) end on_join_request(a_join_request: EM_NET_JOIN_REQUEST) is -- A client wants to join the party... local a_join_response: EM_NET_JOIN_RESPONSE do -- Move the client to the standard group if not server.standard_group.has_connection (a_join_request.updating_connection) then -- You could alswo wait a little bit until time is synchronized between the two peers. a_join_request.updating_connection.join (server.standard_group) end a_join_response := server.object_types.create_em_net_join_response a_join_response.set_resend_time (300) a_join_response.set_resends_left (2) server.event_2pc_id_manager.set_unique_id_for_object (a_join_response) a_join_response.accept_join a_join_response.set_group (a_join_request.group) a_join_response.publish end on_status_request(a_request: EM_NET_STATUS_REQUEST) is -- Handle status request. local a_message: EM_NET_STATUS_MESSAGE do a_message := server.object_types.create_em_net_status_message a_message.set_status ("?servertime=" + server.time.out + "&servername="+server.name+"?") a_message.set_group (a_request.group) a_message.publish end on_netfps_changed(netfps: INTEGER) is -- Handle netfps changed event. do server.set_net_fps (netfps) netfps_label.set_text ("netfps: " + netfps.out) end on_network_problem (error_code: INTEGER) is -- Handle network problems. local error_dialog: EM_MESSAGE_DIALOG do if error_code = error_handler.Em_error_send_failed then create error_dialog.make_from_error ("Error sending UDP packet. Network down?") elseif error_code = error_handler.Em_error_resolve_ip then create error_dialog.make_from_error ("Error resolving IP. DNS problem?") else create error_dialog.make_from_error ("Unknown network error. This should never happen.") end server.disable_auto_transmit server.disable_sending_heartbeats error_dialog.button_clicked_event.subscribe (agent quit) error_dialog.set_draggable (False) error_dialog.show end feature {NONE} -- Implementation server: EM_NET_SERVER[MY_OBJECT_TYPES] -- Server (very important ;) next_frame: INTEGER -- Next frame -- Needed to give a nice visual impression netfps_slider : EM_SLIDER -- netfps slider netfps_label: EM_LABEL -- netfps label end