summaryrefslogtreecommitdiffstats
path: root/network/akgnet_fdescr.cc
blob: bc5df7d2e894c70472aa8e0663f886221d7e91af (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
/*
* 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_fdescr.cc
 *
 * MODULE: akg network
 * CLASS: FileDescriptor
 *
 * COMMENTS:
 *
 */

#include <akgnet_fdescr.hh>

akg::FileDescriptor::FileDescriptor() throw()
  { 
    fileDescriptor = -1;
    savedErrno = 0;
   }   
   
akg::FileDescriptor::~FileDescriptor() throw()
  {
    close();
   }
   
int akg::FileDescriptor::operator()() throw()
  {
    return fileDescriptor;
   }

bool akg::FileDescriptor::isOpen() throw()
  {
    return fileDescriptor == -1 ? false:true;
   }

void akg::FileDescriptor::close() throw()
  {
    if(isOpen()) 
      { ::close(fileDescriptor);
        saveErrno();
       }	
    fileDescriptor = -1;
   }

int akg::FileDescriptor::write(const void *buffer, int count) throw()
  { 
    savedErrno = 0;
    DBTALK("FileDescriptor write: "<<buffer<<" count="<<count);
    int nbytes = ::write(fileDescriptor,buffer,count);
    if(nbytes < 0) saveErrno(); 
    return nbytes;
   }
   
int akg::FileDescriptor::read (void *buffer, int count) throw()
  {
    savedErrno = 0;
    DBTALK("FileDescriptor read: "<<buffer<<" count="<<count);
    int nbytes = ::read(fileDescriptor,buffer,count);
    if(nbytes < 0) saveErrno(); 
    return nbytes;  
   }

bool akg::FileDescriptor::setNonBlocking(bool  nonBlocking) throw()
  {
    if(isOpen())
      { int val  = fcntl(fileDescriptor,F_GETFL,0);
        
	if( nonBlocking) val |= O_NONBLOCK;
	else             val &=~O_NONBLOCK;
	
        fcntl(fileDescriptor,F_SETFL,val);
        return true; 
       }
    
    return false;   
   }
bool akg::FileDescriptor::isNonBlocking() throw()
  {
    if(isOpen())
      { int val = fcntl(fileDescriptor,F_GETFL,0);
        return (val & O_NONBLOCK) ? true:false;
       }
    return false;   
   }

int akg::FileDescriptor::getErrno() throw()
  { return savedErrno;
   }
void akg::FileDescriptor::saveErrno() throw()
  { savedErrno=errno;
   }