platform/packages/wings/lib/src/wings_socket.h

54 lines
1.2 KiB
C
Raw Normal View History

2019-04-27 19:12:09 +00:00
#ifndef WINGS_SOCKET_H
#define WINGS_SOCKET_H
2019-04-29 05:55:02 +00:00
#include <arpa/inet.h>
2019-09-29 05:26:27 +00:00
#include <cstdio>
#include <cstdlib>
2019-04-27 19:12:09 +00:00
#include <dart_api.h>
2019-04-29 05:55:02 +00:00
#include <dart_native_api.h>
2019-09-29 05:26:27 +00:00
#include <list>
2019-04-29 05:55:02 +00:00
#include <memory>
2019-09-29 05:26:27 +00:00
#include <netinet/in.h>
#include <sys/socket.h>
2019-04-29 05:55:02 +00:00
#include <thread>
2019-09-29 05:26:27 +00:00
#include <unistd.h>
2019-04-27 19:12:09 +00:00
#include <vector>
2019-09-29 05:26:27 +00:00
namespace wings {
struct WingsSocketInfo {
2019-04-29 05:55:02 +00:00
const char *address;
uint64_t port;
bool shared;
uint64_t backlog;
bool v6Only;
Dart_Handle sendPortHandle;
2019-05-01 04:29:21 +00:00
bool equals(const WingsSocketInfo &right) const;
2019-04-27 19:12:09 +00:00
};
2019-09-29 05:26:27 +00:00
class WingsSocket {
2019-04-29 05:55:02 +00:00
public:
2019-05-01 01:33:46 +00:00
WingsSocket(sa_family_t family, int sockfd, const WingsSocketInfo &info);
2019-04-29 05:55:02 +00:00
void incrRef(Dart_Port port);
2019-05-01 04:29:21 +00:00
void decrRef(Dart_Port port);
2019-04-29 05:55:02 +00:00
const WingsSocketInfo &getInfo() const;
void start(Dart_NativeArguments arguments);
2019-05-01 01:33:46 +00:00
int getFD() const;
2019-09-29 05:26:27 +00:00
bool isClosed() const;
2019-05-01 01:33:46 +00:00
sa_family_t getFamily() const;
2019-05-01 04:29:21 +00:00
Dart_Port nextPort();
2019-04-27 19:12:09 +00:00
2019-04-29 05:55:02 +00:00
private:
static void threadCallback(Dart_Port dest_port_id, Dart_CObject *message);
WingsSocketInfo info;
2019-09-29 05:26:27 +00:00
std::list<Dart_Port>::iterator portIterator;
2019-04-29 05:55:02 +00:00
int sockfd;
int refCount;
2019-05-01 04:29:21 +00:00
bool open;
2019-05-01 01:33:46 +00:00
sa_family_t family;
2019-04-29 05:55:02 +00:00
std::unique_ptr<std::thread> workerThread;
2019-09-29 05:26:27 +00:00
std::list<Dart_Port> sendPorts;
2019-04-27 19:12:09 +00:00
};
2019-04-29 05:55:02 +00:00
extern std::vector<WingsSocket *> globalSocketList;
2019-04-27 19:12:09 +00:00
} // namespace wings
#endif