From a69f7258f48e48a920281b292428fe96eaafe5f9 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Mon, 23 Jan 2017 22:32:15 -0800 Subject: [PATCH] Netork control of the camera now works, slight hitch every few seconds though. Not sure how to track that down --- include/Input.h | 3 - include/NetworkInput.h | 44 +++++++++++ src/Camera.cpp | 21 +++++- src/NetworkInput.cpp | 161 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 61 ++-------------- 5 files changed, 229 insertions(+), 61 deletions(-) create mode 100644 include/NetworkInput.h create mode 100644 src/NetworkInput.cpp diff --git a/include/Input.h b/include/Input.h index 0ef4249..f8301a0 100644 --- a/include/Input.h +++ b/include/Input.h @@ -25,8 +25,6 @@ private: void transpose_sf_events(std::list event_queue); - // Network controller class - std::vector held_keys; std::vector held_mouse_buttons; @@ -36,7 +34,6 @@ private: private: std::list> event_queue; - }; class WindowHandler : public VrEventSubscriber { diff --git a/include/NetworkInput.h b/include/NetworkInput.h new file mode 100644 index 0000000..def0543 --- /dev/null +++ b/include/NetworkInput.h @@ -0,0 +1,44 @@ +#include +#include +#include "Event.hpp" +#include "Pub_Sub.h" +#include + +struct CustomPacket { + + char data[1024]; + int position = 0; + int size = 12; + +}; + +class NetworkInput : public VrEventPublisher { +public: + NetworkInput(); + + void listen_for_clients(int port); + void stop_listening_for_clients(); + + void recieve_from_clients(); + void stop_recieving_from_clients(); + + void generate_events(); + void dispatch_events(); + + +private: + + std::list> event_queue; + + std::vector client_sockets; + sf::SocketSelector socket_selector; + + std::thread *client_listener_thread; + std::thread *client_reciever_thread; + + void threaded_client_listener(int port); + void threaded_client_reciever(); + + sf::TcpListener listener; + +}; diff --git a/src/Camera.cpp b/src/Camera.cpp index 4775a29..ae8cc22 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -96,10 +96,9 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptrcode == sf::Keyboard::LShift) { default_impulse = 0.01f; } - if (held_event->code == sf::Keyboard::RShift) { + else if (held_event->code == sf::Keyboard::RShift) { default_impulse = 1.0f; } - else if (held_event->code == sf::Keyboard::C) { look_at_center(); } @@ -130,7 +129,7 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr(event.get()); - if (key_event->code == sf::Keyboard::Y) { + if (key_event->code == sf::Keyboard::M) { if (mouse_enabled) mouse_enabled = false; else @@ -156,6 +155,22 @@ void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptrtype == vr::Event::JoystickMoved) { + + vr::JoystickMoved *joystick_event = static_cast(event.get()); + + if (joystick_event->axis == sf::Joystick::Axis::X) { + movement.x -= joystick_event->position / 5; + //add_relative_impulse(Camera::DIRECTION::FORWARD, joystick_event->position); + } + else if (joystick_event->axis == sf::Joystick::Axis::Y) { + movement.y += joystick_event->position / 5; + //add_relative_impulse(Camera::DIRECTION::RIGHT, joystick_event->position); + } + //else if (joystick_event->axis == sf::Joystick::Axis::Z) { + // add_relative_impulse(Camera::DIRECTION::DOWN, joystick_event->position); + //} + } } diff --git a/src/NetworkInput.cpp b/src/NetworkInput.cpp new file mode 100644 index 0000000..7f2e3d7 --- /dev/null +++ b/src/NetworkInput.cpp @@ -0,0 +1,161 @@ +#include "NetworkInput.h" +#include + +NetworkInput::NetworkInput() { + +} + +void NetworkInput::listen_for_clients(int port) { + //listener.listen(port); + client_listener_thread = new std::thread(&NetworkInput::threaded_client_listener, this, port); +} + +void NetworkInput::stop_listening_for_clients() { + delete client_listener_thread; +} + +void NetworkInput::recieve_from_clients() +{ + //// Receive a message from the client + //char buffer[1024]; + + //std::vector packets; + + //sf::TcpSocket::Status status; + + //do { + + // std::size_t received = 0; + // status = socket.receive(buffer, 1024, received); + + // while (received < 12) { + // std::size_t tack_on; + // status = socket.receive(&buffer[received], 1024 - received, tack_on); + // received += tack_on; + // } + + + // int position = 0; + // while (position < received) { + // CustomPacket p; + // memcpy(p.data, &buffer[position], p.size); + // packets.push_back(p); + // position += p.size; + // } + + // std::cout << "packet_count = " << packets.size() << std::endl; + + // int left_over = 12 - (position - received); + // memcpy(buffer, &buffer[received - left_over], left_over); + + //} while (status != sf::TcpSocket::Status::Disconnected); +} + +void NetworkInput::dispatch_events() +{ + while (event_queue.size() != 0) { + notify_subscribers(std::move(event_queue.front())); + event_queue.pop_front(); + } +} + +void NetworkInput::threaded_client_listener(int port) { + + listener.listen(port); + socket_selector.add(listener); + + while (true) + { + // Make the selector wait for data on any socket + if (socket_selector.wait()) + { + // Test the listener + if (socket_selector.isReady(listener)) + { + // The listener is ready: there is a pending connection + sf::TcpSocket* client = new sf::TcpSocket; + if (listener.accept(*client) == sf::Socket::Done) + { + // Add the new client to the clients list + client_sockets.push_back(client); + + // Add the new client to the selector so that we will + // be notified when he sends something + socket_selector.add(*client); + } + else + { + // Error, we won't get a new connection, delete the socket + delete client; + } + } + else + { + // The listener socket is not ready, test all other sockets (the clients) + for (std::vector::iterator it = client_sockets.begin(); it != client_sockets.end(); ++it) + { + sf::TcpSocket& client = **it; + if (socket_selector.isReady(client)) + { + // Receive a message from the client + char buffer[1024]; + + std::vector packets; + + sf::TcpSocket::Status status; + + do { + + std::size_t received = 0; + status = client.receive(buffer, 1024, received); + + while (received < 12) { + std::size_t tack_on; + status = client.receive(&buffer[received], 1024 - received, tack_on); + received += tack_on; + } + + + int position = 0; + while (position < received) { + CustomPacket p; + memcpy(p.data, &buffer[position], p.size); + packets.push_back(p); + position += p.size; + } + + std::cout << "packet_count = " << packets.size() << std::endl; + + int left_over = 12 - (position - received); + memcpy(buffer, &buffer[received - left_over], left_over); + + } while (status != sf::TcpSocket::Status::Done); + + + for (auto i: packets) { + + float x; + float y; + float z; + + memcpy(&x, &i.data, sizeof(x)); + memcpy(&y, &i.data[4], sizeof(y)); + memcpy(&z, &i.data[8], sizeof(z)); + + event_queue.push_back(std::make_unique(vr::JoystickMoved(sf::Joystick::Axis::X, 0, x))); + event_queue.push_back(std::make_unique(vr::JoystickMoved(sf::Joystick::Axis::Y, 0, y))); + event_queue.push_back(std::make_unique(vr::JoystickMoved(sf::Joystick::Axis::Z, 0, z))); + + std::cout << "X: " << x << " Y: " << y << " Z: " << z << std::endl; + } + } + } + } + } + } +} + +void NetworkInput::threaded_client_reciever() +{ + +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8f8c227..1fe306f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ #include "Software_Caster.h" #include "Input.h" #include "Pub_Sub.h" +#include "NetworkInput.h" const int WINDOW_X = 1000; const int WINDOW_Y = 1000; @@ -65,65 +66,13 @@ sf::Texture window_texture; // Y: -1.57 is straight up // Y: 1.57 is straight down -struct CustomPacket { - - char data[1024]; - int position = 0; - int size = 12; - -}; int main() { - - // Create a listener to wait for incoming connections on port 55001 - sf::TcpListener listener; - listener.listen(5000); - - // Wait for a connection - sf::TcpSocket socket; - listener.accept(socket); - std::cout << "New client connected: " << socket.getRemoteAddress() << std::endl; - - // Receive a message from the client - char buffer[1024]; + NetworkInput ni; + ni.listen_for_clients(5000); - std::vector packets; - - sf::TcpSocket::Status status; - - do { - - std::size_t received = 0; - status = socket.receive(buffer, 1024, received); - - while (received < 12) { - std::size_t tack_on; - status = socket.receive(&buffer[received], 1024 - received, tack_on); - received += tack_on; - } - - - int position = 0; - while (position < received) { - CustomPacket p; - memcpy(p.data, &buffer[position], p.size); - packets.push_back(p); - position += p.size; - } - - std::cout << "packet_count = " << packets.size() << std::endl; - - int left_over = 12 - (position - received); - memcpy(buffer, &buffer[received - left_over], left_over); - - } while (status != sf::TcpSocket::Status::Disconnected); - - - - - //Map _map(sf::Vector3i(0, 0, 0)); //_map.generate_octree(); @@ -229,6 +178,7 @@ int main() { camera->subscribe_to_publisher(&input_handler, vr::Event::EventType::KeyHeld); camera->subscribe_to_publisher(&input_handler, vr::Event::EventType::KeyPressed); camera->subscribe_to_publisher(&input_handler, vr::Event::EventType::MouseMoved); + camera->subscribe_to_publisher(&ni, vr::Event::EventType::JoystickMoved); WindowHandler win_hand(&window); win_hand.subscribe_to_publisher(&input_handler, vr::Event::EventType::Closed); @@ -240,6 +190,7 @@ int main() { input_handler.consume_sf_events(&window); input_handler.handle_held_keys(); input_handler.dispatch_events(); + ni.dispatch_events(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::F11)) { @@ -274,7 +225,7 @@ int main() { light_vec.at(0).orbit_around_center(timer_accumulator += delta_time); } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num0)) { std::string path = "../assets/"; std::string filename; std::getline(std::cin, filename);