00001 #ifndef _FL_SOCKET_H_
00002 #define _FL_SOCKET_H_
00003
00004 #ifndef _WIN32
00005 # include <sys/types.h>
00006 # include <sys/socket.h>
00007 # include <sys/un.h>
00008 # include <unistd.h>
00009 typedef int SOCKET;
00010 # define INVALID_SOCKET -1
00011 #else
00012 # include <winsock2.h>
00013 #endif
00014
00015 #include <efltk/Fl_Exception.h>
00016 #include <efltk/Fl_String.h>
00017 #include <efltk/Fl_Buffer.h>
00018
00019 class FL_API Fl_Socket_Reader : protected Fl_Buffer {
00020 int m_socket;
00021 int m_readOffset;
00022 int buffered_read(char *dest,int sz,bool read_line);
00023 int read_available(char *dest,int sz,bool read_line);
00024 public:
00025 Fl_Socket_Reader(int buffer_size=16384);
00026 void open(int socket);
00027 void close() { m_socket = 0; }
00028 int read(char *dest,int sz,bool read_line=false);
00029 };
00030
00031 class FL_API Fl_Socket {
00032 protected:
00033 SOCKET m_sockfd;
00034 int m_domain;
00035 int m_type;
00036 int m_protocol;
00037 Fl_String m_host;
00038 int m_port;
00039 fd_set m_inputs,m_outputs;
00040 Fl_Socket_Reader m_reader;
00041
00042 protected:
00043 static int m_socketCount;
00044 static bool m_inited;
00045
00046 static void init();
00047 static void cleanup();
00048
00049 char get_char();
00050
00051 public:
00052
00053 Fl_Socket(int domain=AF_INET, int type=SOCK_STREAM, int protocol=0);
00054 virtual ~Fl_Socket();
00055
00056 int handle() const { return m_sockfd; }
00057
00058 void host(Fl_String hostName);
00059 const char *host() const { return m_host; }
00060
00061 void port(int portNumber);
00062 int port() const { return m_port; }
00063
00064
00065 virtual void open(Fl_String hostName="", int port=0);
00066 void open_addr(struct sockaddr_in& addr);
00067 virtual void close();
00068
00069 bool active() const { return m_sockfd != INVALID_SOCKET; }
00070
00071
00072 int control(int flag, unsigned long *check);
00073 int set_option(int level,int option,int value);
00074 int get_option(int level,int option,int& value);
00075
00076
00077 int read_line(char *buffer,int size);
00078 int read_line(Fl_Buffer& buffer);
00079 int read(char *buffer,int size);
00080 int read(Fl_Buffer& buffer);
00081 int write(const char *buffer,int size);
00082 int write(const Fl_Buffer& buffer);
00083
00084 bool ready_to_read(int waitmsec);
00085 bool ready_to_write();
00086 };
00087
00088 #endif