summaryrefslogtreecommitdiff
path: root/i/marvin/src/socket/socket_databuffer.cc
blob: 241b8f3e7934cb7b7d3358bff13a4b96eaf2996c (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
// 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.h"

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

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

// R�cup�re un DataBuffer
bool
SocketDataBuffer::read(DataBuffer & dbReaded, bool bloquant)
{
    bool dbRecu;
    if (!isReceiving_)
      {
	// On r�cup�re les donn�es du socket
	dbRecu = read(buffer_, bloquant);
	if(!dbRecu)
	    return false;
	// On extrait la taille de la DB si possible
	if(buffer_.size() < 4)
	  {
	    isReceiving_ = true;
	    return false;
	  }
	else
	  {
	    dbSize_ &= buffer_[0] << 24;
	    dbSize_ &= buffer_[1] << 16;
	    dbSize_ &= buffer_[2] << 8;
	    dbSize_ &= buffer_[3];
	  }
	TODO
	
	
}

// Ecrit un DB dans le socket
void
SocketDataBuffer::write(const DataBuffer & db)
{
    // On met une en-t�te de 4 octects d�terminant la taille de la DB
    unsigned dbSize = db.size();
    write(std::string(static_cast<const char *>(&dbSize)), sizeof(unsigned));
    // 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_exception("ERREUR DE CODAGE dans SocketDataBuffer"
				     "::Write");
    // On envoie le tout et on nettoie l'allocation
    write(std::string(static_cast<const char *>(donnees), dbSize));
    delete [] donnees;
}