diff --git a/WindowsDriver/WindowsDriver/client.cpp b/WindowsDriver/WindowsDriver/client.cpp index 204cb35..97a6ba1 100644 --- a/WindowsDriver/WindowsDriver/client.cpp +++ b/WindowsDriver/WindowsDriver/client.cpp @@ -1,68 +1,45 @@ +// +// Created by local on 03.01.2020. +// + #include "client.h" #include -client::client(const char* hostname, const char* port) { - WSADATA wsaData; - sock_fd = INVALID_SOCKET; - - result = NULL; - ptr = NULL; - int res = 0; - res = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (res != 0) { - this->~client(); - } - - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - - res = getaddrinfo(hostname, port, &hints, &result); - if (res != 0) { - WSACleanup(); - this->~client(); - } +client::client(const char* hostname, int port) { + int res = 0; + WSAData data; + res = WSAStartup(MAKEWORD(2, 2), &data); + if (res != 0) + this->~client(); + sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock_fd == INVALID_SOCKET) + this->~client(); + + address.sin_family = AF_INET; + + res = InetPton(AF_INET, hostname, &address.sin_addr.s_addr); + if (res < 0) + this->~client(); + address.sin_port = htons(port); } client::~client() { - result = NULL; - ptr = NULL; + closesocket(sock_fd); } -int client::_read(char* data, int len) { - queue_mutex.lock(); - int amount = recv(sock_fd, data, len, 0); - queue_mutex.unlock(); - return amount; -} -int client::_write(char* data, int len) { - queue_mutex.lock(); - int amount = send(sock_fd, data, len, 0); - queue_mutex.unlock(); - return amount; +int client::client_write(char* arr, int amount) { + return sendto(sock_fd, arr, amount, 0, reinterpret_cast(&address), sizeof(address)); } -int client::_connect() { - sock_fd = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if (sock_fd == INVALID_SOCKET) { - WSACleanup(); - return INVALID_SOCKET; - } - int res = 0; - res = connect(sock_fd, result->ai_addr, (int)result->ai_addrlen); - if (res == SOCKET_ERROR) { - closesocket(sock_fd); - sock_fd = INVALID_SOCKET; - return res; - } - freeaddrinfo(result); - return 0; - -} +int client::client_read(char* arr, int amount) { + sockaddr_in from; + int size = sizeof(from); + int ret = recvfrom(sock_fd, arr, amount, 0, reinterpret_cast(&from), &size); -void client::disconnect() { - shutdown(sock_fd, 0); + return ret; } +void client::close_socket() { + closesocket(sock_fd); +} \ No newline at end of file diff --git a/WindowsDriver/WindowsDriver/client.h b/WindowsDriver/WindowsDriver/client.h index 33adc40..a52a051 100644 --- a/WindowsDriver/WindowsDriver/client.h +++ b/WindowsDriver/WindowsDriver/client.h @@ -1,22 +1,25 @@ -#pragma once +// +// Created by local on 03.01.2020. +// -#include + +#ifndef UDPCLIENT_CLIENT_H +#define UDPCLIENT_CLIENT_H #include -#include + class client { public: - client(const char* hostname, const char* port); - ~client(); + client(const char* hostname, int port); + ~client(); + int client_read(char* arr, int amount); + int client_write(char* arr, int amount); + void close_socket(); +private: + SOCKET sock_fd; + sockaddr_in address; +}; - int _connect(); - void disconnect(); - int _read(char* data, int len); - int _write(char* data, int len); -private: - SOCKET sock_fd; - struct addrinfo *result, *ptr, hints; - std::mutex queue_mutex; -}; \ No newline at end of file +#endif //UDPCLIENT_CLIENT_H diff --git a/WindowsDriver/WindowsDriver/feeder.cpp b/WindowsDriver/WindowsDriver/feeder.cpp index 869ae89..81f5713 100644 --- a/WindowsDriver/WindowsDriver/feeder.cpp +++ b/WindowsDriver/WindowsDriver/feeder.cpp @@ -9,8 +9,8 @@ static char cb[1] = { 3 }; feeder* feeder::c_instance = nullptr; -feeder::feeder(const char* hostname, const char* port) { - this->tcp_client = new client(hostname, port); +feeder::feeder(const char* hostname, int port) { + this->udp_client = new client(hostname, port); this->driver_client = vigem_alloc(); this->driver_target = vigem_target_x360_alloc(); connected = false; @@ -18,9 +18,9 @@ feeder::feeder(const char* hostname, const char* port) { } feeder::~feeder() { - this->tcp_client->disconnect(); + this->udp_client->close_socket(); connected = false; - this->tcp_client->~client(); + this->udp_client->~client(); vigem_target_remove(this->driver_client, this->driver_target); vigem_disconnect(this->driver_client); vigem_target_free(this->driver_target); @@ -29,8 +29,6 @@ feeder::~feeder() { } int feeder::connect() { - int res = this->tcp_client->_connect(); - if (res != 0) return res; if (vigem_connect(this->driver_client) != VIGEM_ERROR_NONE) return -80; if (vigem_target_add(this->driver_client, this->driver_target) != VIGEM_ERROR_NONE) return -81; @@ -54,7 +52,7 @@ void feeder::controller_callback(PVIGEM_CLIENT Client, UCHAR RightMotor, UCHAR LedNumber) { char data[3] = { cb[0], LeftMotor, RightMotor }; - feeder::GetInstance()->tcp_client->_write(data, 3); + feeder::GetInstance()->udp_client->client_write(data, 3); } @@ -75,9 +73,9 @@ void feeder::feed() { while (connected) { //Do work - this->tcp_client->_write(get, 1); // asking for controller data + this->udp_client->client_write(get, 1); // asking for controller data memset(buf, 0, 14); - this->tcp_client->_read(buf, 14); // receiving it + this->udp_client->client_read(buf, 14); // receiving it vigem_target_x360_register_notification(this->driver_client, this->driver_target, reinterpret_cast(&feeder::controller_callback)); @@ -85,10 +83,10 @@ void feeder::feed() { } - this->tcp_client->_write(poweroff, 1); + this->udp_client->client_write(poweroff, 1); } void feeder::disconnect() { - this->tcp_client->disconnect(); + this->udp_client->close_socket(); } diff --git a/WindowsDriver/WindowsDriver/feeder.h b/WindowsDriver/WindowsDriver/feeder.h index fd6e01b..0b29327 100644 --- a/WindowsDriver/WindowsDriver/feeder.h +++ b/WindowsDriver/WindowsDriver/feeder.h @@ -5,7 +5,7 @@ class feeder { public: - feeder(const char* hostname, const char* port); + feeder(const char* hostname, int port); ~feeder(); static feeder* GetInstance() { return c_instance; } @@ -27,6 +27,6 @@ class feeder { PVIGEM_CLIENT driver_client; PVIGEM_TARGET driver_target; std::thread sender_thread; - client* tcp_client; + client* udp_client; bool connected; }; \ No newline at end of file diff --git a/WindowsDriver/WindowsDriver/main.cpp b/WindowsDriver/WindowsDriver/main.cpp index 5a43668..e4677c7 100644 --- a/WindowsDriver/WindowsDriver/main.cpp +++ b/WindowsDriver/WindowsDriver/main.cpp @@ -4,10 +4,11 @@ int main(int argc, char* argv[]) { + /* if (argc < 2) { return -1; - } - feeder* f = new feeder(argv[1], "1182"); + }*/ + feeder* f = new feeder("192.168.1.158", 1182); int res = f->connect(); printf("%d\n", res); f->start_feeder_thread();