79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
Ethernet, UDP
|
|
Copyright (C) 2011 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 "pithread.h"
|
|
#include "pistring.h"
|
|
#ifndef WINDOWS
|
|
# include <netinet/in.h>
|
|
# include <arpa/inet.h>
|
|
# include <sys/socket.h>
|
|
#else
|
|
# ifdef CC_VC
|
|
# include <winsock.h>
|
|
# define SHUT_RDWR 2
|
|
# else
|
|
# include <winsock2.h>
|
|
# define SHUT_RDWR SD_BOTH
|
|
# endif
|
|
#endif
|
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
typedef bool (*EthernetFunc)(void * , uchar * , int );
|
|
|
|
class PIEthernet: public PIThread
|
|
{
|
|
public:
|
|
// slot is any function format "bool <func>(void*, uchar*, int)"
|
|
PIEthernet(PIString ip = "", int port = 0, void * data = 0, EthernetFunc slot = 0);
|
|
~PIEthernet();
|
|
|
|
void setSlot(EthernetFunc func) {ret_func = func;}
|
|
void setData(void * d) {data = d;}
|
|
void setReadAddress(PIString ip, int port) {ip_ = ip; port_ = port;}
|
|
void setSendAddress(PIString ip, int port) {ip_s = ip; port_s = port;}
|
|
|
|
bool send(PIString ip, int port, char * data, int size);
|
|
bool send(uchar * data, int size);
|
|
bool init();
|
|
bool initSend();
|
|
bool receiverInitialized() const {return sock != -1;}
|
|
bool senderInitialized() const {return sock_s != -1;}
|
|
void terminate();
|
|
const uchar * buffer() const {return buffer_;}
|
|
|
|
private:
|
|
void begin();
|
|
void run();
|
|
void end();
|
|
|
|
int sock, sock_s, port_, port_s, wrote, readed, tries;
|
|
sockaddr_in addr_, saddr_;
|
|
PIString ip_, ip_s;
|
|
EthernetFunc ret_func;
|
|
void * data;
|
|
uchar buffer_[BUFFER_SIZE];
|
|
|
|
};
|
|
|
|
#endif // PIETHERNET_H
|