1 #include "opensrf/transport_socket.h"
5 int main( char* argc, char** argv ) {
7 transport_socket sock_obj;
9 sock_obj.server = "10.0.0.4";
10 sock_obj.data_received_callback = &print_stuff;
12 printf("connecting...\n");
13 if( (tcp_connect( &sock_obj )) < 0 ) {
14 printf( "error connecting" );
17 printf("sending...\n");
18 if( tcp_send( &sock_obj, "<stream>\n" ) < 0 ) {
19 printf( "error sending" );
22 printf("waiting...\n");
23 if( tcp_wait( &sock_obj, 15 ) < 0 ) {
24 printf( "error receiving" );
27 printf("disconnecting...\n");
28 tcp_disconnect( &sock_obj );
34 // returns the socket fd, -1 on error
35 int tcp_connect( transport_socket* sock_obj ){
37 if( sock_obj == NULL ) {
38 fatal_handler( "connect(): null sock_obj" );
41 struct sockaddr_in remoteAddr, localAddr;
48 if( WSAStartup(MAKEWORD(1,1), &data) ) {
49 fatal_handler( "somethin's broke with windows socket startup" );
56 // ------------------------------------------------------------------
58 // ------------------------------------------------------------------
59 if( (sock_fd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
60 fatal_handler( "tcp_connect(): Cannot create socket" );
64 // ------------------------------------------------------------------
66 // ------------------------------------------------------------------
67 if( (hptr = gethostbyname( sock_obj->server ) ) == NULL ) {
68 fatal_handler( "tcp_connect(): Unknown Host" );
72 // ------------------------------------------------------------------
73 // Construct server info struct
74 // ------------------------------------------------------------------
75 memset( &remoteAddr, 0, sizeof(remoteAddr));
76 remoteAddr.sin_family = AF_INET;
77 remoteAddr.sin_port = htons( sock_obj->port );
78 memcpy( (char*) &remoteAddr.sin_addr.s_addr,
79 hptr->h_addr_list[0], hptr->h_length );
81 // ------------------------------------------------------------------
82 // Construct local info struct
83 // ------------------------------------------------------------------
84 memset( &localAddr, 0, sizeof( localAddr ) );
85 localAddr.sin_family = AF_INET;
86 localAddr.sin_addr.s_addr = htonl( INADDR_ANY );
87 localAddr.sin_port = htons(0);
89 // ------------------------------------------------------------------
90 // Bind to a local port
91 // ------------------------------------------------------------------
92 if( bind( sock_fd, (struct sockaddr *) &localAddr, sizeof( localAddr ) ) < 0 ) {
93 fatal_handler( "tcp_connect(): Cannot bind to local port" );
97 // ------------------------------------------------------------------
99 // ------------------------------------------------------------------
100 if( connect( sock_fd, (struct sockaddr*) &remoteAddr, sizeof( struct sockaddr_in ) ) < 0 ) {
101 fatal_handler( "tcp_connect(): Cannot connect to server %s", sock_obj->server );
105 sock_obj->sock_fd = sock_fd;
106 sock_obj->connected = 1;
112 int tcp_send( transport_socket* sock_obj, const char* data ){
114 if( sock_obj == NULL ) {
115 fatal_handler( "tcp_send(): null sock_obj" );
119 //fprintf( stderr, "TCP Sending: \n%s\n", data );
121 // ------------------------------------------------------------------
122 // Send the data down the TCP pipe
123 // ------------------------------------------------------------------
124 debug_handler( "Sending Data At %f Seconds", get_timestamp_millis() );
125 if( send( sock_obj->sock_fd, data, strlen(data), 0 ) < 0 ) {
126 fatal_handler( "tcp_send(): Error sending data" );
133 int tcp_disconnect( transport_socket* sock_obj ){
135 if( sock_obj == NULL ) {
136 fatal_handler( "tcp_disconnect(): null sock_obj" );
140 if( close( sock_obj->sock_fd ) == -1 ) {
142 // ------------------------------------------------------------------
143 // Not really worth throwing an exception for... should be logged.
144 // ------------------------------------------------------------------
145 warning_handler( "tcp_disconnect(): Error closing socket" );
152 // ------------------------------------------------------------------
153 // And now for the gory C socket code.
154 // Returns 0 on failure, 1 otherwise
155 // ------------------------------------------------------------------
156 int tcp_wait( transport_socket* sock_obj, int timeout ){
158 if( sock_obj == NULL ) {
159 fatal_handler( "tcp_wait(): null sock_obj" );
166 int sock_fd = sock_obj->sock_fd;
171 FD_ZERO( &read_set );
172 FD_SET( sock_fd, &read_set );
174 // ------------------------------------------------------------------
175 // Build the timeval struct
176 // ------------------------------------------------------------------
181 if( timeout == -1 ) {
183 // ------------------------------------------------------------------
184 // If timeout is -1, there is no timeout passed to the call to select
185 // ------------------------------------------------------------------
186 if( (retval = select( sock_fd + 1 , &read_set, NULL, NULL, NULL)) == -1 ) {
187 warning_handler( "Call to select interrupted" );
191 } else if( timeout != 0 ) { /* timeout of 0 means don't block */
193 if( (retval = select( sock_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
194 warning_handler( "Call to select interrupted" );
199 memset( &buf, 0, BUFSIZE );
201 if( set_fl( sock_fd, O_NONBLOCK ) < 0 )
204 #ifdef _ROUTER // just read one buffer full of data
206 n = recv(sock_fd, buf, BUFSIZE-1, 0);
207 sock_obj->data_received_callback( sock_obj->user_data, buf );
211 #else // read everything we can
213 debug_handler( "Leaving Socket Select At %f Seconds", get_timestamp_millis() );
214 while( (n = recv(sock_fd, buf, BUFSIZE-1, 0) ) > 0 ) {
215 debug_handler("SOCKET Read: \n%s\n", buf);
216 sock_obj->data_received_callback( sock_obj->user_data, buf );
217 memset( &buf, 0, BUFSIZE );
222 if( clr_fl( sock_fd, O_NONBLOCK ) < 0 ) {
227 if( errno != EAGAIN ) {
228 warning_handler( " * Error reading socket with errno %d", errno );
241 int set_fl( int fd, int flags ) {
245 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
246 fatal_handler("fcntl F_GETFL error");
252 if( fcntl( fd, F_SETFL, val ) < 0 ) {
253 fatal_handler( "fcntl F_SETFL error" );
259 int clr_fl( int fd, int flags ) {
263 if( (val = fcntl( fd, F_GETFL, 0) ) < 0 ) {
264 fatal_handler("fcntl F_GETFL error" );
270 if( fcntl( fd, F_SETFL, val ) < 0 ) {
271 fatal_handler( "fcntl F_SETFL error" );
279 int tcp_connected( transport_socket* obj ) {
282 if( ! obj->sock_fd ) { return 0; }
284 ret = read( obj->sock_fd , NULL,0 );