MFEM v2.0
socketstream.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
00002 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
00003 // reserved. See file COPYRIGHT for details.
00004 //
00005 // This file is part of the MFEM library. For more information and source code
00006 // availability see http://mfem.googlecode.com.
00007 //
00008 // MFEM is free software; you can redistribute it and/or modify it under the
00009 // terms of the GNU Lesser General Public License (as published by the Free
00010 // Software Foundation) version 2.1 dated February 1999.
00011 
00012 #ifndef MFEM_SOCKETSTREAM
00013 #define MFEM_SOCKETSTREAM
00014 
00015 #include <iostream>
00016 
00017 class socketbuf : public std::streambuf
00018 {
00019 private:
00020    int socket_descriptor;
00021    static const int buflen = 1024;
00022    char ibuf[buflen], obuf[buflen];
00023 
00024 public:
00025    socketbuf()
00026    {
00027       socket_descriptor = -1;
00028    }
00029 
00030    explicit socketbuf(int sd)
00031    {
00032       socket_descriptor = sd;
00033       setp(obuf, obuf + buflen);
00034    }
00035 
00036    socketbuf(const char hostname[], int port)
00037    {
00038       socket_descriptor = -1;
00039       open(hostname, port);
00040    }
00041 
00044    int attach(int sd);
00045 
00046    int detach() { return attach(-1); }
00047 
00048    int open(const char hostname[], int port);
00049 
00050    int close();
00051 
00052    int getsocketdescriptor() { return socket_descriptor; }
00053 
00054    bool is_open() { return (socket_descriptor >= 0); }
00055 
00056    ~socketbuf() { close(); }
00057 
00058 protected:
00059    virtual int sync();
00060 
00061    virtual int_type underflow();
00062 
00063    virtual int_type overflow(int_type c = traits_type::eof());
00064 
00065    virtual std::streamsize xsgetn(char_type *__s, std::streamsize __n);
00066 
00067    virtual std::streamsize xsputn(const char_type *__s, std::streamsize __n);
00068 };
00069 
00070 
00071 class socketstream : public std::iostream
00072 {
00073 private:
00074    socketbuf __buf;
00075 
00076 public:
00077    socketstream() : std::iostream(&__buf) { }
00078 
00079    explicit socketstream(int s) : std::iostream(&__buf), __buf(s) { }
00080 
00081    socketstream(const char hostname[], int port)
00082       : std::iostream(&__buf), __buf(hostname, port) { }
00083 
00084    socketbuf *rdbuf() { return &__buf; }
00085 
00086    int open(const char hostname[], int port)
00087    {
00088       return __buf.open(hostname, port);
00089    }
00090 
00091    int close() { return __buf.close(); }
00092 
00093    bool is_open() { return __buf.is_open(); }
00094 
00095    virtual ~socketstream() { }
00096 };
00097 
00098 
00099 class socketserver
00100 {
00101 private:
00102    int listen_socket;
00103 
00104 public:
00105    explicit socketserver(int port);
00106 
00107    bool good() { return (listen_socket >= 0); }
00108 
00109    int close();
00110 
00111    int accept(socketstream &sockstr);
00112 
00113    ~socketserver() { close(); }
00114 };
00115 
00116 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines