Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
localcc committed Jan 10, 2020
2 parents 14468a4 + ca07a52 commit 9dec964
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 83 deletions.
85 changes: 31 additions & 54 deletions WindowsDriver/WindowsDriver/client.cpp
Original file line number Diff line number Diff line change
@@ -1,68 +1,45 @@
//
// Created by local on 03.01.2020.
//

#include "client.h"
#include <cstdio>

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<sockaddr*>(&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<SOCKADDR*>(&from), &size);

void client::disconnect() {
shutdown(sock_fd, 0);
return ret;
}

void client::close_socket() {
closesocket(sock_fd);
}
31 changes: 17 additions & 14 deletions WindowsDriver/WindowsDriver/client.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#pragma once
//
// Created by local on 03.01.2020.
//

#include <winsock2.h>

#ifndef UDPCLIENT_CLIENT_H
#define UDPCLIENT_CLIENT_H
#include <ws2tcpip.h>
#include <mutex>



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;
};
#endif //UDPCLIENT_CLIENT_H
20 changes: 9 additions & 11 deletions WindowsDriver/WindowsDriver/feeder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ 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;
c_instance = this;
}

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);
Expand All @@ -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;
Expand All @@ -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);
}


Expand All @@ -75,20 +73,20 @@ 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<PFN_VIGEM_X360_NOTIFICATION>(&feeder::controller_callback));

vigem_target_x360_update(this->driver_client, this->driver_target, controller_conversion::data_to_report(buf));

}

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();
}

4 changes: 2 additions & 2 deletions WindowsDriver/WindowsDriver/feeder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
};
5 changes: 3 additions & 2 deletions WindowsDriver/WindowsDriver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9dec964

Please sign in to comment.