Debian Codegraph @

src/socket.cpp

raw
1 // Copyright © 2002-2004 Rob Kaper <[email protected]>.
2 // 2010-2015 Sylvain Rochet <[email protected]>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // version 2 as published by the Free Software Foundation.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; see the file COPYING. If not, see
15 // <http://www.gnu.org/licenses/>.
16
17 #include "config.h"
18
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <netinet/tcp.h>
27
28 #include "socket.h"
29
30 Socket::Socket( int fd )
31 : m_status( Socket::Ok ),
32 m_type( Socket::Player ),
33 m_fd( fd ),
34 m_addrinfoNext( NULL )
35 {
36 }
37
38 void Socket::ioWrite(const std::string data)
39 {
40 if (m_status != Ok) {
41 return;
42 }
43
44 if (m_sendBuf.size() == 0) {
45 ssize_t len = strlen(data.c_str());
46 ssize_t written = write(m_fd, data.c_str(), len);
47 if (written == len) {
48 if (data.at(len -1) == '\n') {
49 int flag = 1;
50 setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
51 flag = 0;
52 setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
53 }
54 return;
55 }
56
57 if (written < 0) {
58 switch(errno) {
59 case EAGAIN:
60 case EINTR:
61 m_sendBuf.append(data);
62 break;
63 default:
64 setStatus(Socket::Close);
65 }
66 return;
67 }
68
69 m_sendBuf.append(data.substr(written, len - written));
70 return;
71 }
72
73 m_sendBuf.append(data);
74 }
75
76 void Socket::sendMore()
77 {
78 ssize_t len = strlen(m_sendBuf.c_str());
79 if (len <= 0) {
80 m_sendBuf.erase();
81 return;
82 }
83
84 ssize_t written = write(m_fd, m_sendBuf.c_str(), len);
85 if (written == len) {
86 if (m_sendBuf.at(len -1) == '\n') {
87 int flag = 1;
88 setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
89 flag = 0;
90 setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
91 }
92 m_sendBuf.erase();
93 return;
94 }
95
96 if (written < 0) {
97 switch(errno) {
98 case EAGAIN:
99 case EINTR:
100 return;
101 default:
102 setStatus(Socket::Close);
103 }
104 return;
105 }
106
107 m_sendBuf.erase(0, written);
108 }
109
110 bool Socket::hasReadLine()
111 {
112 static std::string newLine = "\r\n";
113 std::string::size_type pos = m_recvBuf.find_first_of(newLine);
114
115 return (!(pos == std::string::npos));
116 }
117
118 const std::string Socket::readLine()
119 {
120 static std::string newLine = "\r\n";
121 std::string::size_type pos = m_recvBuf.find_first_of(newLine);
122
123 if (pos != std::string::npos)
124 {
125 // Grab first part for the listener
126 std::string data = m_recvBuf.substr(0, pos);
127
128 // Remove grabbed part from buffer
129 m_recvBuf.erase(0, pos);
130
131 // Remove all subsequent newlines
132 m_recvBuf.erase(0, m_recvBuf.find_first_not_of(newLine));
133
134 return data;
135 }
136 return std::string();
137 }
138
139 void Socket::fillBuffer(const std::string data)
140 {
141 if (m_recvBuf.size())
142 m_recvBuf.append(data);
143 else
144 {
145 m_recvBuf.erase();
146 m_recvBuf.append(data);
147 }
148 }