Imported GNU Classpath 0.20
[official-gcc.git] / libjava / classpath / native / jni / java-net / gnu_java_net_VMPlainSocketImpl.c
blobcf10ee4b53c1377c142ca3c95caba6772ad3ef12
1 /* VMPlainSocketImpl.c - Native methods for PlainSocketImpl class
2 Copyright (C) 2005, 2006 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>
45 #include <jni.h>
46 #include <jcl.h>
48 #include "javanet.h"
50 #include "target_native.h"
51 #ifndef WITHOUT_NETWORK
52 #include "target_native_network.h"
53 #endif /* WITHOUT_NETWORK */
55 #include "gnu_java_net_VMPlainSocketImpl.h"
58 * Note that the functions in this module simply redirect to another
59 * internal function. Why? Because many of these functions are shared
60 * with PlainDatagramSocketImpl. The unshared ones were done the same
61 * way for consistency.
64 /*************************************************************************/
67 * Creates a new stream or datagram socket
69 JNIEXPORT void JNICALL
70 Java_gnu_java_net_VMPlainSocketImpl_create(JNIEnv *env,
71 jclass klass __attribute__ ((__unused__)),
72 jobject obj)
74 #ifndef WITHOUT_NETWORK
75 _javanet_create(env, obj, JNI_TRUE);
76 #else /* not WITHOUT_NETWORK */
77 #endif /* not WITHOUT_NETWORK */
80 /*************************************************************************/
83 * Close the socket. Any underlying streams will be closed by this
84 * action as well.
86 JNIEXPORT void JNICALL
87 Java_gnu_java_net_VMPlainSocketImpl_close(JNIEnv *env,
88 jclass klass __attribute__ ((__unused__)),
89 jobject obj)
91 #ifndef WITHOUT_NETWORK
92 _javanet_close(env, obj, 1);
93 #else /* not WITHOUT_NETWORK */
94 #endif /* not WITHOUT_NETWORK */
97 /*************************************************************************/
100 * Connects to the specified destination.
102 JNIEXPORT void JNICALL
103 Java_gnu_java_net_VMPlainSocketImpl_connect(JNIEnv *env,
104 jclass klass __attribute__ ((__unused__)),
105 jobject obj,
106 jobject addr, jint port)
108 #ifndef WITHOUT_NETWORK
109 _javanet_connect(env, obj, addr, port, 1);
110 #else /* not WITHOUT_NETWORK */
111 #endif /* not WITHOUT_NETWORK */
114 /*************************************************************************/
117 * This method binds the specified address to the specified local port.
118 * Note that we have to set the local address and local port public instance
119 * variables.
121 JNIEXPORT void JNICALL
122 Java_gnu_java_net_VMPlainSocketImpl_bind(JNIEnv *env,
123 jclass klass __attribute__ ((__unused__)),
124 jobject obj, jobject addr,
125 jint port)
127 #ifndef WITHOUT_NETWORK
128 _javanet_bind(env, obj, addr, port, 1);
129 #else /* not WITHOUT_NETWORK */
130 #endif /* not WITHOUT_NETWORK */
133 /*************************************************************************/
136 * Starts listening on a socket with the specified number of pending
137 * connections allowed.
139 JNIEXPORT void JNICALL
140 Java_gnu_java_net_VMPlainSocketImpl_listen(JNIEnv *env,
141 jclass klass __attribute__ ((__unused__)),
142 jobject obj, jint queuelen)
144 #ifndef WITHOUT_NETWORK
145 _javanet_listen(env, obj, queuelen);
146 #else /* not WITHOUT_NETWORK */
147 #endif /* not WITHOUT_NETWORK */
150 /*************************************************************************/
153 * Accepts a new connection and assigns it to the passed in SocketImpl
154 * object. Note that we assume this is a PlainSocketImpl just like us.
156 JNIEXPORT void JNICALL
157 Java_gnu_java_net_VMPlainSocketImpl_accept(JNIEnv *env,
158 jclass klass __attribute__ ((__unused__)),
159 jobject obj, jobject impl)
161 #ifndef WITHOUT_NETWORK
162 _javanet_accept(env, obj, impl);
163 #else /* not WITHOUT_NETWORK */
164 #endif /* not WITHOUT_NETWORK */
167 /*************************************************************************/
169 JNIEXPORT jint JNICALL
170 Java_gnu_java_net_VMPlainSocketImpl_available(JNIEnv *env,
171 jclass klass __attribute__ ((__unused__)),
172 jobject obj)
174 #ifndef WITHOUT_NETWORK
175 jclass cls;
176 jfieldID fid;
177 int fd;
178 int bytesAvailable;
179 int result;
181 cls = (*env)->GetObjectClass(env, obj);
182 if (cls == 0)
184 JCL_ThrowException(env, IO_EXCEPTION, "internal error");
185 return 0;
188 fid = (*env)->GetFieldID(env, cls, "native_fd", "I");
189 if (fid == 0)
191 JCL_ThrowException(env, IO_EXCEPTION, "internal error");
192 return 0;
195 fd = (*env)->GetIntField(env, obj, fid);
197 TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE(fd,bytesAvailable,result);
198 if (result != TARGET_NATIVE_OK)
200 JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING());
201 return 0;
204 return bytesAvailable;
205 #else /* not WITHOUT_NETWORK */
206 return 0;
207 #endif /* not WITHOUT_NETWORK */
210 /*************************************************************************/
213 * This method sets the specified option for a socket
215 JNIEXPORT void JNICALL
216 Java_gnu_java_net_VMPlainSocketImpl_setOption(JNIEnv *env,
217 jclass klass __attribute__ ((__unused__)),
218 jobject obj,
219 jint option_id, jobject val)
221 #ifndef WITHOUT_NETWORK
222 _javanet_set_option(env, obj, option_id, val);
223 #else /* not WITHOUT_NETWORK */
224 #endif /* not WITHOUT_NETWORK */
227 /*************************************************************************/
230 * This method gets the specified option for a socket
232 JNIEXPORT jobject JNICALL
233 Java_gnu_java_net_VMPlainSocketImpl_getOption(JNIEnv *env,
234 jclass klass __attribute__ ((__unused__)),
235 jobject obj,
236 jint option_id)
238 #ifndef WITHOUT_NETWORK
239 return(_javanet_get_option(env, obj, option_id));
240 #else /* not WITHOUT_NETWORK */
241 return NULL;
242 #endif /* not WITHOUT_NETWORK */
245 /*************************************************************************/
248 * Reads a buffer from a remote host
250 JNIEXPORT jint JNICALL
251 Java_gnu_java_net_VMPlainSocketImpl_read(JNIEnv *env,
252 jclass klass __attribute__ ((__unused__)),
253 jobject obj, jarray buf,
254 jint offset, jint len)
256 #ifndef WITHOUT_NETWORK
257 return(_javanet_recvfrom(env, obj, buf, offset, len, 0, 0));
258 #else /* not WITHOUT_NETWORK */
259 return 0;
260 #endif /* not WITHOUT_NETWORK */
263 /*************************************************************************/
266 * Writes a buffer to the remote host
268 JNIEXPORT void JNICALL
269 Java_gnu_java_net_VMPlainSocketImpl_write(JNIEnv *env,
270 jclass klass __attribute__ ((__unused__)),
271 jobject obj, jarray buf,
272 jint offset, jint len)
274 #ifndef WITHOUT_NETWORK
275 _javanet_sendto(env, obj, buf, offset, len, 0, 0);
276 #else /* not WITHOUT_NETWORK */
277 #endif /* not WITHOUT_NETWORK */
280 JNIEXPORT void JNICALL
281 Java_gnu_java_net_VMPlainSocketImpl_shutdownInput (JNIEnv * env,
282 jclass klass __attribute__ ((__unused__)),
283 jobject this)
285 #ifndef WITHOUT_NETWORK
286 _javanet_shutdownInput (env, this);
287 #else /* not WITHOUT_NETWORK */
288 #endif /* not WITHOUT_NETWORK */
291 JNIEXPORT void JNICALL
292 Java_gnu_java_net_VMPlainSocketImpl_shutdownOutput (JNIEnv * env,
293 jclass klass __attribute__ ((__unused__)),
294 jobject this)
296 #ifndef WITHOUT_NETWORK
297 _javanet_shutdownOutput (env, this);
298 #else /* not WITHOUT_NETWORK */
299 #endif /* not WITHOUT_NETWORK */
302 /* end of file */