source: BAORadio/libindi/libindi/drivers/telescope/Socket.cpp @ 490

Last change on this file since 490 was 490, checked in by campagne, 14 years ago

import libindi (JEC)

File size: 4.3 KB
Line 
1// Implementation of the Socket class.
2
3
4#include "Socket.h"
5#include "string.h"
6#include <string.h>
7#include <errno.h>
8#include <fcntl.h>
9#include <iostream>
10
11
12
13Socket::Socket() :
14        m_sock ( -1 )
15{
16
17    memset ( &m_addr,
18             0,
19             sizeof ( m_addr ) );
20}
21
22Socket::~Socket()
23{
24    if ( is_valid() )
25        ::close ( m_sock );
26}
27
28bool Socket::create()
29{
30    m_sock = socket ( AF_INET,
31                      SOCK_STREAM,
32                      0); //IPPROTO_TCP
33
34    if ( ! is_valid() )
35        return false;
36
37
38   //  TIME_WAIT - argh
39    int on = 1;
40    if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 ) return false;
41
42    return true;
43}
44
45
46
47bool Socket::bind ( const int port )
48{
49    if ( ! is_valid() ) return false;
50   
51    m_addr.sin_family = AF_INET;
52    m_addr.sin_addr.s_addr = INADDR_ANY;
53    m_addr.sin_port =  htons ( port );
54
55    int bind_return = ::bind ( m_sock,
56                               ( struct sockaddr * ) &m_addr,
57                               sizeof ( m_addr ) );
58
59
60    if ( bind_return == -1 ) return false;
61   
62    return true;
63}
64
65
66bool Socket::listen() const
67{
68    if ( ! is_valid() ) return false;
69   
70    int listen_return = ::listen ( m_sock, MAXCONNECTIONS );
71
72    if ( listen_return == -1 ) return false;
73   
74    return true;
75}
76
77
78bool Socket::accept ( Socket& new_socket ) const
79{
80    int addr_length = sizeof ( m_addr );
81   
82    new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );
83
84    if ( new_socket.m_sock <= 0 ) return false;
85       
86    return true;
87}
88
89
90bool Socket::send ( const std::string s ) const
91{
92    int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL);
93   
94    if ( status == -1 ) return false;
95       
96    return true;   
97}
98
99
100bool Socket::close(  Socket& new_socket ) const
101{
102  int status = ::close ( m_sock );
103   
104    if ( status == -1 ) return false;
105       
106    return true;     
107}
108
109bool Socket::shutdown(  Socket& new_socket ) const
110{
111  int status = ::shutdown ( m_sock, SHUT_RDWR );
112   
113    if ( status == -1 ) return false;
114       
115    return true;     
116}
117
118
119int Socket::recv ( std::string& s ) const
120{
121    int sel;
122    fd_set r;
123    struct timeval timeout;
124   
125    s="";
126
127    timeout.tv_sec = 0; // initialise le timeout, ici  les secondes
128    timeout.tv_usec = 1; // les microsecondes
129
130    FD_ZERO(&r); // initialise la liste de fd
131    FD_SET(m_sock, &r); // ajoute la socket dans la liste des fd
132    sel = select(m_sock + 1, &r, NULL, NULL, &timeout);
133   
134    if (sel < 0) // select a rencontré un problÚme
135    {
136        return -1;
137    }
138    else if (sel == 0) // timeout
139    {
140        s="\n";
141        return 1;
142    }
143    else // quelque chose est à lire sur un file descriptor
144    {
145        if (FD_ISSET(m_sock, &r)) // si la socket est prête à être lue
146        {
147            char buf [ MAXRECV + 1 ];
148
149            s = "";
150
151            memset ( buf, 0, MAXRECV + 1 );
152
153            int status = ::recv ( m_sock, buf, MAXRECV, 0 );
154
155            if ( status == -1 )
156            {
157                 // là, nous ne sommes pas en présence d'une vraie erreur
158                 // car le mode non-bloquant est activé ici
159                 // (consulter la doc de la fct recv...)
160                 if (errno == EAGAIN )
161                 {
162                   s="\n";
163                   return 1;
164                 }
165                 
166                 return -1;
167            }
168            else if ( status == 0 )
169            {
170                return 0;
171            }
172            else
173            {
174                s = buf;
175                return status;
176            }
177        }
178    }
179}
180
181
182
183bool Socket::connect ( const std::string host, const int port )
184{
185    if ( ! is_valid() ) return false;
186
187    m_addr.sin_family = AF_INET;
188    m_addr.sin_port = htons ( port );
189
190    int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr );
191
192    if ( errno == EAFNOSUPPORT ) return false;
193
194    status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );
195
196    if ( status == 0 ) return true;
197   
198    return false;
199}
200
201
202void Socket::set_non_blocking ( const bool b )
203{
204    int opts;
205
206    opts = fcntl ( m_sock,F_GETFL );
207
208    if ( opts < 0 )
209    {
210        return;
211    }
212
213    if ( b )
214        opts = ( opts | O_NONBLOCK );
215    else
216        opts = ( opts & ~O_NONBLOCK );
217
218    fcntl ( m_sock,
219            F_SETFL,opts );
220
221}
222
223
224std::string Socket::recupip() const
225{
226    return inet_ntoa(m_addr.sin_addr);
227}
Note: See TracBrowser for help on using the repository browser.