[api翻译]AsynchronousSocketChannel

最近学习aio,遇到的类的api都翻译一下吧,先打一局魔兽再翻译。。。


An asynchronous channel for stream-oriented connecting sockets.
面向流连接套接字的异步管道

Asynchronous socket channels are created in one of two ways. A newly-created AsynchronousSocketChannel is created by invoking one of the open methods defined by this class. A newly-created channel is open but not yet connected. A connected AsynchronousSocketChannel is created when a connection is made to the socket of anAsynchronousServerSocketChannel. It is not possible to create an asynchronous socket channel for an arbitrary, pre-existing socket.

异步套接字管道有两种创建方式。

调用这个类中的open方法创建一个新的AsynchronousSocketChannel。

刚创建的管道是打开这的,但是还没有连接。

当有一个连接连接到AsynchronousServerSocketChannel套接字上时,会创建一个连接状态的AsynchronousSocketChannel。

不能在已经存在的任意的socket上创建异步套接管道。


A newly-created channel is connected by invoking its connect method; once connected, a channel remains connected until it is closed. Whether or not a socket channel is connected may be determined by invoking its getRemoteAddress method. An attempt to invoke an I/O operation upon an unconnected channel will cause aNotYetConnectedException to be thrown.

新创建的管道在调用它的connect方法时建立连接;一旦建立了连接,管道会维持连接状态直到关闭。

查看一个套接字管道是否连接上了,可以调用他的getRemoteAddres 方法。

在未连接的管道上调用io操作会抛出一个NotYetConnectedException


Channels of this type are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one read operation and one write operation can be outstanding at any time. If a thread initiates a read operation before a previous read operation has completed then a ReadPendingException will be thrown. Similarly, an attempt to initiate a write operation before a previous write has completed will throw a WritePendingException.

Socket options are configured using the setOption method. Asynchronous socket channels support the following options:

这个类型的管道在多线程下是安全的。

他们支持并发读写,索然任何时候其实至多只有一个读操作盒一个写操作在执行。

如果如果一个线程在前一个读操作完成前,发起读操作,就会抛出一个ReadPendingException。

相似的,一个线程在前一个写操作完成前,发起写操作,会抛出WritePendingException。

使用setOption方法配置套接字,异步套接字管道支持如下选项:

Option NameDescription
SO_SNDBUFThe size of the socket send buffer发送缓冲区大小
SO_RCVBUFThe size of the socket receive buffer接受缓冲区大小
SO_KEEPALIVEKeep connection alive保持连接
SO_REUSEADDRRe-use address 地址复用
TCP_NODELAYDisable the Nagle algorithm禁用nagle算法
Additional (implementation specific) options may also be supported.
额外的具体实现细节选项也被支持
Timeouts 超时

The read and write methods defined by this class allow a timeout to be specified when initiating a read or write operation. If the timeout elapses before an operation completes then the operation completes with the exception InterruptedByTimeoutException. A timeout may leave the channel,o r the underlying connection, in an inconsistent state. Where the implementation cannot guarantee that bytes have not been read from the channel then it puts the channel into an implementation specific error state. A subsequent attempt to initiate a read operation causes an unspecified runtime exception to be thrown. Similarly if a write operation times out and the implementation cannot guarantee bytes have not been written to the channel then further attempts to write to the channel cause an unspecified runtime exception to be thrown. When a timeout elapses then the state of the ByteBuffer, or the sequence of buffers, for the I/O operation is not defined. Buffers should be discarded or at least care must be taken to ensure that the buffers are not accessed while the channel remains open. All methods that accept timeout parameters treat values less than or equal to zero to mean that the I/O operation does not timeout.

这个类的读写方法允许在初始化读写操作时设置超时时间。

如果一个操作超时就会以抛出InterruptedByTimeoutException结束。

超时可能在管道和底层连接之外,以一种单独的持久化的状态存在。(不懂。。。。蒙的)

在实现无法保证 从管道中读出bytes的地方,它会设置管道为实现细节错误状态。

开始一个读操作的请求会导致一个未知的运行时异常被抛出。

同样的如果写操作超时,并且实现不能保证字节没有被写到管道中,将来的写操作就会导致抛出未指定的超时异常。

当超时后,ByteBuffer的状态,或者是缓冲区的顺序对于io操作来说是不确定的。

缓冲区里的数据应该被丢弃,至少应该保证在管道任然开启的状态下这些缓存数据不可以再被访问。

所有接受超时时间做为参数的方法,当参数小于等于0时意味着io操作没有超时。



Since:

1.7

(下面的内容我用到哪个就先翻译哪个了哈)


public final <A> void write(ByteBuffer src,
             A attachment,
             CompletionHandler<Integer,? super A> handler)
Description copied from interface: AsynchronousByteChannel

Writes a sequence of bytes to this channel from the given buffer.
从给定缓冲中向管道中写入有序的字节。

This method initiates an asynchronous write operation to write a sequence of bytes to this channel from the given buffer. The handler parameter is a completion handler that is invoked when the write operation completes (or fails). The result passed to the completion handler is the number of bytes written.

这个方法开启了一个从给定的缓冲区向管道中异步写的操作,写入的内容为有序的字节。

handler属性是当操作完成/失败时被回掉的处理器。

传递给控制器的处理结果是写入的字节长度。


The write operation may write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, src.remaining() at the time that the write is attempted. Where r is 0, the write operation completes immediately with a result of 0 without initiating an I/O operation.

写操作月会写入r字节到管道,r是缓冲区剩余的字节数目,也是在调用写操作时的src.remaining()值。

当r=0,写操作立刻完成,结果是0,并且不会调用io操作。


TODO 先上班去。。。

Suppose that a byte sequence of length n is written, where 0 < n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer's position at the moment the write is performed; the index of the last byte written will be p + n - 1. Upon completion the buffer's position will be equal to p + n; its limit will not have changed.

Buffers are not safe for use by multiple concurrent threads so care should be taken to not access the buffer until the operation has completed.

This method may be invoked at any time. Some channel types may not allow more than one write to be outstanding at any given time. If a thread initiates a write operation before a previous write operation has completed then a WritePendingException will be thrown.

Specified by:
write in interface  AsynchronousByteChannel
Parameters:
src - The buffer from which bytes are to be retrieved
attachment - The object to attach to the I/O operation; can be  null
handler - The completion handler object
Throws:
WritePendingException - If the channel does not allow more than one write to be outstanding and a previous write has not completed
NotYetConnectedException - If this channel is not yet connected
ShutdownChannelGroupException - If the channel group has terminated

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值