2006-01-17 Andrew Pinski <pinskia@physics.uc.edu>
[official-gcc.git] / libjava / classpath / native / jni / java-net / gnu_java_net_PlainSocketImpl.c
blobf5f22ab77e7d05942333e19b0063aa399ba009ce
1 /* PlainSocketImpl.c - Native methods for PlainSocketImpl class
2 Copyright (C) 1998, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 /* do not move; needed here because of some macro definitions */
39 #include <config.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <assert.h>
46 #include <jni.h>
47 #include <jcl.h>
49 #include "javanet.h"
51 #include "target_native.h"
52 #ifndef WITHOUT_NETWORK
53 #include "target_native_file.h" /* Get FIONREAD on Solaris. */
54 #include "target_native_network.h"
55 #endif /* WITHOUT_NETWORK */
57 #include "gnu_java_net_PlainSocketImpl.h"
60 * Note that the functions in this module simply redirect to another
61 * internal function. Why? Because many of these functions are shared
62 * with PlainDatagramSocketImpl. The unshared ones were done the same
63 * way for consistency.
66 /*************************************************************************/
69 * Creates a new stream or datagram socket
71 JNIEXPORT void JNICALL
72 Java_gnu_java_net_PlainSocketImpl_create (JNIEnv * env, jobject this,
73 jboolean stream)
75 #ifndef WITHOUT_NETWORK
76 assert (env != NULL);
77 assert ((*env) != NULL);
79 _javanet_create (env, this, stream);
80 #else /* not WITHOUT_NETWORK */
81 #endif /* not WITHOUT_NETWORK */
84 /*************************************************************************/
87 * Close the socket. Any underlying streams will be closed by this
88 * action as well.
90 JNIEXPORT void JNICALL
91 Java_gnu_java_net_PlainSocketImpl_close (JNIEnv * env, jobject this)
93 #ifndef WITHOUT_NETWORK
94 assert (env != NULL);
95 assert ((*env) != NULL);
97 _javanet_close (env, this, 1);
98 #else /* not WITHOUT_NETWORK */
99 #endif /* not WITHOUT_NETWORK */
102 /*************************************************************************/
105 * Connects to the specified destination.
107 JNIEXPORT void JNICALL
108 Java_gnu_java_net_PlainSocketImpl_connect (JNIEnv * env, jobject this,
109 jobject addr, jint port)
111 #ifndef WITHOUT_NETWORK
112 assert (env != NULL);
113 assert ((*env) != NULL);
115 _javanet_connect (env, this, addr, port);
116 #else /* not WITHOUT_NETWORK */
117 #endif /* not WITHOUT_NETWORK */
120 /*************************************************************************/
123 * This method binds the specified address to the specified local port.
124 * Note that we have to set the local address and local port public instance
125 * variables.
127 JNIEXPORT void JNICALL
128 Java_gnu_java_net_PlainSocketImpl_bind (JNIEnv * env, jobject this,
129 jobject addr, jint port)
131 #ifndef WITHOUT_NETWORK
132 assert (env != NULL);
133 assert ((*env) != NULL);
135 _javanet_bind (env, this, addr, port, 1);
136 #else /* not WITHOUT_NETWORK */
137 #endif /* not WITHOUT_NETWORK */
140 /*************************************************************************/
143 * Starts listening on a socket with the specified number of pending
144 * connections allowed.
146 JNIEXPORT void JNICALL
147 Java_gnu_java_net_PlainSocketImpl_listen (JNIEnv * env, jobject this,
148 jint queuelen)
150 #ifndef WITHOUT_NETWORK
151 assert (env != NULL);
152 assert ((*env) != NULL);
154 _javanet_listen (env, this, queuelen);
155 #else /* not WITHOUT_NETWORK */
156 #endif /* not WITHOUT_NETWORK */
159 /*************************************************************************/
162 * Accepts a new connection and assigns it to the passed in SocketImpl
163 * object. Note that we assume this is a PlainSocketImpl just like us.
165 JNIEXPORT void JNICALL
166 Java_gnu_java_net_PlainSocketImpl_accept (JNIEnv * env, jobject this,
167 jobject impl)
169 #ifndef WITHOUT_NETWORK
170 assert (env != NULL);
171 assert ((*env) != NULL);
173 _javanet_accept (env, this, impl);
174 #else /* not WITHOUT_NETWORK */
175 #endif /* not WITHOUT_NETWORK */
178 /*************************************************************************/
180 JNIEXPORT jint JNICALL
181 Java_gnu_java_net_PlainSocketImpl_available (JNIEnv * env, jobject this)
183 #ifndef WITHOUT_NETWORK
184 jclass cls;
185 jfieldID fid;
186 int fd;
187 int bytesAvailable;
188 int result;
190 assert (env != NULL);
191 assert ((*env) != NULL);
193 cls = (*env)->GetObjectClass (env, this);
194 if (cls == 0)
196 JCL_ThrowException (env, IO_EXCEPTION, "internal error");
197 return 0;
200 fid = (*env)->GetFieldID (env, cls, "native_fd", "I");
201 if (fid == 0)
203 JCL_ThrowException (env, IO_EXCEPTION, "internal error");
204 return 0;
207 fd = (*env)->GetIntField (env, this, fid);
209 TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE (fd, bytesAvailable, result);
210 if (result != TARGET_NATIVE_OK)
212 JCL_ThrowException (env, IO_EXCEPTION,
213 TARGET_NATIVE_LAST_ERROR_STRING ());
214 return 0;
217 return bytesAvailable;
218 #else /* not WITHOUT_NETWORK */
219 return 0;
220 #endif /* not WITHOUT_NETWORK */
223 /*************************************************************************/
226 * This method sets the specified option for a socket
228 JNIEXPORT void JNICALL
229 Java_gnu_java_net_PlainSocketImpl_setOption (JNIEnv * env, jobject this,
230 jint option_id, jobject val)
232 #ifndef WITHOUT_NETWORK
233 assert (env != NULL);
234 assert ((*env) != NULL);
236 _javanet_set_option (env, this, option_id, val);
237 #else /* not WITHOUT_NETWORK */
238 #endif /* not WITHOUT_NETWORK */
241 /*************************************************************************/
244 * This method sets the specified option for a socket
246 JNIEXPORT jobject JNICALL
247 Java_gnu_java_net_PlainSocketImpl_getOption (JNIEnv * env, jobject this,
248 jint option_id)
250 #ifndef WITHOUT_NETWORK
251 assert (env != NULL);
252 assert ((*env) != NULL);
254 return (_javanet_get_option (env, this, option_id));
255 #else /* not WITHOUT_NETWORK */
256 return NULL;
257 #endif /* not WITHOUT_NETWORK */
260 /*************************************************************************/
263 * Reads a buffer from a remote host
265 JNIEXPORT jint JNICALL
266 Java_gnu_java_net_PlainSocketImpl_read (JNIEnv * env, jobject this,
267 jarray buf, jint offset, jint len)
269 #ifndef WITHOUT_NETWORK
270 assert (env != NULL);
271 assert ((*env) != NULL);
273 return (_javanet_recvfrom (env, this, buf, offset, len, 0, 0));
274 #else /* not WITHOUT_NETWORK */
275 return 0;
276 #endif /* not WITHOUT_NETWORK */
279 /*************************************************************************/
282 * Writes a buffer to the remote host
284 JNIEXPORT void JNICALL
285 Java_gnu_java_net_PlainSocketImpl_write (JNIEnv * env, jobject this,
286 jarray buf, jint offset, jint len)
288 #ifndef WITHOUT_NETWORK
289 assert (env != NULL);
290 assert ((*env) != NULL);
292 _javanet_sendto (env, this, buf, offset, len, 0, 0);
293 #else /* not WITHOUT_NETWORK */
294 #endif /* not WITHOUT_NETWORK */
297 JNIEXPORT void JNICALL
298 Java_gnu_java_net_PlainSocketImpl_shutdownInput (JNIEnv * env, jobject this)
300 #ifndef WITHOUT_NETWORK
301 assert (env != NULL);
302 assert ((*env) != NULL);
304 _javanet_shutdownInput (env, this);
305 #else /* not WITHOUT_NETWORK */
306 #endif /* not WITHOUT_NETWORK */
309 JNIEXPORT void JNICALL
310 Java_gnu_java_net_PlainSocketImpl_shutdownOutput (JNIEnv * env, jobject this)
312 #ifndef WITHOUT_NETWORK
313 assert (env != NULL);
314 assert ((*env) != NULL);
316 _javanet_shutdownOutput (env, this);
317 #else /* not WITHOUT_NETWORK */
318 #endif /* not WITHOUT_NETWORK */
321 /* end of file */