|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <sys/socket.h> |
|
19 #include <netinet/in.h> |
|
20 #include <netdb.h> |
|
21 #include <unistd.h> |
|
22 #include <errno.h> |
|
23 #include <arpa/inet.h> |
|
24 #include <fcntl.h> |
|
25 #include <sys/select.h> |
|
26 #ifndef __SYMBIAN32__ |
|
27 #include <netinet/tcp.h> // TCP_NODELAY |
|
28 #endif |
|
29 #include <string.h> |
|
30 |
|
31 #include "logger.h" |
|
32 |
|
33 #include "socket.h" |
|
34 |
|
35 const int INVALID_SOCKET = 0; |
|
36 |
|
37 namespace java |
|
38 { |
|
39 namespace comms |
|
40 { |
|
41 |
|
42 Socket::Socket() : mSocket(INVALID_SOCKET) |
|
43 { |
|
44 } |
|
45 |
|
46 Socket::Socket(int aSocket) : mSocket(aSocket) |
|
47 { |
|
48 setSockOpts(); |
|
49 } |
|
50 |
|
51 Socket::~Socket() |
|
52 { |
|
53 close(); |
|
54 } |
|
55 |
|
56 int Socket::open(int aPort) |
|
57 { |
|
58 sockaddr_in addr; |
|
59 |
|
60 addr.sin_family = AF_INET; |
|
61 addr.sin_port = htons(aPort); |
|
62 addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
|
63 |
|
64 mSocket = socket(AF_INET, SOCK_STREAM , 0); |
|
65 |
|
66 setSockOpts(); |
|
67 |
|
68 int rc = connect(mSocket, (sockaddr*)&addr, sizeof(addr)); |
|
69 |
|
70 if (rc<0) |
|
71 { |
|
72 ELOG2(EJavaComms, "connect failed %d - %s", errno, strerror(errno)); |
|
73 close(); |
|
74 } |
|
75 else |
|
76 { |
|
77 rc = 0; |
|
78 } |
|
79 |
|
80 return rc; |
|
81 } |
|
82 |
|
83 // returns: 0 ok |
|
84 // < 0 in error |
|
85 int Socket::read(char *aBuf, int len) |
|
86 { |
|
87 int rc = 0; |
|
88 do |
|
89 { |
|
90 if (mSocket==INVALID_SOCKET) return -1; |
|
91 |
|
92 fd_set readset; |
|
93 FD_ZERO(&readset); |
|
94 FD_SET(mSocket, &readset); |
|
95 |
|
96 timeval tv; |
|
97 tv.tv_sec = 600; |
|
98 tv.tv_usec = 0; |
|
99 |
|
100 rc = select(mSocket+1, &readset, 0, 0, &tv); |
|
101 |
|
102 if (rc < 0 && errno != EINTR) |
|
103 { |
|
104 ELOG3(EJavaComms, "select failed %d - %s - fd=%d", errno, strerror(errno), mSocket); |
|
105 break; |
|
106 } |
|
107 else if (rc > 0 && FD_ISSET(mSocket, &readset)) |
|
108 { |
|
109 rc = recv(aBuf, len); |
|
110 break; |
|
111 } |
|
112 } |
|
113 while (1); |
|
114 |
|
115 return rc; |
|
116 } |
|
117 |
|
118 |
|
119 // returns: 0 ok (all bytes read) |
|
120 // < 0 in error |
|
121 int Socket::recv(char *aBuf, int len) |
|
122 { |
|
123 int count = len; |
|
124 while (count > 0) |
|
125 { |
|
126 int rc = ::recv(mSocket, aBuf, count, 0); |
|
127 |
|
128 if (rc < 0) |
|
129 { |
|
130 if (errno == EINTR) continue; |
|
131 WLOG3(EJavaComms, "recv failed %d - %s - fd=%d", errno, strerror(errno), mSocket); |
|
132 return -1; |
|
133 } |
|
134 if (rc == 0) // EOF |
|
135 { |
|
136 // LOG1(EJavaComms, EInfo, "EOF received - exiting fd=%d", mSocket); |
|
137 return -1; |
|
138 } |
|
139 aBuf += rc; |
|
140 count -= rc; |
|
141 } |
|
142 return 0; |
|
143 } |
|
144 |
|
145 // returns: 0 in ok |
|
146 // < 0 in error |
|
147 int Socket::write(const char *aBuf, int len) |
|
148 { |
|
149 int sent = 0; |
|
150 do |
|
151 { |
|
152 int rc = send(mSocket, aBuf+sent, len-sent, 0); |
|
153 if (rc > 0) |
|
154 sent += rc; |
|
155 else if (rc < 0 && errno != EINTR) |
|
156 { |
|
157 WLOG3(EJavaComms, "send failed %d - %s - fd=%d", errno, strerror(errno), mSocket); |
|
158 return -1; |
|
159 } |
|
160 } |
|
161 while (len > sent); |
|
162 |
|
163 return 0; |
|
164 } |
|
165 |
|
166 int Socket::shutdown(int aHow) |
|
167 { |
|
168 int rc = ::shutdown(mSocket, aHow); |
|
169 if (rc<0) WLOG3(EJavaComms, "shutdown failed %d - %s - fd=%d", errno, strerror(errno), mSocket); |
|
170 |
|
171 return rc; |
|
172 } |
|
173 |
|
174 void Socket::close() |
|
175 { |
|
176 if (mSocket !=INVALID_SOCKET) |
|
177 { |
|
178 int rc = ::close(mSocket); |
|
179 if (rc<0) WLOG3(EJavaComms, "close failed %d - %s - fd=%d", errno, strerror(errno), mSocket); |
|
180 |
|
181 mSocket = INVALID_SOCKET; |
|
182 } |
|
183 } |
|
184 |
|
185 int Socket::getSocket() const |
|
186 { |
|
187 return mSocket; |
|
188 } |
|
189 |
|
190 void Socket::setSockOpts() |
|
191 { |
|
192 int flag = 1; |
|
193 int rc = setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int)); |
|
194 if (rc<0) ELOG3(EJavaComms, "setsockopt(SO_REUSEADDR) failed %d - %s - fd=%d", errno, strerror(errno), mSocket); |
|
195 |
|
196 rc = setsockopt(mSocket, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); |
|
197 if (rc<0) ELOG3(EJavaComms, "setsockopt(TCP_NODELAY) failed %d - %s - fd=%d", errno, strerror(errno), mSocket); |
|
198 } |
|
199 |
|
200 } // namespace comms |
|
201 } // namespace java |