summaryrefslogtreecommitdiff
path: root/i/marvin/src/socket/socket_databuffer.cc
blob: 1b4258c2c1962c52d54ae6e323ed81837b8b7d34 (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
// socket_databuffer.cc
// marvin - programme du robot 2006. {{{
//
// Copyright (C) 2006 Nicolas Haller
//
// 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: http://perso.efrei.fr/~haller/
//      Email: <nicolas@boiteameuh.org>
// }}}
#include "socket_databuffer.hh"

#include <stdexcept>
#include <iostream>

// Constructeur
SocketDataBuffer::SocketDataBuffer(void)
    :SocketClient(), dbSize_(0) // TODO �a sert � quelque chose o� il le fait implicitement?
{
}

// Constructeur accept
SocketDataBuffer::SocketDataBuffer(SocketServer & socketServer)
    :SocketClient(socketServer), dbSize_(0)
{
}

// R�cup�re un DataBuffer
// false en valeur de retour avec bloquant = true est un motif de licenciement
bool
SocketDataBuffer::read(DataBuffer & dbReaded, bool bloquant)
{
    std::string strReceive;
    do
      {
	SocketClient::read(strReceive, bloquant);
	strBuffer_.append(strReceive);
	// On regarde si on a assez de donn�e pour regarder si on a une en-t�te
	if(strBuffer_.size() > sizeof(unsigned))
	  {
	    dbSize_ = 0;
	    dbSize_ = static_cast<unsigned>(strBuffer_[0]) << 24
		    | static_cast<unsigned>(strBuffer_[1]) << 16
		    | static_cast<unsigned>(strBuffer_[2]) << 8
		    | static_cast<unsigned>(strBuffer_[3]);
	    // On regarde si on a suffisemment de donn�es pour constituer le DB
	    if(dbSize_ <= strBuffer_.size() - 4)
	      {
		dbBuffer_.write(reinterpret_cast<const uint8_t *>(strBuffer_.c_str() + 4)
				, dbSize_);
		dbReaded.swap(dbBuffer_);
		dbBuffer_.clear();
		strBuffer_.erase(0, dbSize_ + 4);
		return true;
	      }
	  }
      }while(bloquant);
    return false;
}

// Ecrit un DB dans le socket
void
SocketDataBuffer::write(DataBuffer & db)
{
    // On met une en-t�te de 4 octects d�terminant la taille de la DB
    unsigned dbSize = db.size();
    char entete[4];
    entete[0] = static_cast<char> (dbSize >> 24);
    entete[1] = static_cast<char> (dbSize >> 16);
    entete[2] = static_cast<char> (dbSize >> 8);
    entete[3] = static_cast<char> (dbSize);

    SocketClient::write(std::string(entete,4));
    // On r�cup�re les donn�es de la dataBuffer
    uint8_t * donnees = new uint8_t[dbSize];
    db.read(donnees, dbSize);
    if (db.size() != 0)
	throw std::runtime_error("ERREUR DE CODAGE dans SocketDataBuffer"
				 "::Write");
    // On envoie le tout et on nettoie l'allocation
    SocketClient::write(std::string(reinterpret_cast<const char *>(donnees),
				    dbSize));
    delete [] donnees;
}