15.10.2012 - version 0.2.0 - PIIODevice - base of file, ethernet, serial and packets extractor. PIEthernet now support also TCP (client and server). PIConsole now can align labels in each column individually.
This commit is contained in:
83
piethernet.h
Normal file → Executable file
83
piethernet.h
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ethernet, UDP
|
||||
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
|
||||
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
|
||||
@@ -20,12 +20,13 @@
|
||||
#ifndef PIETHERNET_H
|
||||
#define PIETHERNET_H
|
||||
|
||||
#include "pithread.h"
|
||||
#include "pistring.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>
|
||||
@@ -36,42 +37,66 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
typedef bool (*EthernetFunc)(void * , uchar * , int );
|
||||
|
||||
class PIEthernet: public PIThread
|
||||
class PIEthernet: public PIIODevice
|
||||
{
|
||||
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 * data, ReadRetFunc slot);
|
||||
|
||||
enum Type {UDP, TCP_Client, TCP_Server};
|
||||
|
||||
PIEthernet(Type type = UDP, void * data = 0, ReadRetFunc 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 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 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_;}
|
||||
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:
|
||||
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];
|
||||
static void server_func(void * eth);
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user