summaryrefslogtreecommitdiff
path: root/i/simulotron/src/socket/socket.cc
blob: 0e7f819c3c7b05c2c79c40622cb23e7b1300ad37 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <sys/socket.h>
#include <cerrno>
#include <iostream>
#include "socket/socket.hh"
#include "utils/errno_exception.hh"
#include "socket/address.hh"

Socket::Socket(const Address & address)
{
    // Cr�e le file descriptor du socket
    socket_ = socket (PF_INET, SOCK_STREAM, 0);
    if(socket_ < 0)
	throw errno_exception ("La chaussette n'a pas pu �tre tricot�", errno);

    // On bind le socket
    if(::bind (socket_, address.getSockaddr(), sizeof(*address.getSockaddr())) < 0)
	throw errno_exception("Impossible d'assigner la chausette", errno);
}

Socket::Socket(int socket)
{
    socket_ = socket;
}

Socket::Socket(Socket & socket)
{
    socket_ = socket.socket_;
    socket.socket_ = -1;
}

Socket::~Socket(void)
{
    if(socket_ >= 0)
	::close(socket_);
}

Socket & Socket::operator= (Socket & socket)
{
    socket_ = socket.socket_;
    socket.socket_ = -1;
}

void Socket::connect(const Address & address)
{
    if (::connect (socket_, address.getSockaddr(), sizeof(sockaddr_in)) < 0)
	throw errno_exception("Impossible de mettre la chaussette", errno);
}

void Socket::listen(int maxQueue)
{
    if(::listen(socket_, maxQueue) < 0 )
	throw errno_exception("Impossible d'�couter la mer dans la chaussette", errno);
}

Socket Socket::accept(Address & address)
{
    sockaddr addr;
    socklen_t length = sizeof(sockaddr_in);

    int socket = ::accept(socket_, &addr, &length);
    if (socket < 0)
	throw errno_exception("Chaussette bloqu� � la douane", errno);
    address = Address(&addr, length);

    return Socket(socket);
}

void Socket::close(void)
{
    ::close(socket_);
}

std::string Socket::read(void)
{
    std::string str;
    int charReaded;
    char buffer[BUFFER_SIZE];
    do
    {
	charReaded = ::read(socket_, buffer, BUFFER_SIZE - 1);
	if(charReaded < 0)
	    throw errno_exception("Socket: probl�me de lecture", errno);
	buffer[charReaded] = 0;
	str += buffer;
    }while(charReaded == BUFFER_SIZE - 1);
    return str;
}

void Socket::write(const std::string & str)
{
    char buffer[BUFFER_SIZE];
    unsigned int pos = 0;
    int retval = 0;

    while(str.size() != pos)
    {
	int size = str.size() - pos < BUFFER_SIZE - 1 ? str.size() - pos:BUFFER_SIZE - 1;
	str.copy(buffer, size, pos);
	pos += size;
	buffer[size] = 0;
	retval = ::write(socket_, buffer, size);
	if(retval < 0)
	    throw errno_exception("Socket: erreur d'�criture (write()) ", errno);
    }
}

char Socket::getChar(void)
{
    char getted;
    ::read(socket_, &getted, 1);
    return getted;
}

void Socket::putChar(char c)
{
    ::write(socket_, &c, 1);
}

int Socket::getFD(void)
{
    return socket_;
}