30.11.2013 - New PICollection namespace, Android support, my own PIVector implementation
This commit is contained in:
@@ -19,17 +19,40 @@
|
||||
|
||||
#include "piethernet.h"
|
||||
#include "piconfig.h"
|
||||
#ifdef QNX
|
||||
# include <net/if_dl.h>
|
||||
# include <hw/nicinfo.h>
|
||||
# include <sys/dcmd_io-net.h>
|
||||
#endif
|
||||
|
||||
|
||||
PIEthernet::PIEthernet(void * data, ReadRetFunc slot): PIIODevice("", ReadWrite) {
|
||||
/** \class PIEthernet
|
||||
* \brief Ethernet device
|
||||
* \details
|
||||
* \section PIEthernet_sec0 Synopsis
|
||||
* %PIEthernet designed to work with IPv4 network by two protocols:
|
||||
* UDP and TCP. This class allow you send and receive packets to/from
|
||||
* another computer through network.
|
||||
*
|
||||
* \section PIEthernet_sec1 IPv4
|
||||
*
|
||||
*
|
||||
* \section PIEthernet_sec2 UDP
|
||||
* User Datagram Protocol
|
||||
*
|
||||
* \section PIEthernet_sec3 TCP
|
||||
* Transmission Control Protocol
|
||||
*
|
||||
* */
|
||||
|
||||
|
||||
PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) {
|
||||
piMonitor.ethernets++;
|
||||
setPriority(piHigh);
|
||||
type_ = UDP;
|
||||
ret_data_ = data;
|
||||
ip_ = ip_s = "";
|
||||
port_ = port_s = 0;
|
||||
sock = sock_s = -1;
|
||||
ret_func_ = slot;
|
||||
connected_ = false;
|
||||
params = PIEthernet::ReuseAddress;
|
||||
server_thread_.setData(this);
|
||||
@@ -38,17 +61,16 @@ PIEthernet::PIEthernet(void * data, ReadRetFunc slot): PIIODevice("", ReadWrite)
|
||||
}
|
||||
|
||||
|
||||
PIEthernet::PIEthernet(PIEthernet::Type type, void * data, ReadRetFunc slot): PIIODevice("", ReadWrite) {
|
||||
PIEthernet::PIEthernet(PIEthernet::Type type, const PIString & ip_port, const PIFlags<PIEthernet::Parameters> params_): PIIODevice(ip_port, ReadWrite) {
|
||||
piMonitor.ethernets++;
|
||||
setPriority(piHigh);
|
||||
type_ = type;
|
||||
ret_data_ = data;
|
||||
ip_ = ip_s = "";
|
||||
port_ = port_s = 0;
|
||||
parseAddress(ip_port, &ip_, &port_);
|
||||
ip_s = "";
|
||||
port_s = 0;
|
||||
sock = sock_s = -1;
|
||||
ret_func_ = slot;
|
||||
connected_ = false;
|
||||
params = (type == UDP ? PIEthernet::ReuseAddress : 0);
|
||||
params = params_;
|
||||
server_thread_.setData(this);
|
||||
setThreadedReadBufferSize(65536);
|
||||
if (type_ != UDP) init();
|
||||
@@ -75,6 +97,7 @@ PIEthernet::~PIEthernet() {
|
||||
if (server_thread_.isRunning()) server_thread_.terminate();
|
||||
stop();
|
||||
closeSocket(sock);
|
||||
closeSocket(sock_s);
|
||||
//if (buffer_ != 0) delete buffer_;
|
||||
//buffer_ = 0;
|
||||
}
|
||||
@@ -83,7 +106,8 @@ PIEthernet::~PIEthernet() {
|
||||
bool PIEthernet::init() {
|
||||
//cout << "init " << type_ << endl;
|
||||
closeSocket(sock);
|
||||
int st = 0, pr = 0;;
|
||||
closeSocket(sock_s);
|
||||
int st = 0, pr = 0;
|
||||
#ifdef WINDOWS
|
||||
int flags = WSA_FLAG_OVERLAPPED;
|
||||
#else
|
||||
@@ -99,10 +123,12 @@ bool PIEthernet::init() {
|
||||
#ifdef WINDOWS
|
||||
if (type_ == UDP) flags = WSA_FLAG_MULTIPOINT_C_LEAF | WSA_FLAG_MULTIPOINT_D_LEAF;
|
||||
sock = WSASocket(AF_INET, st, pr, NULL, 0, flags);
|
||||
sock_s = WSASocket(AF_INET, st, pr, NULL, 0, WSA_FLAG_OVERLAPPED);
|
||||
#else
|
||||
sock = socket(AF_INET, st, pr);
|
||||
sock_s = socket(AF_INET, st, pr);
|
||||
#endif
|
||||
if (sock == -1) {
|
||||
if (sock == -1 || sock_s == -1) {
|
||||
piCoutObj << "Can`t create socket, " << ethErrorString();
|
||||
return false;
|
||||
}
|
||||
@@ -161,6 +187,7 @@ bool PIEthernet::openDevice() {
|
||||
bool PIEthernet::closeDevice() {
|
||||
//cout << "close\n";
|
||||
closeSocket(sock);
|
||||
closeSocket(sock_s);
|
||||
piForeach (PIEthernet * i, clients_)
|
||||
delete i;
|
||||
clients_.clear();
|
||||
@@ -177,7 +204,7 @@ void PIEthernet::closeSocket(int & sd) {
|
||||
shutdown(sd, SD_BOTH);
|
||||
closesocket(sd);
|
||||
#else
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
shutdown(sd, SHUT_RDWR);
|
||||
::close(sd);
|
||||
#endif
|
||||
}
|
||||
@@ -193,7 +220,7 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
return false;
|
||||
}
|
||||
if (!opened_) {
|
||||
if (mcast_queue.has(group))
|
||||
if (mcast_queue.contains(group))
|
||||
return false;
|
||||
mcast_queue.enqueue(group);
|
||||
return true;
|
||||
@@ -353,7 +380,7 @@ int PIEthernet::read(void * read_to, int max_size) {
|
||||
//piCout << "eth" << path_ << "read return" << rs << errno;
|
||||
if (rs <= 0 && type_ == TCP_Client) {
|
||||
connected_ = false;
|
||||
disconnected(rs < 0);
|
||||
if (connected_) disconnected(rs < 0);
|
||||
//piCoutObj << "eth" << path_ << "disconnected";
|
||||
}
|
||||
if (rs > 0) received(read_to, rs);
|
||||
@@ -402,9 +429,9 @@ int PIEthernet::write(const void * data, int max_size) {
|
||||
saddr_.sin_family = AF_INET;
|
||||
//piCout << "[PIEth] write to" << ip_s << ":" << port_s << max_size << "bytes ...";
|
||||
#ifdef WINDOWS
|
||||
return sendto(sock, (const char * )data, max_size, 0, (sockaddr * )&saddr_, sizeof(saddr_));
|
||||
return sendto(sock_s, (const char * )data, max_size, 0, (sockaddr * )&saddr_, sizeof(saddr_));
|
||||
#else
|
||||
return sendto(sock, data, max_size, 0, (sockaddr * )&saddr_, sizeof(saddr_));
|
||||
return sendto(sock_s, data, max_size, 0, (sockaddr * )&saddr_, sizeof(saddr_));
|
||||
#endif
|
||||
//piCout << "[PIEth] write to" << ip_s << ":" << port_s << "ok";
|
||||
case TCP_Client:
|
||||
|
||||
Reference in New Issue
Block a user