ksock.h
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (C) 1997 Torben Weis (weis@kde.org) 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 #ifndef KSOCK_H 00021 #define KSOCK_H 00022 00023 #include "kdelibs_export.h" 00024 00025 #ifdef Q_OS_UNIX 00026 00027 #include <qobject.h> 00028 #include <sys/types.h> 00029 // we define STRICT_ANSI to get rid of some warnings in glibc 00030 #ifndef __STRICT_ANSI__ 00031 #define __STRICT_ANSI__ 00032 #define _WE_DEFINED_IT_ 00033 #endif 00034 #include <sys/socket.h> 00035 #ifdef _WE_DEFINED_IT_ 00036 #undef __STRICT_ANSI__ 00037 #undef _WE_DEFINED_IT_ 00038 #endif 00039 00040 #include <sys/un.h> 00041 00042 #include <netinet/in.h> 00043 class QSocketNotifier; 00044 00045 #ifdef KSOCK_NO_BROKEN 00046 // This is here for compatibility with old applications still using the constants 00047 // Never use them in new programs 00048 00049 // Here are a whole bunch of hackish macros that allow one to 00050 // get at the correct member of ksockaddr_in 00051 // But since ksockaddr_in is IPv4-only, and deprecated... 00052 00053 typedef sockaddr_in ksockaddr_in; 00054 #define get_sin_addr(x) x.sin_addr 00055 #define get_sin_port(x) x.sin_port 00056 #define get_sin_family(x) x.sin_family 00057 #define get_sin_paddr(x) x->sin_addr 00058 #define get_sin_pport(x) x->sin_port 00059 #define get_sin_pfamily(x) x->sin_family 00060 #endif 00061 00062 #define KSOCK_DEFAULT_DOMAIN PF_INET 00063 00064 class KSocketPrivate; 00065 class KServerSocketPrivate; 00066 00087 class KDECORE_EXPORT KSocket : public QObject 00088 { 00089 Q_OBJECT 00090 public: 00095 KSocket( int _sock ) KDE_DEPRECATED; 00102 KSocket( const char *_host, unsigned short int _port, int timeOut = 30) KDE_DEPRECATED; 00103 00108 KSocket( const char * _path ) KDE_DEPRECATED; 00109 00113 virtual ~KSocket(); 00114 00119 int socket() const { return sock; } 00120 00129 void enableRead( bool enable ); 00130 00142 void enableWrite( bool enable ); 00143 00144 #ifdef KSOCK_NO_BROKEN 00145 // BCI: remove in libkdecore.so.4 00153 unsigned long ipv4_addr() KDE_DEPRECATED; 00154 00155 // BCI: remove in libkdecore.so.4 00161 static bool initSockaddr(ksockaddr_in *server_name, const char *hostname, unsigned short int port, int domain = PF_INET) KDE_DEPRECATED; 00162 #endif 00163 00164 signals: 00172 void readEvent( KSocket *s ); 00173 00185 void writeEvent( KSocket *s ); 00186 00191 void closeEvent( KSocket *s ); 00192 00193 public slots: 00201 void slotWrite( int x); 00202 00210 void slotRead( int x ); 00211 00212 protected: 00213 bool connect( const QString& _host, unsigned short int _port, int timeout = 0 ); 00214 bool connect( const char *_path ); 00215 00216 /****************************************************** 00217 * The file descriptor for this socket. sock may be -1. 00218 * This indicates that it is not connected. 00219 */ 00220 int sock; 00221 00222 private: 00223 KSocket(const KSocket&); 00224 KSocket& operator=(const KSocket&); 00225 00226 KSocketPrivate *d; 00227 00228 }; 00229 00230 00250 class KDECORE_EXPORT KServerSocket : public QObject 00251 { 00252 Q_OBJECT 00253 public: 00261 KServerSocket( unsigned short int _port, bool _bind = true ); 00262 00270 KServerSocket( const char *_path, bool _bind = true); 00271 00275 virtual ~KServerSocket(); 00276 00283 bool bindAndListen(); 00284 00290 int socket() const { return sock; } 00291 00296 unsigned short int port(); 00297 00298 #ifdef KSOCK_NO_BROKEN 00299 // BCI: remove in libkdecore.so.4 00306 unsigned long ipv4_addr(); 00307 #endif 00308 00309 public slots: 00313 virtual void slotAccept( int ); // why is this virtual? 00314 00315 signals: 00325 void accepted( KSocket*s ); 00326 00327 protected: 00328 bool init( unsigned short int ); 00329 bool init( const char *_path ); 00330 00335 int sock; 00336 00337 private: 00338 KServerSocket(const KServerSocket&); 00339 KServerSocket& operator=(const KServerSocket&); 00340 00341 KServerSocketPrivate *d; 00342 }; 00343 00344 #endif //Q_OS_UNIX 00345 00346 #endif