MFEM  v3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
socketstream.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.googlecode.com.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_SOCKETSTREAM
13 #define MFEM_SOCKETSTREAM
14 
15 #include "../config/config.hpp"
16 #include <iostream>
17 
18 namespace mfem
19 {
20 
21 class socketbuf : public std::streambuf
22 {
23 private:
24  int socket_descriptor;
25  static const int buflen = 1024;
26  char ibuf[buflen], obuf[buflen];
27 
28 public:
30  {
31  socket_descriptor = -1;
32  }
33 
34  explicit socketbuf(int sd)
35  {
36  socket_descriptor = sd;
37  setp(obuf, obuf + buflen);
38  }
39 
40  socketbuf(const char hostname[], int port)
41  {
42  socket_descriptor = -1;
43  open(hostname, port);
44  }
45 
48  int attach(int sd);
49 
50  int detach() { return attach(-1); }
51 
52  int open(const char hostname[], int port);
53 
54  int close();
55 
56  int getsocketdescriptor() { return socket_descriptor; }
57 
58  bool is_open() { return (socket_descriptor >= 0); }
59 
60  ~socketbuf() { close(); }
61 
62 protected:
63  virtual int sync();
64 
65  virtual int_type underflow();
66 
67  virtual int_type overflow(int_type c = traits_type::eof());
68 
69  virtual std::streamsize xsgetn(char_type *__s, std::streamsize __n);
70 
71  virtual std::streamsize xsputn(const char_type *__s, std::streamsize __n);
72 };
73 
74 
75 class socketstream : public std::iostream
76 {
77 private:
78  socketbuf __buf;
79 
80 public:
81  socketstream() : std::iostream(&__buf) { }
82 
83  explicit socketstream(int s) : std::iostream(&__buf), __buf(s) { }
84 
85  socketstream(const char hostname[], int port)
86  : std::iostream(&__buf) { open(hostname, port); }
87 
88  socketbuf *rdbuf() { return &__buf; }
89 
90  int open(const char hostname[], int port)
91  {
92  int err = __buf.open(hostname, port);
93  if (err)
94  setstate(std::ios::failbit);
95  return err;
96  }
97 
98  int close() { return __buf.close(); }
99 
100  bool is_open() { return __buf.is_open(); }
101 
102  virtual ~socketstream() { }
103 };
104 
105 
107 {
108 private:
109  int listen_socket;
110 
111 public:
112  explicit socketserver(int port);
113 
114  bool good() { return (listen_socket >= 0); }
115 
116  int close();
117 
118  int accept(socketstream &sockstr);
119 
121 };
122 
123 }
124 
125 #endif
virtual std::streamsize xsgetn(char_type *__s, std::streamsize __n)
socketbuf * rdbuf()
int getsocketdescriptor()
int accept(socketstream &sockstr)
virtual int_type underflow()
socketstream(const char hostname[], int port)
virtual int_type overflow(int_type c=traits_type::eof())
int attach(int sd)
virtual int sync()
virtual std::streamsize xsputn(const char_type *__s, std::streamsize __n)
int open(const char hostname[], int port)
socketbuf(const char hostname[], int port)
int open(const char hostname[], int port)