Skip to content

Commit

Permalink
Add initial NativeSocket impl, fix oversight with VAO not being exclu…
Browse files Browse the repository at this point in the history
…ded under NO_SDL/NO_GL
  • Loading branch information
SgtCoDFish committed Dec 23, 2015
1 parent 53c9004 commit d0a4b0f
Show file tree
Hide file tree
Showing 11 changed files with 640 additions and 30 deletions.
17 changes: 13 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(APG)

set(VERSION_MAJOR 0)
set(VERSION_MINOR 3)
set(VERSION_PATCH 0)
set(VERSION_PATCH 5)

set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
message("Conifguring APG version ${VERSION}.")
Expand All @@ -20,8 +20,9 @@ else ()
set (APG_DEBUG TRUE)
endif ()

option(APG_NO_SDL "Do not build any component of APG which uses SDL; removes SDL as a dependency. At present this leaves most of APG fairly unusable. Also currently sets APG_NO_GL." OFF)
option(APG_NO_GL "Do not build any component of APG which uses OpenGL/GLEW, removing those as dependencies. Affects graphics rendering capabilities. Also currently sets APG_NO_SDL." OFF)
option(APG_NO_SDL "Do not build any component which uses SDL; removes SDL as a dependency. At present this leaves most of APG fairly unusable. Also currently sets APG_NO_GL." OFF)
option(APG_NO_GL "Do not build any component which uses OpenGL/GLEW, removing those as dependencies. Affects graphics rendering capabilities. Also currently sets APG_NO_SDL." OFF)
option(APG_NO_NATIVE "Do not build any component which uses functionality which differs across platforms (think #ifdef _WIN32). Useful if you're trying to build on some arcane platform." OFF)

option(EXCLUDE_GL_TEST "Should we exclude compiling the OpenGL TMX rendering test?" OFF)
option(EXCLUDE_SDL_TEST "Should we exclude compiling the SDL TMX rendering test?" OFF)
Expand All @@ -37,6 +38,10 @@ if( APG_NO_GL OR APG_NO_SDL )
set(EXCLUDE_AUDIO_TEST ON)
endif ()

if( APG_NO_NATIVE )
message("Warning: APG_NO_NATIVE is not thoroughly tested so there could be bugs!")
endif()

message ("Finding dependencies for APG.")

if ( NOT APG_NO_SDL )
Expand Down Expand Up @@ -78,7 +83,7 @@ if( DEFINED WIN32 )
else ()
# MinGW
set (OS_FLAGS "-mwindows")
set (OS_LIBS Version Imm32 winmm vorbisfile ogg vorbis mingw32)
set (OS_LIBS Version Imm32 winmm vorbisfile ogg vorbis winsock32 mingw32)
endif ()
else ()
set (OS_FLAGS "-fPIC")
Expand Down Expand Up @@ -107,6 +112,10 @@ if ( APG_NO_GL )
add_definitions(-DAPG_NO_GL)
endif()

if ( APG_NO_NATIVE)
add_definitions(-DAPG_NO_NATIVE)
endif()

set(CMAKE_CXX_FLAGS_BASE ${COMPILER_FLAGS})

include_directories("include"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ stop having to worry about cleanup.
- A SpriteBatch class similar to the one in [LibGDX](https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/SpriteBatch.java) which aims to minimise draw calls for 2D sprites.
- Two renderers for TMX maps; one using pure SDL2 `SDL_Renderer` functions, one using pure modern OpenGL (core context).
- An SDL2 input manager for the keyboard supporting "isPressed" and "isJustPressed" as required.
- An SDL2 audio manager for playing a channel of music and multiple channels of sound effects.
- An SDL2_mixer audio manager for playing a channel of music and multiple channels of sound effects.
- SDL2_net/Native Socket wrappers for networking including a modified version of [ByteBufferCpp](https://github.com/SgtCoDFish/ByteBufferCpp) for handling data.
- The ability to compile without SDL/GL at all, allowing you to use the rest of the library freely and reduce dependencies/library size if you don't need some components (ideal for, for example, servers which share some data structures with the client).

How To Use
----------
Expand Down
1 change: 1 addition & 0 deletions include/APG/APGNet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
#include "net/ByteBuffer.hpp"
#include "net/NetUtil.hpp"
#include "net/SDLSocket.hpp"
#include "net/NativeSocket.hpp"

#endif /* INCLUDE_APG_APGNET_HPP_ */
6 changes: 6 additions & 0 deletions include/APG/graphics/VAO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#ifndef INCLUDE_APG_GRAPHICS_VAOINCLUDE_HPP_
#define INCLUDE_APG_GRAPHICS_VAOINCLUDE_HPP_

#ifndef APG_NO_SDL
#ifndef APG_NO_GL

#include <cstdint>

namespace APG {
Expand All @@ -49,4 +52,7 @@ class VAO final {

}

#endif
#endif

#endif /* VAOINCLUDE_HPP_ */
150 changes: 150 additions & 0 deletions include/APG/net/NativeSocket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2015 See AUTHORS file.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef INCLUDE_APG_NET_NATIVESOCKET_HPP_
#define INCLUDE_APG_NET_NATIVESOCKET_HPP_

#ifndef APG_NO_NATIVE

#include <string>
#include <memory>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif

#include "Socket.hpp"
#include "ByteBuffer.hpp"

namespace APG {

class NativeSocketUtil {
public:
using addrinfo_ptr = std::unique_ptr<addrinfo, void(*)(addrinfo*)>;

/**
* Traverses a linked list returned by getaddrinfo and calls socket() on each
* trying to get a socket descriptor. If it succeeds, returns the descriptor and places
* the valid addrinfo into targetStruct.
*
* If bind is true, ::bind() will be called on a valid descriptor. If false, ::connect() is called.
* @return a valid socket descriptor if a valid addrinfo was found, or -1 on failure.
*/
static int findValidSocket(const addrinfo_ptr &ptr, addrinfo ** targetStruct, bool bind = false);

static int closeSocket(int socketFD);

static addrinfo_ptr make_addrinfo_ptr(addrinfo *ainfo);
};

class NativeSocket : public Socket {
public:
/**
* Must be called at start of application if native sockets are to be used.
*
* Mainly for Windows; not calling probably won't be a problem on other platforms.
*/
static void nativeSocketInit();

/**
* Must be called at end of application if native sockets are to be used.
*
* Mainly for Windows.
*/
static void nativeSocketCleanup();

/**
* Create a socket from a raw file descriptor.
*/
static std::unique_ptr<Socket> fromRawFileDescriptor(int fd, sockaddr_storage theirAddr);

explicit NativeSocket(const char * remoteHost, uint16_t port, bool autoConnect = false, uint32_t bufferSize =
BB_DEFAULT_SIZE);

/**
* Not reccommended for use; mainly for use by unique_ptr
*/
explicit NativeSocket(int fd, const std::string &str, uint16_t port, uint32_t bufferSize = BB_DEFAULT_SIZE);
virtual ~NativeSocket();

virtual int send() override final;

virtual int recv(uint32_t length = 1024u) override final;

virtual bool waitForActivity(uint32_t millisecondsToWait = 0u) override final;

virtual void connect() override final;
virtual void disconnect() override final;

private:
void addToSet();
const std::string portString;

int internalSocket = -1;
bool connected = false;

fd_set socketSet;
};

class NativeAcceptorSocket : public AcceptorSocket {
public:
explicit NativeAcceptorSocket(uint16_t port_, bool autoListen = false, uint32_t bufferSize = BB_DEFAULT_SIZE);
virtual ~NativeAcceptorSocket();

virtual std::unique_ptr<Socket> acceptSocket(float maxWaitInSeconds = -1.0f) override final;
virtual std::unique_ptr<Socket> acceptSocketOnce() override final;

virtual void disconnect() override final;

protected:
virtual void listen() override final;

private:
static constexpr const int CONNECTION_BACKLOG_SIZE = 10;

bool listening = false;
const std::string portString;

int internalListener = -1;
};

}

#endif

#endif /* INCLUDE_APG_NET_NATIVESOCKET_HPP_ */
7 changes: 4 additions & 3 deletions include/APG/net/SDLSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <cstdint>

#include <string>
#include <memory>

#include <SDL2/SDL_net.h>
Expand All @@ -47,9 +48,9 @@ class SDLSocket : public Socket {
public:
static std::unique_ptr<SDLSocket> fromRawSDLSocket(TCPsocket socket);

explicit SDLSocket(const char * hostName, uint16_t port, bool autoConnect = false);
explicit SDLSocket(const std::string &hostName, uint16_t port, bool autoConnect = false, uint32_t bufferSize = BB_DEFAULT_SIZE);
/**
* Not recommended for general use
* Not recommended for general use, mainly used by unique_ptr
*/
explicit SDLSocket(TCPsocket readSocket_, IPaddress *ip_, const char *remoteHost_, uint16_t port_);
virtual ~SDLSocket();
Expand Down Expand Up @@ -79,7 +80,7 @@ class SDLSocket : public Socket {

class SDLAcceptorSocket final : public AcceptorSocket {
public:
explicit SDLAcceptorSocket(uint16_t port, bool autoListen = false);
explicit SDLAcceptorSocket(uint16_t port, bool autoListen = false, uint32_t bufferSize = BB_DEFAULT_SIZE);
virtual ~SDLAcceptorSocket();

virtual std::unique_ptr<Socket> acceptSocket(float maxWaitInSeconds = -1.0f) override final;
Expand Down
18 changes: 9 additions & 9 deletions include/APG/net/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@

#include <memory>
#include <array>
#include <string>

#include "ByteBuffer.hpp"

namespace APG {

// TODO: Handle connections better ("connected" variable"?)

/**
* This is a class for implementing shared functionality between Socket and AcceptorSocket;
* you probably want one of those.
Expand Down Expand Up @@ -66,21 +64,23 @@ class SocketCommon : public ByteBuffer {

class Socket : public SocketCommon {
public:
explicit Socket(const char * remoteHost, uint16_t port, uint32_t bufferSize = BB_DEFAULT_SIZE);
explicit Socket(const std::string &remoteHost, uint16_t port, uint32_t bufferSize = BB_DEFAULT_SIZE);
virtual ~Socket() = default;

const uint16_t port;
const char * const remoteHost;
const std::string remoteHost;

/**
* Send data currently in the buffer.
* @return the number of bytes sent
* Send all data currently in the buffer.
* @return the number of bytes sent which should match the size() of the buffer; less on error.
*/
virtual int send() = 0;

// TODO: sendOnce() ?

/**
* Read data into readBuffer.
* @return the number of bytes read.
* Read up to length bytes of data into the buffer.
* @return the number of bytes actually read; 0 if an error occurred.
*/
virtual int recv(uint32_t length = 1024u) = 0;

Expand Down
5 changes: 5 additions & 0 deletions src/graphics/VAO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef APG_NO_SDL
#ifndef APG_NO_GL

#include <cstdint>

#include <GL/glew.h>
Expand All @@ -43,3 +46,5 @@ void APG::VAO::bind() const {
glBindVertexArray(vaoID);
}

#endif
#endif
Loading

0 comments on commit d0a4b0f

Please sign in to comment.