source: BAORadio/libindi/libindi/BAOControl/Socket.cpp @ 614

Last change on this file since 614 was 504, checked in by frichard, 13 years ago

-Version 0.8 de libini
-Formule de Marc
-Nouvelles fonctionnalités (goto nom-de l'objet etc...)

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