Skip to content

Commit ab4dc44

Browse files
committed
First coomit
1 parent 7d5d9e6 commit ab4dc44

18 files changed

+1034
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Alexandre Moghrabi <[email protected]>

Makefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
##
2+
## Makefile for Makefile in
3+
##
4+
## Made by Moghrabi Alexandre
5+
6+
##
7+
## Started on Fri Jun 6 11:16:50 2014 Moghrabi Alexandre
8+
## Last update Fri Oct 10 15:36:29 2014 Moghrabi Alexandre
9+
##
10+
11+
NAME= libnetwork.a
12+
13+
SRCDIR= ./src/
14+
15+
SRC= $(SRCDIR)IpAddress.cpp \
16+
$(SRCDIR)UnixSocket.cpp \
17+
$(SRCDIR)Socket.cpp \
18+
$(SRCDIR)TcpSocket.cpp \
19+
$(SRCDIR)WinSocket.cpp \
20+
$(SRCDIR)SocketSelector.cpp
21+
22+
OBJS= $(SRC:.cpp=.o)
23+
24+
RM= rm -rf
25+
26+
CXX= g++
27+
28+
AR= ar rs
29+
30+
CXXFLAGS= -Wall -Werror -Wextra -I include
31+
LDFLAGS=
32+
33+
all: $(NAME)
34+
35+
$(NAME): $(OBJS)
36+
$(AR) $(NAME) $(LDFLAGS) $(OBJS)
37+
38+
debug: CXXFLAGS += -g3
39+
debug: $(NAME)
40+
41+
clean:
42+
$(RM) $(OBJS)
43+
44+
fclean: clean
45+
$(RM) $(NAME)
46+
47+
re: fclean all
48+
49+
.PHONY: all debug clean fclean re

include/IpAddress.hh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// IpAddress.hh for in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Mon Jun 9 16:06:02 2014 Moghrabi Alexandre
8+
// Last update Wed Oct 8 07:56:15 2014 Moghrabi Alexandre
9+
//
10+
11+
#include <stdint.h>
12+
13+
#ifndef IPADDRESS_HH
14+
# define IPADDRESS_HH
15+
16+
# include <string>
17+
18+
namespace Mog
19+
{
20+
class IpAddress
21+
{
22+
public:
23+
IpAddress(const std::string& address);
24+
virtual ~IpAddress();
25+
26+
private:
27+
void resolve();
28+
29+
public:
30+
int getInt() const;
31+
32+
private:
33+
std::string m_sAddress;
34+
uint32_t m_address;
35+
};
36+
} // namespace Mog
37+
38+
#endif // !IPADDRESS_HH

include/OS.hh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// OS.hh for OS in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Thu Jun 5 18:44:24 2014 Moghrabi Alexandre
8+
// Last update Thu Jun 5 19:12:39 2014 Moghrabi Alexandre
9+
//
10+
11+
#ifndef OS_HH
12+
# define OS_HH
13+
14+
// Find the operating system
15+
#if defined(_WIN32) || defined(__WIN32__)
16+
17+
// Windows
18+
#define OS_WINDOWS
19+
20+
#elif defined(linux) || defined(__linux)
21+
22+
// Linux
23+
#define OS_LINUX
24+
25+
#elif defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)
26+
27+
// Mac OS
28+
#define OS_MAC
29+
30+
#elif defined(__FreeBSD) || defined(__FreeBSD_kernel__)
31+
32+
// FreeBSD
33+
#define OS_FREEBSD
34+
35+
#else
36+
37+
#error This os is not supported by this project.
38+
39+
#endif
40+
41+
#endif // !OS_HH

include/OsSocket.hh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// OsSocket.hh for OsSocket in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Thu Jun 5 19:34:20 2014 Moghrabi Alexandre
8+
// Last update Wed Oct 8 08:05:50 2014 Moghrabi Alexandre
9+
//
10+
11+
#if defined(OS_WINDOWS)
12+
#include "WinSocket.hh"
13+
#else
14+
#include "UnixSocket.hh"
15+
#endif

include/Socket.hh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Socket.hh for Network in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Thu Jun 5 19:07:37 2014 Moghrabi Alexandre
8+
// Last update Fri Oct 10 15:53:48 2014 Moghrabi Alexandre
9+
//
10+
11+
#ifndef SOCKET_HH
12+
#define SOCKET_HH
13+
14+
#include "SocketFD.hh"
15+
16+
namespace Mog
17+
{
18+
class Socket
19+
{
20+
public:
21+
enum Status
22+
{
23+
Ok, // Socket ready to read / send datas
24+
Nok, // Socket not ready to read / send datas
25+
Disconnected, // Socket disconnected
26+
Waiting, // Waiting for datas
27+
Error // Unexpected error
28+
};
29+
30+
public:
31+
virtual ~Socket();
32+
SocketFD getSocketFD() const;
33+
34+
protected:
35+
enum Type
36+
{
37+
Tcp,
38+
Udp
39+
};
40+
Socket(Type type);
41+
void create();
42+
void create(SocketFD fd);
43+
void close();
44+
45+
private:
46+
Type m_type;
47+
SocketFD m_socket;
48+
};
49+
} // namespace Mog
50+
51+
#endif /* !SOCKET_HH */

include/SocketFD.hh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// SocketFD.hh for SocketFD in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Thu Jun 5 19:12:47 2014 Moghrabi Alexandre
8+
// Last update Thu Jun 5 19:35:34 2014 Moghrabi Alexandre
9+
//
10+
11+
#include "OS.hh"
12+
13+
#ifndef SOCKETFD_HH
14+
# define SOCKETFD_HH
15+
16+
#if defined(OS_WINDOWS)
17+
typedef UINT_PTR SocketFD;
18+
#else
19+
typedef int SocketFD;
20+
#endif
21+
22+
#endif // !SOCKETFD_HH

include/SocketSelector.hh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// SocketSelector.hh for Mog in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Wed Jun 11 15:43:12 2014 Moghrabi Alexandre
8+
// Last update Fri Oct 10 15:52:21 2014 Moghrabi Alexandre
9+
//
10+
11+
#ifndef SOCKETSELECTOR_HH
12+
# define SOCKETSELECTOR_HH
13+
14+
#include <sys/select.h>
15+
#include <sys/time.h>
16+
#include <sys/types.h>
17+
#include <unistd.h>
18+
#include <list>
19+
#include "SocketSelectorListener.hh"
20+
#include "TcpSocket.hh"
21+
22+
typedef timeval Time;
23+
24+
namespace Mog
25+
{
26+
class SocketSelector
27+
{
28+
public:
29+
enum TriggerType
30+
{
31+
None,
32+
Read,
33+
Write
34+
};
35+
enum State
36+
{
37+
Waiting,
38+
Error
39+
};
40+
SocketSelector(TcpSocket& serverSocket, SocketSelectorListener& socketSelectorListener);
41+
~SocketSelector();
42+
43+
public:
44+
void waitForTrigger();
45+
46+
public:
47+
void setTimeout(Time* timeout) {m_timeout = timeout;}
48+
Time* getTimeout() const {return (m_timeout);}
49+
State getState() const {return (m_state);}
50+
const TcpSocket& getServerSocket() const {return (m_serverSocket);}
51+
52+
private:
53+
void setFds();
54+
void acceptClient();
55+
void handleClients();
56+
57+
private:
58+
// Timeout of the select
59+
Time* m_timeout;
60+
// List of connected sockets
61+
std::list<TcpSocket> m_sockets;
62+
// The state of the selector
63+
State m_state;
64+
fd_set rdfs;
65+
fd_set wdfs;
66+
int m_maxFds;
67+
TcpSocket& m_serverSocket;
68+
SocketSelectorListener& m_socketSelectorListener;
69+
};
70+
} // namespace Mog
71+
72+
#endif // !SOCKETSELECTOR_HH

include/SocketSelectorListener.hh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// SocketSelectorListener.hh for SocketSelectorListener in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Fri Oct 10 15:31:21 2014 Moghrabi Alexandre
8+
// Last update Fri Oct 10 15:53:01 2014 Moghrabi Alexandre
9+
//
10+
11+
#ifndef SOCKETSELECTORLISTENER_HH_
12+
# define SOCKETSELECTORLISTENER_HH_
13+
14+
# include "TcpSocket.hh"
15+
16+
namespace Mog
17+
{
18+
class SocketSelectorListener
19+
{
20+
public:
21+
virtual ~SocketSelectorListener();
22+
23+
public:
24+
virtual void clientAccepted(TcpSocket& client);
25+
virtual void clientDatasReceived(TcpSocket& client);
26+
virtual void clientDisconnected(TcpSocket& client);
27+
};
28+
}
29+
30+
#endif /* !SOCKETSELECTORLISTENER_HH_ */

include/TcpSocket.hh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// TcpSocket.hh for TCPSOCKET in
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Thu Jun 5 19:59:35 2014 Moghrabi Alexandre
8+
// Last update Fri Oct 10 15:55:58 2014 Moghrabi Alexandre
9+
//
10+
11+
#ifndef TCPSOCKET_HH
12+
# define TCPSOCKET_HH
13+
14+
# include <list>
15+
# include <vector>
16+
# include <string>
17+
# include "IpAddress.hh"
18+
# include "Socket.hh"
19+
20+
namespace Mog
21+
{
22+
class TcpSocket : public Socket
23+
{
24+
public:
25+
typedef void (*ReadFunction)(TcpSocket* client);
26+
typedef std::vector<char> Data;
27+
typedef std::list<Data> DataList;
28+
29+
public:
30+
struct ReadedDatas
31+
{
32+
ReadedDatas();
33+
std::size_t readed;
34+
std::size_t totalSize;
35+
Data datas;
36+
};
37+
TcpSocket();
38+
TcpSocket(SocketFD fd);
39+
Socket::Status connect(const IpAddress& address, unsigned short port);
40+
void disconnect();
41+
Socket::Status send(const char* data, std::size_t size);
42+
Socket::Status receive(char* data, std::size_t size, std::size_t& received, int _flags);
43+
Socket::Status asyncSend(const char* data, std::size_t size);
44+
Socket::Status sendPendingDatas();
45+
bool havingPendingDatas() const;
46+
void setUserData(void* userData);
47+
void* getUserData() const;
48+
Socket::Status readPendingDatas();
49+
void setReadFunc(ReadFunction);
50+
const TcpSocket::ReadedDatas& getDatasReaded() const;
51+
52+
private:
53+
DataList m_pendingDatas;
54+
ReadedDatas m_pendingRDatas;
55+
ReadedDatas m_allDataReaded;
56+
void* m_userData;
57+
ReadFunction m_readFunction;
58+
};
59+
} // namespace Mog
60+
61+
#endif // !TCPSOCKET_HH

0 commit comments

Comments
 (0)