104 lines
3.3 KiB
C++
Executable File
104 lines
3.3 KiB
C++
Executable File
/*
|
|
PIP - Platform Independent Primitives
|
|
Ethernet, UDP
|
|
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PIETHERNET_H
|
|
#define PIETHERNET_H
|
|
|
|
#include "pitimer.h"
|
|
#include "piiodevice.h"
|
|
#ifndef WINDOWS
|
|
# include <netinet/in.h>
|
|
# include <arpa/inet.h>
|
|
# include <sys/socket.h>
|
|
# include <fcntl.h>
|
|
#else
|
|
# ifdef CC_VC
|
|
# include <winsock.h>
|
|
# define SHUT_RDWR 2
|
|
# else
|
|
# include <winsock2.h>
|
|
# define SHUT_RDWR SD_BOTH
|
|
# endif
|
|
#endif
|
|
|
|
class PIEthernet: public PIIODevice
|
|
{
|
|
public:
|
|
// slot is any function format "bool <func>(void*, uchar*, int)"
|
|
PIEthernet(void * data, ReadRetFunc slot);
|
|
|
|
enum Type {UDP, TCP_Client, TCP_Server};
|
|
|
|
PIEthernet(Type type = UDP, void * data = 0, ReadRetFunc slot = 0);
|
|
~PIEthernet();
|
|
|
|
void setReadAddress(PIString ip, int port) {path_ = ip + ":" + PIString::fromNumber(port);}
|
|
void setReadAddress(PIString ip_port) {path_ = ip_port;}
|
|
void setSendAddress(PIString ip, int port) {ip_s = ip; port_s = port;}
|
|
void setSendAddress(PIString ip_port) {parseAddress(ip_port, &ip_s, &port_s);}
|
|
|
|
Type type() const {return type_;}
|
|
|
|
bool connect(PIString ip, int port);
|
|
bool connect(PIString ip_port) {parseAddress(ip_port, &ip_c, &port_c); return connect(ip_c, port_c);}
|
|
bool isConnected() const {return connected_;}
|
|
|
|
bool listen();
|
|
bool listen(PIString ip, int port) {setReadAddress(ip, port); return listen();}
|
|
bool listen(PIString ip_port) {setReadAddress(ip_port); return listen();}
|
|
|
|
PIEthernet * client(int index) {return clients_[index];}
|
|
int clientsCount() const {return clients_.size_s();}
|
|
PIVector<PIEthernet * > clients() {return clients_;}
|
|
|
|
bool send(PIString ip, int port, const void * data, int size) {ip_s = ip; port_s = port; return send(data, size);}
|
|
bool send(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);}
|
|
|
|
int read(void * read_to, int max_size);
|
|
int write(const void * data, int max_size);
|
|
|
|
protected:
|
|
PIEthernet(int sock, PIString ip_port);
|
|
|
|
bool init();
|
|
bool openDevice();
|
|
bool closeDevice();
|
|
#ifdef WINDOWS
|
|
void closeSocket(int & sd) {if (sd != -1) closesocket(sd); sd = -1;}
|
|
#else
|
|
void closeSocket(int & sd) {if (sd != -1) ::close(sd); sd = -1;}
|
|
#endif
|
|
void parseAddress(const PIString & ipp, PIString * ip, int * port);
|
|
|
|
int sock, port_, port_s, port_c, wrote;
|
|
bool connected_;
|
|
sockaddr_in addr_, saddr_;
|
|
PIString ip_, ip_s, ip_c;
|
|
PIThread server_thread_;
|
|
PIVector<PIEthernet * > clients_;
|
|
Type type_;
|
|
|
|
private:
|
|
static void server_func(void * eth);
|
|
|
|
};
|
|
|
|
#endif // PIETHERNET_H
|