-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsocket.cpp
44 lines (33 loc) · 1.26 KB
/
socket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Copyright (C) 2010 IsmAvatar <[email protected]>
This file is part of EnigmaNet
EnigmaNet is free software and comes with ABSOLUTELY NO WARRANTY.
See LICENSE for details.
*/
#include "net.h"
#ifdef _WIN32
#define DLLEXPORT extern "C" __declspec(dllexport)
#else
#define DLLEXPORT extern "C"
#endif
DLLEXPORT double dllInit() { return net_init(); }
double doConnect(char *ip, double port, double mode, bool serve, bool udp)
{
char sport[6]; //can't exceed 65535
snprintf(sport,6,"%f",port);
int s = net_connect(ip,sport,serve,udp);
if (s < 0) return -1;
net_blocking(s,(char) mode);
return s;
}
DLLEXPORT double tcpconnect(char*ip, double port, double mode) { return doConnect(ip,port,mode,true,false); }
DLLEXPORT double tcplisten(double port, double max, double mode) { return doConnect(NULL,port,mode,false,false); }
DLLEXPORT double udpconnect(double port, double mode) { return doConnect(NULL,port,mode,false,true); }
DLLEXPORT double closesock(double sockid) { return closesocket((int)sockid) == 0 ? 1 : -1; }
DLLEXPORT double setsync(double sockid, double mode) { net_blocking((int)sockid,(char)mode) == 0 ? 1 : -1; }
DLLEXPORT double tcpaccept(double sockid, double mode)
{
int s = net_accept((int) sockid);
if (s >= 0) net_blocking(s,(char) mode);
return s;
}