KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > jni > Socket


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.jni;
19
20 /* Import needed classes */
21 import java.nio.ByteBuffer JavaDoc;
22
23 /** Socket
24  *
25  * @author Mladen Turk
26  * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
27  */

28
29 public class Socket {
30
31     /* Standard socket defines */
32     public static final int SOCK_STREAM = 0;
33     public static final int SOCK_DGRAM = 1;
34     /*
35      * apr_sockopt Socket option definitions
36      */

37     public static final int APR_SO_LINGER = 1; /** Linger */
38     public static final int APR_SO_KEEPALIVE = 2; /** Keepalive */
39     public static final int APR_SO_DEBUG = 4; /** Debug */
40     public static final int APR_SO_NONBLOCK = 8; /** Non-blocking IO */
41     public static final int APR_SO_REUSEADDR = 16; /** Reuse addresses */
42     public static final int APR_SO_SNDBUF = 64; /** Send buffer */
43     public static final int APR_SO_RCVBUF = 128; /** Receive buffer */
44     public static final int APR_SO_DISCONNECTED = 256; /** Disconnected */
45     /** For SCTP sockets, this is mapped to STCP_NODELAY internally. */
46     public static final int APR_TCP_NODELAY = 512;
47     public static final int APR_TCP_NOPUSH = 1024; /** No push */
48     /** This flag is ONLY set internally when we set APR_TCP_NOPUSH with
49      * APR_TCP_NODELAY set to tell us that APR_TCP_NODELAY should be turned on
50      * again when NOPUSH is turned off
51      */

52     public static final int APR_RESET_NODELAY = 2048;
53     /** Set on non-blocking sockets (timeout != 0) on which the
54      * previous read() did not fill a buffer completely. the next
55      * apr_socket_recv() will first call select()/poll() rather than
56      * going straight into read(). (Can also be set by an application to
57      * force a select()/poll() call before the next read, in cases where
58      * the app expects that an immediate read would fail.)
59      */

60     public static final int APR_INCOMPLETE_READ = 4096;
61     /** like APR_INCOMPLETE_READ, but for write
62      */

63     public static final int APR_INCOMPLETE_WRITE = 8192;
64     /** Don't accept IPv4 connections on an IPv6 listening socket.
65      */

66     public static final int APR_IPV6_V6ONLY = 16384;
67     /** Delay accepting of new connections until data is available.
68      */

69     public static final int APR_TCP_DEFER_ACCEPT = 32768;
70
71     /** Define what type of socket shutdown should occur.
72      * apr_shutdown_how_e enum
73      */

74     public static final int APR_SHUTDOWN_READ = 0; /** no longer allow read request */
75     public static final int APR_SHUTDOWN_WRITE = 1; /** no longer allow write requests */
76     public static final int APR_SHUTDOWN_READWRITE = 2; /** no longer allow read or write requests */
77
78     public static final int APR_IPV4_ADDR_OK = 0x01;
79     public static final int APR_IPV6_ADDR_OK = 0x02;
80
81     /* TODO: Missing:
82      * APR_INET
83      * APR_UNSPEC
84      * APR_INET6
85      */

86     public static final int APR_UNSPEC = 0;
87     public static final int APR_INET = 1;
88     public static final int APR_INET6 = 2;
89
90     public static final int APR_PROTO_TCP = 6; /** TCP */
91     public static final int APR_PROTO_UDP = 17; /** UDP */
92     public static final int APR_PROTO_SCTP = 132; /** SCTP */
93
94     /**
95      * Enum to tell us if we're interested in remote or local socket
96      * apr_interface_e
97      */

98     public static final int APR_LOCAL = 0;
99     public static final int APR_REMOTE = 1;
100
101     /* Socket.get types */
102     public static final int SOCKET_GET_POOL = 0;
103     public static final int SOCKET_GET_IMPL = 1;
104     public static final int SOCKET_GET_APRS = 2;
105     public static final int SOCKET_GET_TYPE = 3;
106
107     /**
108      * Create a socket.
109      * @param family The address family of the socket (e.g., APR_INET).
110      * @param type The type of the socket (e.g., SOCK_STREAM).
111      * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
112      * @param cont The parent pool to use
113      * @return The new socket that has been set up.
114      */

115     public static native long create(int family, int type,
116                                      int protocol, long cont)
117         throws Exception JavaDoc;
118
119
120     /**
121      * Shutdown either reading, writing, or both sides of a socket.
122      * <br />
123      * This does not actually close the socket descriptor, it just
124      * controls which calls are still valid on the socket.
125      * @param thesocket The socket to close
126      * @param how How to shutdown the socket. One of:
127      * <PRE>
128      * APR_SHUTDOWN_READ no longer allow read requests
129      * APR_SHUTDOWN_WRITE no longer allow write requests
130      * APR_SHUTDOWN_READWRITE no longer allow read or write requests
131      * </PRE>
132      */

133     public static native int shutdown(long thesocket, int how);
134
135     /**
136      * Close a socket.
137      * @param thesocket The socket to close
138      */

139     public static native int close(long thesocket);
140
141     /**
142      * Destroy a pool associated with socket
143      * @param thesocket The destroy
144      */

145     public static native void destroy(long thesocket);
146
147     /**
148      * Bind the socket to its associated port
149      * @param sock The socket to bind
150      * @param sa The socket address to bind to
151      * This may be where we will find out if there is any other process
152      * using the selected port.
153      */

154     public static native int bind(long sock, long sa);
155
156     /**
157      * Listen to a bound socket for connections.
158      * @param sock The socket to listen on
159      * @param backlog The number of outstanding connections allowed in the sockets
160      * listen queue. If this value is less than zero, the listen
161      * queue size is set to zero.
162      */

163     public static native int listen(long sock, int backlog);
164
165     /**
166      * Accept a new connection request
167      * @param sock The socket we are listening on.
168      * @param pool The pool for the new socket.
169      * @return A copy of the socket that is connected to the socket that
170      * made the connection request. This is the socket which should
171      * be used for all future communication.
172      */

173     public static native long accept(long sock)
174         throws Exception JavaDoc;
175
176     /**
177      * Set an OS level accept filter.
178      * @param sock The socket to put the accept filter on.
179      * @param name The accept filter
180      * @param args Any extra args to the accept filter. Passing NULL here removes
181      * the accept filter.
182      */

183     public static native int acceptfilter(long sock, String JavaDoc name, String JavaDoc args);
184
185     /**
186      * Query the specified socket if at the OOB/Urgent data mark
187      * @param sock The socket to query
188      * @return True if socket is at the OOB/urgent mark,
189      * otherwise return false.
190      */

191     public static native boolean atmark(long sock);
192
193     /**
194      * Issue a connection request to a socket either on the same machine
195      * or a different one.
196      * @param sock The socket we wish to use for our side of the connection
197      * @param sa The address of the machine we wish to connect to.
198      */

199     public static native int connect(long sock, long sa);
200
201     /**
202      * Send data over a network.
203      * <PRE>
204      * This functions acts like a blocking write by default. To change
205      * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
206      * socket option.
207      *
208      * It is possible for both bytes to be sent and an error to be returned.
209      *
210      * APR_EINTR is never returned.
211      * </PRE>
212      * @param sock The socket to send the data over.
213      * @param buf The buffer which contains the data to be sent.
214      * @param offset Offset in the byte buffer.
215      * @param len The number of bytes to write; (-1) for full array.
216      * @return The number of bytes send.
217      *
218      */

219     public static native int send(long sock, byte[] buf, int offset, int len);
220
221     /**
222      * Send data over a network.
223      * <PRE>
224      * This functions acts like a blocking write by default. To change
225      * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
226      * socket option.
227      *
228      * It is possible for both bytes to be sent and an error to be returned.
229      *
230      * APR_EINTR is never returned.
231      * </PRE>
232      * @param sock The socket to send the data over.
233      * @param buf The Byte buffer which contains the data to be sent.
234      * @param offset The offset within the buffer array of the first buffer from
235      * which bytes are to be retrieved; must be non-negative
236      * and no larger than buf.length
237      * @param len The maximum number of buffers to be accessed; must be non-negative
238      * and no larger than buf.length - offset
239      * @return The number of bytes send.
240      *
241      */

242     public static native int sendb(long sock, ByteBuffer JavaDoc buf,
243                                    int offset, int len);
244     /**
245      * Send data over a network using internally set ByteBuffer
246      */

247     public static native int sendbb(long sock,
248                                    int offset, int len);
249
250     /**
251      * Send multiple packets of data over a network.
252      * <PRE>
253      * This functions acts like a blocking write by default. To change
254      * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
255      * socket option.
256      * The number of bytes actually sent is stored in argument 3.
257      *
258      * It is possible for both bytes to be sent and an error to be returned.
259      *
260      * APR_EINTR is never returned.
261      * </PRE>
262      * @param sock The socket to send the data over.
263      * @param vec The array from which to get the data to send.
264      *
265      */

266     public static native int sendv(long sock, byte[][] vec);
267
268     /**
269      * @param sock The socket to send from
270      * @param where The apr_sockaddr_t describing where to send the data
271      * @param flags The flags to use
272      * @param buf The data to send
273      * @param offset Offset in the byte buffer.
274      * @param len The length of the data to send
275      */

276     public static native int sendto(long sock, long where, int flags,
277                                     byte[] buf, int offset, int len);
278
279     /**
280      * Read data from a network.
281      *
282      * <PRE>
283      * This functions acts like a blocking read by default. To change
284      * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
285      * socket option.
286      * The number of bytes actually received is stored in argument 3.
287      *
288      * It is possible for both bytes to be received and an APR_EOF or
289      * other error to be returned.
290      *
291      * APR_EINTR is never returned.
292      * </PRE>
293      * @param sock The socket to read the data from.
294      * @param buf The buffer to store the data in.
295      * @param offset Offset in the byte buffer.
296      * @param nbytes The number of bytes to read (-1) for full array.
297      * @return the number of bytes received.
298      */

299     public static native int recv(long sock, byte[] buf, int offset, int nbytes);
300
301     /**
302      * Read data from a network with timeout.
303      *
304      * <PRE>
305      * This functions acts like a blocking read by default. To change
306      * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
307      * socket option.
308      * The number of bytes actually received is stored in argument 3.
309      *
310      * It is possible for both bytes to be received and an APR_EOF or
311      * other error to be returned.
312      *
313      * APR_EINTR is never returned.
314      * </PRE>
315      * @param sock The socket to read the data from.
316      * @param buf The buffer to store the data in.
317      * @param offset Offset in the byte buffer.
318      * @param nbytes The number of bytes to read (-1) for full array.
319      * @param timeout The socket timeout in microseconds.
320      * @return the number of bytes received.
321      */

322     public static native int recvt(long sock, byte[] buf, int offset,
323                                    int nbytes, long timeout);
324
325     /**
326      * Read data from a network.
327      *
328      * <PRE>
329      * This functions acts like a blocking read by default. To change
330      * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
331      * socket option.
332      * The number of bytes actually received is stored in argument 3.
333      *
334      * It is possible for both bytes to be received and an APR_EOF or
335      * other error to be returned.
336      *
337      * APR_EINTR is never returned.
338      * </PRE>
339      * @param sock The socket to read the data from.
340      * @param buf The buffer to store the data in.
341      * @param offset Offset in the byte buffer.
342      * @param nbytes The number of bytes to read (-1) for full array.
343      * @return the number of bytes received.
344      */

345     public static native int recvb(long sock, ByteBuffer JavaDoc buf,
346                                    int offset, int nbytes);
347     /**
348      * Read data from a network using internally set ByteBuffer
349      */

350     public static native int recvbb(long sock,
351                                     int offset, int nbytes);
352     /**
353      * Read data from a network with timeout.
354      *
355      * <PRE>
356      * This functions acts like a blocking read by default. To change
357      * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
358      * socket option.
359      * The number of bytes actually received is stored in argument 3.
360      *
361      * It is possible for both bytes to be received and an APR_EOF or
362      * other error to be returned.
363      *
364      * APR_EINTR is never returned.
365      * </PRE>
366      * @param sock The socket to read the data from.
367      * @param buf The buffer to store the data in.
368      * @param offset Offset in the byte buffer.
369      * @param nbytes The number of bytes to read (-1) for full array.
370      * @param timeout The socket timeout in microseconds.
371      * @return the number of bytes received.
372      */

373     public static native int recvbt(long sock, ByteBuffer JavaDoc buf,
374                                     int offset, int nbytes, long timeout);
375     /**
376      * Read data from a network with timeout using internally set ByteBuffer
377      */

378     public static native int recvbbt(long sock,
379                                      int offset, int nbytes, long timeout);
380
381     /**
382      * @param from The apr_sockaddr_t to fill in the recipient info
383      * @param sock The socket to use
384      * @param flags The flags to use
385      * @param buf The buffer to use
386      * @param offset Offset in the byte buffer.
387      * @param nbytes The number of bytes to read (-1) for full array.
388      * @return the number of bytes received.
389      */

390     public static native int recvFrom(long from, long sock, int flags,
391                                       byte[] buf, int offset, int nbytes);
392
393     /**
394      * Setup socket options for the specified socket
395      * @param sock The socket to set up.
396      * @param opt The option we would like to configure. One of:
397      * <PRE>
398      * APR_SO_DEBUG -- turn on debugging information
399      * APR_SO_KEEPALIVE -- keep connections active
400      * APR_SO_LINGER -- lingers on close if data is present
401      * APR_SO_NONBLOCK -- Turns blocking on/off for socket
402      * When this option is enabled, use
403      * the APR_STATUS_IS_EAGAIN() macro to
404      * see if a send or receive function
405      * could not transfer data without
406      * blocking.
407      * APR_SO_REUSEADDR -- The rules used in validating addresses
408      * supplied to bind should allow reuse
409      * of local addresses.
410      * APR_SO_SNDBUF -- Set the SendBufferSize
411      * APR_SO_RCVBUF -- Set the ReceiveBufferSize
412      * </PRE>
413      * @param on Value for the option.
414      */

415     public static native int optSet(long sock, int opt, int on);
416
417     /**
418      * Query socket options for the specified socket
419      * @param sock The socket to query
420      * @param opt The option we would like to query. One of:
421      * <PRE>
422      * APR_SO_DEBUG -- turn on debugging information
423      * APR_SO_KEEPALIVE -- keep connections active
424      * APR_SO_LINGER -- lingers on close if data is present
425      * APR_SO_NONBLOCK -- Turns blocking on/off for socket
426      * APR_SO_REUSEADDR -- The rules used in validating addresses
427      * supplied to bind should allow reuse
428      * of local addresses.
429      * APR_SO_SNDBUF -- Set the SendBufferSize
430      * APR_SO_RCVBUF -- Set the ReceiveBufferSize
431      * APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
432      * (Currently only used on Windows)
433      * </PRE>
434      * @return Socket option returned on the call.
435      */

436     public static native int optGet(long sock, int opt)
437         throws Exception JavaDoc;
438
439     /**
440      * Setup socket timeout for the specified socket
441      * @param sock The socket to set up.
442      * @param t Value for the timeout in microseconds.
443      * <PRE>
444      * t > 0 -- read and write calls return APR_TIMEUP if specified time
445      * elapsess with no data read or written
446      * t == 0 -- read and write calls never block
447      * t < 0 -- read and write calls block
448      * </PRE>
449      */

450     public static native int timeoutSet(long sock, long t);
451
452     /**
453      * Query socket timeout for the specified socket
454      * @param sock The socket to query
455      * @return Socket timeout returned from the query.
456      */

457     public static native long timeoutGet(long sock)
458         throws Exception JavaDoc;
459
460     /**
461      * Send a file from an open file descriptor to a socket, along with
462      * optional headers and trailers.
463      * <br />
464      * This functions acts like a blocking write by default. To change
465      * this behavior, use apr_socket_timeout_set() or the
466      * APR_SO_NONBLOCK socket option.
467      * The number of bytes actually sent is stored in the len parameter.
468      * The offset parameter is passed by reference for no reason; its
469      * value will never be modified by the apr_socket_sendfile() function.
470      * @param sock The socket to which we're writing
471      * @param file The open file from which to read
472      * @param headers Array containing the headers to send
473      * @param trailers Array containing the trailers to send
474      * @param offset Offset into the file where we should begin writing
475      * @param len Number of bytes to send from the file
476      * @param flags APR flags that are mapped to OS specific flags
477      * @return Number of bytes actually sent, including headers,
478      * file, and trailers
479      *
480      */

481     public static native long sendfile(long sock, long file, byte [][] headers,
482                                        byte[][] trailers, long offset,
483                                        long len, int flags);
484
485     /**
486      * Send a file without header and trailer arrays.
487      */

488     public static native long sendfilen(long sock, long file, long offset,
489                                         long len, int flags);
490
491     /**
492      * Create a child pool from associated socket pool.
493      * @param thesocket The socket to use
494      */

495     public static native long pool(long thesocket)
496         throws Exception JavaDoc;
497
498     /**
499      * Private method for geting the socket struct members
500      * @param socket The soocket to use
501      * @param what Struct member to obtain
502      * <PRE>
503      * SOCKET_GET_POOL - The socket pool
504      * SOCKET_GET_IMPL - The socket implementation object
505      * SOCKET_GET_APRS - APR socket
506      * SOCKET_GET_TYPE - Socket type
507      * </PRE>
508      * @return The stucture member address
509      */

510     private static native long get(long socket, int what);
511
512     /**
513      * Set internal send ByteBuffer.
514      * This function will preset internal Java ByteBuffer for
515      * consecutive sendbb calls.
516      * @param thesocket The socket to use
517      * @param buf The ByteBuffer
518      */

519     public static native void setsbb(long sock, ByteBuffer JavaDoc buf);
520
521     /**
522      * Set internal receive ByteBuffer.
523      * This function will preset internal Java ByteBuffer for
524      * consecutive revcvbb/recvbbt calls.
525      * @param thesocket The socket to use
526      * @param buf The ByteBuffer
527      */

528     public static native void setrbb(long sock, ByteBuffer JavaDoc buf);
529 }
530
Popular Tags