4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen
This commit is contained in:
26
piethernet.h
26
piethernet.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ethernet, UDP
|
||||
Ethernet, UDP/TCP Broadcast/Multicast
|
||||
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
@@ -24,8 +24,9 @@
|
||||
#include "piiodevice.h"
|
||||
#include "piprocess.h"
|
||||
|
||||
class PIEthernet: public PIIODevice
|
||||
class PIP_EXPORT PIEthernet: public PIIODevice
|
||||
{
|
||||
PIOBJECT(PIEthernet)
|
||||
friend class PIPeer;
|
||||
public:
|
||||
// slot is any function format "bool <func>(void*, uchar*, int)"
|
||||
@@ -35,7 +36,7 @@ public:
|
||||
enum Parameters {ReuseAddress = 0x1, Broadcast = 0x2};
|
||||
|
||||
PIEthernet(Type type = UDP, void * data = 0, ReadRetFunc slot = 0);
|
||||
~PIEthernet();
|
||||
virtual ~PIEthernet();
|
||||
|
||||
void setReadAddress(const PIString & ip, int port) {path_ = ip + ":" + PIString::fromNumber(port);}
|
||||
void setReadAddress(const PIString & ip_port) {path_ = ip_port;}
|
||||
@@ -61,8 +62,9 @@ public:
|
||||
bool joinMulticastGroup(const PIString & group);
|
||||
bool leaveMulticastGroup(const PIString & group);
|
||||
|
||||
bool connect(const PIString & ip, int port);
|
||||
bool connect(const PIString & ip_port) {parseAddress(ip_port, &ip_c, &port_c); return connect(ip_c, port_c);}
|
||||
bool connect();
|
||||
bool connect(const PIString & ip, int port) {path_ = ip + ":" + PIString::fromNumber(port); return connect();}
|
||||
bool connect(const PIString & ip_port) {path_ = ip_port; return connect();}
|
||||
bool isConnected() const {return connected_;}
|
||||
|
||||
bool listen();
|
||||
@@ -73,9 +75,10 @@ public:
|
||||
int clientsCount() const {return clients_.size_s();}
|
||||
PIVector<PIEthernet * > clients() {return clients_;}
|
||||
|
||||
bool send(const PIString & ip, int port, const void * data, int size) {ip_s = ip; port_s = port; return send(data, size);}
|
||||
bool send(const PIString & ip_port, const void * data, int size) {parseAddress(ip_port, &ip_s, &port_s); return send(data, size);}
|
||||
bool send(const void * data, int size) {return (write(data, size) == size);}
|
||||
bool send(const PIString & ip, int port, const void * data, int size, bool threaded = false) {ip_s = ip; port_s = port; if (threaded) {writeThreaded(data, size); return true;} return send(data, size);}
|
||||
bool send(const PIString & ip_port, const void * data, int size, bool threaded = false) {parseAddress(ip_port, &ip_s, &port_s); if (threaded) {writeThreaded(data, size); return true;} return send(data, size);}
|
||||
bool send(const void * data, int size, bool threaded = false) {if (threaded) {writeThreaded(data, size); return true;} return (write(data, size) == size);}
|
||||
bool send(const PIByteArray & ba, bool threaded = false) {if (threaded) {writeThreaded(ba); return true;} return (write(ba) == ba.size_s());}
|
||||
|
||||
int read(void * read_to, int max_size);
|
||||
int write(const void * data, int max_size);
|
||||
@@ -84,6 +87,10 @@ public:
|
||||
static PIStringList interfaces();
|
||||
static PIString interfaceAddress(const PIString & interface_);
|
||||
static PIStringList allAddresses();
|
||||
|
||||
EVENT1(newConnection, PIEthernet * , client)
|
||||
EVENT0(connected)
|
||||
EVENT1(disconnected, bool, withError)
|
||||
|
||||
protected:
|
||||
PIEthernet(int sock, PIString ip_port);
|
||||
@@ -106,6 +113,7 @@ protected:
|
||||
PIString ip_, ip_s, ip_c;
|
||||
PIThread server_thread_;
|
||||
PIVector<PIEthernet * > clients_;
|
||||
PIQueue<PIString> mcast_queue;
|
||||
#ifdef WINDOWS
|
||||
PIMap<PIString, SOCKET> leafs;
|
||||
#endif
|
||||
@@ -115,7 +123,7 @@ protected:
|
||||
private:
|
||||
static void server_func(void * eth);
|
||||
|
||||
static std::string EthErrorString() {
|
||||
static std::string ethErrorString() {
|
||||
#ifdef WINDOWS
|
||||
char * msg;
|
||||
int err = WSAGetLastError();
|
||||
|
||||
Reference in New Issue
Block a user