00001
00002
00003
00004
00005
00006
00007
00008 #ifndef _IMPULSE_SOCKET_H_
00009 #define _IMPULSE_SOCKET_H_
00010
00011 #include "../impulse.h"
00012
00013
00014 #include <sys/types.h>
00015 #ifdef _WIN32
00016 #define _WIN32_WINNT 0x0501
00017 #include <winsock2.h>
00018 #include <ws2tcpip.h>
00019 #else
00020 #include <sys/socket.h>
00021 #include <netdb.h>
00022 #endif
00023 #include <cstring>
00024
00025 namespace impulse {
00026
00027
00028
00029
00030
00031 class Socket : public Frame {
00032
00033 public:
00034
00035 Socket( string host, string protocol )
00036 {
00037 _info = getInfo( "www.sun.com", "http" );
00038
00039 _socket = socket( _info->ai_family, _info->ai_socktype, _info->ai_protocol );
00040 cout << "socket = " << _socket << endl;
00041
00042 int result = ::connect( _socket, _info->ai_addr, _info->ai_addrlen );
00043 cout << "connect = " << result << endl;
00044
00045 freeaddrinfo( _info );
00046 }
00047
00048 void initSlots()
00049 {
00050
00051
00052 }
00053
00054 string inspect() { return "[Socket]"; }
00055
00056 addrinfo* getInfo( string host, string protocol )
00057 {
00058 addrinfo hints;
00059 memset( &hints, 0, sizeof( hints ) );
00060 hints.ai_family = AF_UNSPEC;
00061 hints.ai_socktype = SOCK_STREAM;
00062
00063 int result = getaddrinfo( host.c_str(), protocol.c_str(), &hints, &_info );
00064 cout << "getaddrinfo = " << result << endl;
00065
00066 return _info;
00067 }
00068
00069 void send( const char* data )
00070 {
00071 int result = ::send( _socket, data, strlen( data ), 0 );
00072 cout << "send = " << result << endl;
00073 }
00074
00075 void send( const void* data, int length )
00076 {
00077 int result = ::send( _socket, (char*) data, length, 0 );
00078 cout << "send = " << result << endl;
00079 }
00080
00081 string receive()
00082 {
00083 char buffer[4096];
00084
00085 int length = ::recv( _socket, buffer, 4095, 0 );
00086 cout << "recv = " << length << endl;
00087 buffer[length] = '\0';
00088
00089 return buffer;
00090 }
00091
00092 protected:
00093
00094 Value _test( Array& args, Value self )
00095 {
00096 BEG( "Object::_test()" );
00097
00098 Value result = self.setSlot( Symbol::at("foo"), 15.0 );
00099
00100 END( "" );
00101
00102 return result;
00103 }
00104
00105 Value _setSlot_( Array& args, Value self )
00106 {
00107 return setSlot( *args.at(0).get<Symbol>(), args.at(1) );
00108 }
00109
00110 private:
00111
00112 int _socket;
00113 addrinfo* _info;
00114
00115 };
00116
00117 }
00118
00119 #endif