52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
#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
|
|
# include <winsock2.h>
|
|
# define SHUT_RDWR SD_BOTH
|
|
#endif
|
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
typedef bool (*EthernetFunc)(void * , char * );
|
|
|
|
class PIEthernet: public PIThread
|
|
{
|
|
public:
|
|
// slot is any function format "bool <func>(void*, char*)"
|
|
PIEthernet(PIString ip, int port, void * data, EthernetFunc slot = 0);
|
|
~PIEthernet();
|
|
|
|
void setSlot(EthernetFunc func) {ret_func = func;}
|
|
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(char * data, int size);
|
|
bool init();
|
|
bool initialized() const {return sock != -1;}
|
|
|
|
private:
|
|
void begin();
|
|
void run();
|
|
void end();
|
|
|
|
|
|
int sock, sock_s, port_, port_s, wrote;
|
|
sockaddr_in addr_, saddr_;
|
|
PIString ip_, ip_s;
|
|
EthernetFunc ret_func;
|
|
char * buffer;
|
|
void * data;
|
|
int readed;
|
|
|
|
};
|
|
|
|
#endif // PIETHERNET_H
|