root/juggler/tags/1.0.5/Environment/vjSockStream.cpp
| Revision 7539, 3.2 kB (checked in by anonymous, 7 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /*************** <auto-copyright.pl BEGIN do not edit this line> ************** |
| 2 | * |
| 3 | * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University |
| 4 | * |
| 5 | * Original Authors: |
| 6 | * Allen Bierbaum, Christopher Just, |
| 7 | * Patrick Hartling, Kevin Meinert, |
| 8 | * Carolina Cruz-Neira, Albert Baker |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Library General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Library General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Library General Public |
| 21 | * License along with this library; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 02111-1307, USA. |
| 24 | * |
| 25 | * ----------------------------------------------------------------- |
| 26 | * File: $RCSfile$ |
| 27 | * Date modified: $Date$ |
| 28 | * Version: $Revision$ |
| 29 | * ----------------------------------------------------------------- |
| 30 | * |
| 31 | *************** <auto-copyright.pl END do not edit this line> ***************/ |
| 32 | |
| 33 | #include <vjConfig.h> |
| 34 | |
| 35 | #ifdef VJ_OS_Win32 |
| 36 | #include <winsock2.h> |
| 37 | #else |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/socket.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <Environment/vjSockStream.h> |
| 43 | |
| 44 | |
| 45 | const size_t sockstreambuf::BUFSIZE = 128; |
| 46 | |
| 47 | |
| 48 | sockstreambuf::sockstreambuf (int _sock) { |
| 49 | sock = _sock; |
| 50 | buf = new char[2*BUFSIZE +2]; //allocate a buffer |
| 51 | setg (buf, buf, buf); //set the get area |
| 52 | setp (buf+BUFSIZE, buf+2*BUFSIZE); //set the put area |
| 53 | } |
| 54 | |
| 55 | |
| 56 | sockstreambuf::sockstreambuf () { |
| 57 | sock = -1; |
| 58 | buf = new char[2*BUFSIZE +2]; //allocate a buffer |
| 59 | setg (buf, buf, buf); //set the get area |
| 60 | setp (buf+BUFSIZE, buf+2*BUFSIZE); //set the put area |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | int sockstreambuf::overflow (int c) { |
| 66 | //std::cout << "overflow: pbase is " << (int)pbase() << " and msg size is " << (int)(pptr()-pbase()) << std::endl; |
| 67 | |
| 68 | send (sock, pbase(), pptr()-pbase(), 0); |
| 69 | setp (buf+BUFSIZE, buf+2*BUFSIZE); //set the put area |
| 70 | if (c != EOF) |
| 71 | sputc(c); |
| 72 | return 1; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | sockstreambuf::int_type sockstreambuf::underflow () { |
| 78 | char c; |
| 79 | //std::cout << "underflow: gptr is " << (int)gptr() << " and egptr is " << (int)egptr() << std::endl; |
| 80 | if (gptr() < egptr()) { |
| 81 | // does underflow ever actually get called under this circumstance? |
| 82 | // there's already a character to read... |
| 83 | c = *gptr(); |
| 84 | return c; |
| 85 | } |
| 86 | else { |
| 87 | int nread = recv (sock, /*base()*/eback(), BUFSIZE, 0); |
| 88 | //cout << "nread = " << nread << endl; |
| 89 | if (nread > 0) { |
| 90 | setg (eback(), eback(), eback()+nread/*base(), base(), base()+nread*/); |
| 91 | return *eback()/**base()*/; |
| 92 | } |
| 93 | else { |
| 94 | setg (buf, buf, buf); |
| 95 | return EOF; |
| 96 | } |
| 97 | } |
| 98 | return EOF; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | int sockstreambuf::sync() { |
| 103 | //std::cout << "sync was called!" << std::endl; |
| 104 | overflow(); |
| 105 | return 0; |
| 106 | //return streambuf::sync(); |
| 107 | } |
| 108 |
Note: See TracBrowser for help on using the browser.
