ESA JPIP server  0.1
socket_stream.h
Go to the documentation of this file.
1 #ifndef _NET_SOCKET_STREAM_H_
2 #define _NET_SOCKET_STREAM_H_
3 
4 
5 #include <iostream>
6 #include <stdio.h>
7 #include <string.h>
8 #include "socket.h"
9 
10 
11 namespace net
12 {
13 
23  class SocketBuffer: public std::streambuf
24  {
25  protected:
26  int sum;
27  int in_len;
28  int out_len;
29  char *in_buf;
30  char *out_buf;
32 
33  public:
34  enum {
37  };
38 
39  SocketBuffer(int sid,
42  {
43  sum = 0;
44 
45  this->in_len = in_len;
46  this->out_len = out_len;
47 
48  in_buf = new char[in_len];
49  out_buf = new char[out_len];
50 
51  setp(out_buf, out_buf + out_len);
52  }
53 
54  virtual int sync()
55  {
56  if(pptr() != pbase()) {
57  int n, len = 0;
58 
59  while(pbase() + len < pptr()) {
60  n = socket.Send(pbase() + len, pptr() - pbase() - len);
61  if(n >= 0) len += n;
62  else return -1;
63  }
64 
65  setp(out_buf, out_buf + out_len);
66  }
67 
68  return 0;
69  }
70 
71  virtual int_type underflow()
72  {
73  sum += (egptr() - eback());
74  int len = socket.Receive(in_buf, in_len);
75 
76  if(len <= 0) return traits_type::eof();
77  else {
78  setg(in_buf, in_buf, in_buf + len);
79  return traits_type::to_int_type(*gptr());
80  }
81  }
82 
83  virtual int_type overflow(int_type c = EOF)
84  {
85  if(traits_type::eq_int_type(traits_type::eof(), c)) return sync();
86  else {
87  if(pptr() == epptr()) sync();
88 
89  traits_type::assign(*pptr(), traits_type::to_char_type(c));
90  pbump(1);
91 
92  return c;
93  }
94  }
95 
96  int GetReadBytes() const
97  {
98  return (sum + (egptr() - eback()));
99  }
100 
102  {
103  return &socket;
104  }
105 
106  virtual ~SocketBuffer()
107  {
108  delete [] in_buf;
109  delete [] out_buf;
110  }
111  };
112 
113 
121  class SocketStream :public SocketBuffer, public std::iostream
122  {
123  public:
124  SocketStream(int sid,
127  :SocketBuffer(sid, in_len, out_len), std::iostream(this)
128  {
129  }
130 
132  {
133  return &(socket);
134  }
135 
136  virtual ~SocketStream()
137  {
138  }
139  };
140 }
141 
142 
143 #endif /* _NET_SOCKET_STREAM_H_ */
Class derived from the STL std::streambuf to allow streaming with sockets.
Definition: socket_stream.h:23
Socket * operator->()
Definition: socket_stream.h:131
int in_len
Definition: socket_stream.h:27
int sum
Definition: socket_stream.h:26
char * in_buf
Definition: socket_stream.h:29
int Receive(void *buf, int len, bool prevent_block=false)
Receives a number of bytes.
Definition: socket.cc:57
int out_len
Definition: socket_stream.h:28
virtual int_type overflow(int_type c=EOF)
Definition: socket_stream.h:83
virtual int sync()
Definition: socket_stream.h:54
int GetReadBytes() const
Definition: socket_stream.h:96
Socket socket
Definition: socket_stream.h:31
SocketStream(int sid, int in_len=INPUT_BUFFER_LENGTH, int out_len=OUTPUT_BUFFER_LENGTH)
Definition: socket_stream.h:124
Definition: socket_stream.h:36
int Send(void *buf, int len, bool prevent_block=false)
Sends a number of bytes.
Definition: socket.cc:79
Class derived from std::iostream and SocketBuffer that represents a socket stream.
Definition: socket_stream.h:121
virtual int_type underflow()
Definition: socket_stream.h:71
virtual ~SocketStream()
Definition: socket_stream.h:136
char * out_buf
Definition: socket_stream.h:30
virtual ~SocketBuffer()
Definition: socket_stream.h:106
SocketBuffer(int sid, int in_len=INPUT_BUFFER_LENGTH, int out_len=OUTPUT_BUFFER_LENGTH)
Definition: socket_stream.h:39
Definition: socket_stream.h:35
Socket * GetSocket()
Definition: socket_stream.h:101
This class has been designed to work with UNIX sockets in an easy and object oriented way...
Definition: socket.h:28