Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
openjazz
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PocketInsanity
openjazz
Commits
630b578a
Commit
630b578a
authored
Oct 28, 2009
by
anotherguest
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial SDL_Net support. Not tested yet though!
parent
5a60ec98
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
15 deletions
+72
-15
network.cpp
src/io/network.cpp
+63
-14
network.h
src/io/network.h
+9
-1
No files found.
src/io/network.cpp
View file @
630b578a
...
...
@@ -24,7 +24,9 @@
#include "gfx/font.h"
#include "gfx/video.h"
#include "network.h"
#ifdef USE_SDL_NET
#include <arpa/inet.h>
#else
#ifdef WIN32
#include <winsock.h>
#define ioctl ioctlsocket
...
...
@@ -39,33 +41,48 @@
#include <unistd.h>
#include <errno.h>
#endif
#endif
Network
::
Network
()
{
#ifdef USE_SDL_NET
SDLNet_Init
();
#else
#ifdef WIN32
WSADATA
WSAData
;
// Start Windows Sockets
WSAStartup
(
MAKEWORD
(
1
,
0
),
&
WSAData
);
#endif
#endif
return
;
}
Network
::~
Network
()
{
#ifdef USE_SDL_NET
SDLNet_Quit
();
#else
#ifdef WIN32
// Shut down Windows Sockets
WSACleanup
();
#endif
#endif
return
;
}
int
Network
::
host
()
{
#ifdef USE_SDL_NET
ipAddress
.
port
=
NET_PORT
;
ipAddress
.
host
=
0
;
socket
=
SDLNet_TCP_Open
(
&
ipAddress
);
if
(
socket
==
NULL
)
return
E_N_SOCKET
;
return
(
int
)
socket
;
#else
sockaddr_in
sockAddr
;
int
sock
,
nonblock
;
...
...
@@ -102,11 +119,24 @@ int Network::host () {
}
return
sock
;
#endif
}
int
Network
::
join
(
char
*
address
)
{
#ifdef USE_SDL_NET
clearScreen
(
0
);
fontmn2
->
showString
(
"CONNECTING TO SERVER"
,
screenW
>>
2
,
(
screenH
>>
1
)
-
16
);
loop
(
NORMAL_LOOP
);
ipAddress
.
port
=
NET_PORT
;
ipAddress
.
host
=
inet_addr
(
address
);
socket
=
SDLNet_TCP_Open
(
&
ipAddress
);
if
(
socket
==
NULL
)
return
-
1
;
return
(
int
)
socket
;
#else
sockaddr_in
sockAddr
;
fd_set
writefds
;
timeval
timeouttv
;
...
...
@@ -197,11 +227,17 @@ int Network::join (char *address) {
}
return
sock
;
#endif
}
int
Network
::
accept
(
int
sock
)
{
#ifdef USE_SDL_NET
clientSocket
=
SDLNet_TCP_Accept
((
TCPsocket
)
sock
);
if
(
clientSocket
==
NULL
)
return
-
1
;
return
(
int
)
&
clientSocket
;
#else
sockaddr_in
sockAddr
;
int
clientSocket
,
length
;
...
...
@@ -218,35 +254,45 @@ int Network::accept (int sock) {
}
return
clientSocket
;
#endif
}
void
Network
::
close
(
int
sock
)
{
#ifdef USE_SDL_NET
SDLNet_TCP_Close
((
TCPsocket
)
sock
);
#else
#ifdef WIN32
closesocket
(
sock
);
#else
::
close
(
sock
);
#endif
#endif
return
;
}
int
Network
::
send
(
int
sock
,
unsigned
char
*
buffer
)
{
#ifdef USE_SDL_NET
return
SDLNet_TCP_Send
((
TCPsocket
)
sock
,
(
char
*
)
buffer
,
buffer
[
0
]);
#else
return
::
send
(
sock
,
(
char
*
)
buffer
,
buffer
[
0
],
MSG_NOSIGNAL
);
#endif
}
int
Network
::
recv
(
int
sock
,
unsigned
char
*
buffer
,
int
length
)
{
#ifdef USE_SDL_NET
return
SDLNet_TCP_Recv
((
TCPsocket
)
sock
,
buffer
,
length
);
#else
return
::
recv
(
sock
,
(
char
*
)
buffer
,
length
,
MSG_NOSIGNAL
);
#endif
}
bool
Network
::
isConnected
(
int
sock
)
{
#ifdef USE_SDL_NET
return
SDLNet_SocketReady
((
TCPsocket
)
sock
);
#else
int
length
;
char
buffer
;
...
...
@@ -255,18 +301,21 @@ bool Network::isConnected (int sock) {
// Still connected if data was received or if there was no data to receive
return
(
length
!=
-
1
)
||
(
getError
()
==
EWOULDBLOCK
);
#endif
}
int
Network
::
getError
()
{
#ifdef USE_SDL_NET
return
(
int
)
SDLNet_GetError
();
#else
#ifdef WIN32
return
WSAGetLastError
();
#else
return
errno
;
#endif
#endif
}
src/io/network.h
View file @
630b578a
...
...
@@ -24,7 +24,9 @@
#include "OpenJazz.h"
#ifdef USE_SDL_NET
#include <SDL_net.h>
#endif
// Constants
...
...
@@ -55,6 +57,12 @@ class Network {
int
recv
(
int
sock
,
unsigned
char
*
buffer
,
int
length
);
bool
isConnected
(
int
sock
);
int
getError
();
#ifdef USE_SDL_NET
TCPsocket
socket
;
TCPsocket
clientSocket
;
IPaddress
ipAddress
;
SDLNet_SocketSet
socketset
;
#endif
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment