-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/localcc/360AsController
- Loading branch information
Showing
5 changed files
with
62 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters