Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

unix_pipe_connection.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: unix_pipe_connection.cpp,v 1.1 2001/02/15 13:18:52 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #include "Core/precomp.h"
00016 
00017 #ifdef USE_NETWORK
00018 
00019 #include <API/Core/System/error.h>
00020 #include "API/Core/System/cl_assert.h"
00021 #include <Core/Network/Generic/network_delivery_impl.h>
00022 #include <Core/Network/Unix/network_delivery_unix.h>
00023 #include <Core/Network/Unix/network_delivery_pipe.h>
00024 
00025 #include <string.h>
00026 #include <errno.h>
00027 
00028 /******************************************************************************
00029                       Unix PipeConnection implementation
00030 ******************************************************************************/
00031 
00032 CL_UnixPipeConnection::CL_UnixPipeConnection(int _read, int _write)
00033 {
00034         read = _read;
00035         write = _write;
00036         is_connection_lost = false;
00037 }
00038 
00039 CL_UnixPipeConnection::~CL_UnixPipeConnection()
00040 {
00041         close(read);
00042         close(write);
00043 }
00044 
00045 bool CL_UnixPipeConnection::peek()
00046 {
00047         fd_set rfds;
00048         FD_ZERO(&rfds);
00049         FD_SET(read, &rfds);
00050 
00051         timeval timeout;
00052         memset(&timeout, 0, sizeof(timeval));
00053 
00054         int retval = select(read+1, &rfds, NULL, NULL, &timeout);
00055         return retval > 0;
00056 }
00057 
00058 CL_ConnectionPacket CL_UnixPipeConnection::receive()
00059 {
00060         unsigned long size_data = read_int();
00061 
00062         unsigned char *data = new unsigned char[size_data];
00063         read_data(data, size_data);
00064 
00065         CL_ConnectionPacket ret;
00066         ret.size = size_data;
00067         ret.data = data;
00068 
00069         return ret;
00070 }
00071 
00072 void CL_UnixPipeConnection::send(CL_ConnectionPacket message)
00073 {
00074         write_int(message.size);
00075         write_data(message.data, message.size);
00076 }
00077 
00078 bool CL_UnixPipeConnection::connection_lost()
00079 {
00080         return is_connection_lost;
00081 }
00082 
00083 unsigned long CL_UnixPipeConnection::read_int()
00084 {
00085         unsigned long ret;
00086         read_data(&ret, sizeof(unsigned long));
00087 
00088         return ret;
00089 }
00090 
00091 void CL_UnixPipeConnection::read_data(void *dest, int size_data)
00092 {
00093         int total_read = 0;
00094         while (total_read < size_data)
00095         {
00096                 int res = ::read(
00097                         read, 
00098                         ((unsigned char *) dest)+total_read,
00099                         size_data-total_read);
00100 
00101                 if (res == 0)
00102                 {
00103                         std::cout << "OH MY GOD - TIME TO QUIT" << std::endl;
00104                         is_connection_lost = true;
00105                         return;
00106                 }
00107                         
00108                 total_read += res;
00109         }
00110 }
00111 
00112 void CL_UnixPipeConnection::write_int(unsigned long value)
00113 {
00114         write_data(&value, sizeof(unsigned long));
00115 }
00116 
00117 inline void CL_UnixPipeConnection::write_data(void *data, unsigned int size)
00118 {
00119         ::write(write, data, size);
00120 }
00121 
00122 #endif // USE_NETWORK
00123 
00124 

Generated at Wed Apr 4 19:54:04 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001