4 // Based on netSock 0.2 by linuzappz.
5 // The Plugin is free source code.
12 #include <sys/types.h>
19 const unsigned char version = 2; // NET library v2
20 const unsigned char revision = 0;
21 const unsigned char build = 3; // increase that with each version
23 static char *libraryName = N_("Socket Driver");
25 unsigned long CALLBACK PSEgetLibType() {
29 char* CALLBACK PSEgetLibName() {
30 return _(libraryName);
33 unsigned long CALLBACK PSEgetLibVersion() {
34 return version << 16 | revision << 8 | build;
37 long CALLBACK NETinit() {
41 int SEND(const void *buf, int Size, int Mode) {
44 const char *pData = (const char *)buf;
46 if (Mode & PSE_NET_NONBLOCKING) { // NONBLOCKING
52 ret = select(sock + 1, NULL, &wset, NULL, &tm);
53 if (ret == -1) return -1;
55 if (FD_ISSET(sock, &wset)) {
56 return send(sock, pData, Size, 0);
62 bytes = send(sock, pData, Size, 0);
63 if (bytes < 0) return -1;
64 pData += bytes; Size -= bytes;
72 int RECV(void *buf, int Size, int Mode) {
75 char *pData = (char *)buf;
77 if (Mode & PSE_NET_NONBLOCKING) { // NONBLOCKING
83 ret = select(sock, &rset, NULL, NULL, &tm);
85 if (FD_ISSET(sock, &rset)) {
86 return recv(sock, pData, Size, 0);
92 bytes = recv(sock, pData, Size, 0);
93 if (bytes == -1) return -1;
94 pData+= bytes; Size-= bytes;
102 long CALLBACK NETopen(unsigned long *gpuDisp) {
103 int ret = sockOpen();
105 struct sockaddr_in address;
107 if (ret == -1) return -1;
109 if (conf.PlayerNum == 1) {
110 int listen_sock, reuse_addr = 1;
113 memset((char *)&address, 0, sizeof (address));
115 address.sin_family = AF_INET;
116 address.sin_port = htons(conf.PortNum);
117 address.sin_addr.s_addr = INADDR_ANY;
119 listen_sock = socket(AF_INET, SOCK_STREAM, 0);
120 if (listen_sock == -1)
123 setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse_addr, sizeof(reuse_addr));
125 if (bind(listen_sock,(struct sockaddr *) &address, sizeof(address)) == -1)
128 if (listen(listen_sock, 1) != 0)
138 FD_SET(listen_sock, &rset);
140 ret = select(listen_sock + 1, &rset, NULL, NULL, &tm);
141 if (FD_ISSET(listen_sock, &rset)) {
142 sock = accept(listen_sock, NULL, NULL);
145 if (WaitCancel) break;
150 sockDestroyWaitDlg();
151 if (WaitCancel == 1) return -1;
153 memset((char *)&address, 0, sizeof(address));
154 address.sin_family = AF_INET;
155 address.sin_port = htons(conf.PortNum);
156 address.sin_addr.s_addr = inet_addr(conf.ipAddress);
158 sock = socket(AF_INET, SOCK_STREAM, 0);
160 if (connect(sock, (struct sockaddr *)&address, sizeof(address))!=0) {
161 SysMessage(_("error connecting to %s: %s\n"), conf.ipAddress, strerror(errno));
173 Ping = (sockPing() + Ping) / 2;
174 Ping = (sockPing() + Ping) / 2;
176 if (conf.PlayerNum == 1) {
177 PadCountMax = (int)(((double)Ping / 1000.0) * 60.0);
178 if (PadCountMax <= 0) PadCountMax = 1;
179 SEND(&PadCountMax, 4, PSE_NET_BLOCKING);
181 RECV(&PadCountMax, 4, PSE_NET_BLOCKING);
184 PadSendData = (char *)malloc(PadCountMax * 128);
185 if (PadSendData == NULL) {
186 SysMessage(_("Error allocating memory!\n")); return -1;
188 memset(PadSendData, 0xff, PadCountMax);
193 long CALLBACK NETclose() {
199 long CALLBACK NETshutdown() {
200 return sockShutdown();
203 void CALLBACK NETpause() {
204 /* unsigned char Code = 0x80;
206 SEND(&Code, 1, PSE_NET_BLOCKING);*/
209 void CALLBACK NETresume() {
210 /* unsigned char Code = 0x80;
212 SEND(&Code, 1, PSE_NET_BLOCKING);*/
215 long CALLBACK NETsendData(void *pData, int Size, int Mode) {
216 return SEND(pData, Size, Mode);
219 long CALLBACK NETrecvData(void *pData, int Size, int Mode) {
220 return RECV(pData, Size, Mode);
223 long CALLBACK NETsendPadData(void *pData, int Size) {
224 if (PadSendSize == -1) {
227 if (SEND(&PadSendSize, 1, PSE_NET_BLOCKING) == -1)
230 if (RECV(&PadRecvSize, 1, PSE_NET_BLOCKING) == -1)
234 memcpy(&PadSendData[PadCount], pData, Size);
235 if (SEND(pData, PadSendSize, PSE_NET_BLOCKING) == -1)
241 long CALLBACK NETrecvPadData(void *pData, int Pad) {
243 if (conf.PlayerNum == Pad) {
244 memset(pData, 0xff, PadSendSize);
246 memset(pData, 0xff, PadRecvSize);
249 if (conf.PlayerNum == Pad) {
250 memcpy(pData, &PadSendData[PadCount == 0 ? PadCountMax-1 : PadCount-1], PadSendSize);
252 if (RECV(pData, PadRecvSize, PSE_NET_BLOCKING) == -1)
259 if (PadCount == PadCountMax) {
268 long CALLBACK NETqueryPlayer() {
269 return conf.PlayerNum;
272 long CALLBACK NETtest() {