summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/socket/socket_text.cc
blob: 9c94509d00ad85237518e9f1de0fc674784ea430 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// socket_text.cc
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Dufour J�r�my
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Contact :
//        Web: %WEB%
//      Email: <dufourj@efrei.fr>
// }}}

#include "socket_text.hh"

#include "server_socket.hh"
#include "address.hh"
#include "data/data_buffer.hh"

#include <sstream>
#include <stdexcept>

/// Constructeur du serveur
SocketText::SocketText (ServerSocket &ss)
    : sendBufSize_ (0), recvTaille_ (0)
{
    socket_ = ss.accept ();
}

/// Constructeur du serveur avec addresse
SocketText::SocketText (ServerSocket &ss, Address &a)
    : sendBufSize_ (0), recvTaille_ (0)
{
    socket_ = ss.accept (a);
}

/// Constructeur du client
SocketText::SocketText (const Address &a)
    : sendBufSize_ (0), recvTaille_ (0)
{
    // Cr�ation du socket
    socket_ = socket (AF_INET, SOCK_STREAM, 0);
    if (socket_ == -1)
	throw std::runtime_error ("Erreur de cr�ation de socket");
    // Connexion au serveur
    if (connect (socket_, a.getSockaddr (), sizeof (sockaddr_in)) == -1)
	throw std::runtime_error ("Erreur de connexion au serveur");
}

/// Envoie d'un buffer de donn�es.
SocketText &
SocketText::operator<< (const DataBuffer &d)
{
    DataBuffer db (d);
    unsigned size = db.size ();
    std::vector <char> v;
    v.resize (size);
    db.read ((uint8_t*) &v[0], size);
    send (&v[0], size);
    return *this;
}

/// Reception d'un buffer de donn�es.
SocketText &
SocketText::operator>> (DataBuffer &d)
{
    std::vector <char> v;
    recv (v);
    d.write ((uint8_t*) &v[0], v.size ());
    return *this;
}

/// Envoie d'un tableau de donn�es (data) de taille size.
int
SocketText::send (const char *data, const int size)
{
    int ss;
    int status;
    int dataSend = 0;
    // Encodage, ss = taille (message + header)
    ss = size + encodeMsg (data, size);
    do
      {
	status = ::send (socket_, &sendBuf_[0], ss - dataSend, MSG_NOSIGNAL);
	dataSend += status;
      } while (status >= 0 && size >= dataSend);
    return status < 0 ? -1 : dataSend;
}

/// Encodage du message (rajout du header)
int
SocketText::encodeMsg (const char *data, const int size)
{
    // Cr�ation du header
    std::ostringstream o;
    o << "H";
    if (!(o << size ))
	throw std::runtime_error ("Erreur de convertion int -> string");
    o << "H";
    std::string s = o.str ();
    int ss = s.size ();
    // Re-Allocation du buffer d'envoye
    sendBuf_.resize (ss + size);
    // Copie du header
    strncpy (&sendBuf_[0], s.c_str (), ss);
    // Copie du message
    strncpy (&sendBuf_[ss], data, size);
    return ss;
}

/// Reception de donn�es dans un vecteur.
void SocketText::recv (std::vector<char> &v)
{
    // FIXME C'est KK
    int status;
    static unsigned int pos = 0;
    static unsigned int posHeader = 0;
    // 0 : pas re�u, 1 : partiellement re�u, 2 : re�u
    static int headerStatus = 0;

    // On n'a pas re�u la taille encore
    if (headerStatus != 2)
      {
	// R�cup�ration des donn�es une par une.
	do
	  {
	    status = ::recv (socket_, &headBuf_[posHeader], 1, MSG_NOSIGNAL);
	    if (headBuf_[posHeader++] == 'H')
		headerStatus ++;
	  } while (status > 0 && headerStatus != 2);
      }
    // R�cup�ration de la taille du message du header
    if (headerStatus == 2 && recvTaille_ == 0)
      {
	recvTaille_ = atoi (&headBuf_[1]);
	pos = 0;
	recvBuf_.resize (recvTaille_);
      }
    if (recvTaille_)
      {
	// R�cup�ration du message lui-m�me
	do
	  {
	    status = ::recv (socket_, &recvBuf_[pos], recvTaille_,
			     MSG_NOSIGNAL);
	    if (status > 0)
		pos += status;
	  } while (status > 0 && pos < recvTaille_);
	if (pos == recvTaille_)
	  {
	    v = recvBuf_;
	    headerStatus = posHeader = recvTaille_ = 0;
	  }
      }
}