1 /* Copyright 2008 Sun Microsystems, Inc.
2 All rights reserved. Use is subject to license terms.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
23
24 #include <SocketInputStream2.hpp>
25
26 #include <NdbOut.hpp>
27
28 bool
gets(BaseString & str)29 SocketInputStream2::gets(BaseString& str)
30 {
31 if (get_buffered_line(str))
32 return true;
33
34 char buf[16];
35 do {
36 ssize_t read_res = read_socket(buf, sizeof(buf));
37 if (read_res == -1)
38 return false;
39
40 if (!add_buffer(buf, read_res))
41 return false;
42
43 if (get_buffered_line(str))
44 return true;
45
46 } while(true);
47
48 abort(); // Should never come here
49 return false;
50 };
51
52
53 bool
has_data_to_read()54 SocketInputStream2::has_data_to_read()
55 {
56 const int res = ndb_poll(m_socket, true, false, false,
57 m_read_timeout * 1000);
58
59 if (res == 1)
60 return true; // Yes, there was data
61
62 if (res == 0)
63 return false; // Timeout occured
64
65 require(res == -1);
66 return false;
67 }
68
69
70 ssize_t
read_socket(char * buf,size_t len)71 SocketInputStream2::read_socket(char* buf, size_t len)
72 {
73 if (!has_data_to_read())
74 return -1;
75
76 size_t read_res = my_recv(m_socket, buf, len, 0);
77 if (read_res == 0)
78 return -1; // Has data to read but only EOF received
79
80 return read_res;
81 }
82
83
84 bool
get_buffered_line(BaseString & str)85 SocketInputStream2::get_buffered_line(BaseString& str)
86 {
87 char *start, *ptr;
88 char *end = (char*)m_buffer.get_data() + m_buffer.length();
89 start = ptr =(char*)m_buffer.get_data() + m_buffer_read_pos;
90
91 while(ptr && ptr < end && *ptr)
92 {
93 if (*ptr == '\n')
94 {
95 size_t len = ptr-start;
96 /* Found end of line, return this part of the buffer */
97 str.assign(start, len);
98
99 /*
100 Set new read position in buffer, increase with
101 one to step past '\n'
102 */
103 m_buffer_read_pos += (len + 1);
104
105 return true;
106 }
107 ptr++;
108 }
109 return false;
110 }
111
112
113 bool
add_buffer(char * buf,ssize_t len)114 SocketInputStream2::add_buffer(char* buf, ssize_t len)
115 {
116 // ndbout_c("add_buffer: '%.*s'", len, buf);
117 if (m_buffer.append(buf, len) != 0)
118 return false;
119 return true;
120 }
121