summaryrefslogtreecommitdiffstats
path: root/network/akgnet_commbuffer.cc
blob: 3c5c561833926eb4983d97714f82af320c6623bd (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* This file is part of rasdaman community.
*
* Rasdaman community 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 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community 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 rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: akgnet_commbuffer.cc
 *
 * MODULE: akg network
 * CLASS:  CommBuffer
 *
 * COMMENTS:
 *
 */

#include <akgnet_commbuffer.hh>
#include <string>
#include <assert.h>


akg::CommBuffer::CommBuffer() throw()
  {
    data     = NULL;
    buffSize = 0;
    fillSize = 0;
    sendSize = 0;
    allocated= false;
   }
   
akg::CommBuffer::CommBuffer(int size) throw()
  {
    assert(size > 0);
    allocate(size);
   }
   
akg::CommBuffer::CommBuffer(void *externalBuffer,int totalSize, int dataSize) throw()
  {
    data = NULL;
    takeOver(externalBuffer,totalSize,dataSize);
   }
      
akg::CommBuffer::~CommBuffer() throw()
  {
    freeBuffer();
   }

bool  akg::CommBuffer::allocate(int size) throw()
  {
    assert(size > 0);
    
    freeBuffer();
    data = new char[size];
    // new throws?
    buffSize=size;
    allocated=true;
    return true;
   }

void akg::CommBuffer::freeBuffer() throw()
  {
    if(allocated == true && data != NULL) delete[] data;
    data     = NULL;
    buffSize = 0;
    fillSize = 0;
    sendSize = 0;
    allocated= false;
   }
   
void akg::CommBuffer::takeOver(void *externalBuffer,int totalSize, int dataSize) throw()
  {
    assert(externalBuffer != 0);
    assert(totalSize > 0);
    assert(dataSize >= 0);
    assert(totalSize >= dataSize);
    
    freeBuffer();
    data     = (char*)externalBuffer;
    buffSize = totalSize;
    fillSize = dataSize;
   }
   
bool akg::CommBuffer::resize(int newSize) throw()
  {
    assert(data != 0);
    
    // we can't make the buffer smaller by truncating inside data!
    if(newSize < fillSize) return false;
    
    char *newData = new char[newSize];
    memcpy(newData, data, fillSize);
    if(allocated == true ) delete[] data;
    
    data      = newData;
    buffSize  = newSize;
    allocated = true;
    return true; 
   }
            
void* akg::CommBuffer::getData()          throw(){ return data;}
int   akg::CommBuffer::getDataSize()      throw(){ return fillSize;}
int   akg::CommBuffer::getBufferSize()    throw(){ return buffSize;}
int   akg::CommBuffer::getSendedSize()    throw(){ return sendSize;}
int   akg::CommBuffer::getNotFilledSize() throw(){ return buffSize-fillSize;}
int   akg::CommBuffer::getNotSendedSize() throw(){ return fillSize-sendSize;}
bool  akg::CommBuffer::isAllocated()      throw(){ return allocated;}
      
int akg::CommBuffer::read(FileDescriptor &socket) throw()
  {
    int rasp = socket.read(data+fillSize,buffSize-fillSize);
    
    if(rasp>=0) fillSize += rasp;
    
    return rasp;
   }
   
int akg::CommBuffer::read(const void *externalBuffer,int size) throw()
  {
    assert(externalBuffer != 0);
    assert(size >= 0);
    
    int cpSize = size<(buffSize-fillSize) ? size:(buffSize-fillSize);
    
    memcpy(data+fillSize,externalBuffer,cpSize);
    fillSize += cpSize;
    
    return cpSize;
   }

int akg::CommBuffer::reserve(int size) throw()
  {
    assert(size >= 0);
    
    int cpSize = size<(buffSize-fillSize) ? size:(buffSize-fillSize);
    
    fillSize += cpSize;
    
    return cpSize;
   }
      
int akg::CommBuffer::write(FileDescriptor &socket) throw()
  {
    DBTALK("CommBuffer write fillSize="<<fillSize<<" sendSize="<<sendSize);
    int rasp = socket.write(data+sendSize,fillSize-sendSize);
    
    if(rasp>=0) sendSize+=rasp;
    
    return rasp;
   }
   
int akg::CommBuffer::write(void *externalBuffer,int size) throw()
  {
    assert(externalBuffer != 0);
    assert(size >= 0);
    
    int cpSize = size<(fillSize-sendSize) ? size:(fillSize-sendSize);
    
    memcpy(externalBuffer,data+sendSize,cpSize);
    sendSize+=cpSize;
    
    return cpSize;
   }
      
void akg::CommBuffer::clearToRead() throw()
  {
    DBTALK("CommBuffer clearToRead");
    fillSize=0;sendSize=0;
   }
void akg::CommBuffer::clearToWrite() throw()
  {
    DBTALK("CommBuffer clearToWrite");
    sendSize=0;
   }