/// <exception cref="System.IO.IOException"/> internal NioInetPeer(Socket socket) { this.socket = socket; this.@in = new SocketInputStream(socket.GetChannel(), 0); this.@out = new SocketOutputStream(socket.GetChannel(), 0); this.isLocal = socket.GetInetAddress().Equals(socket.GetLocalAddress()); }
////////////////////////////////////////////////////////////////////////// #region Thread For Connect public void ConnectThread() { m_connectStatus = ConnectStatus.CONNECTING; while (true) { m_Socket.close(); Console.WriteLine("connect:" + m_strServerAddr); m_strConnectResult = m_Socket.connect(m_strServerAddr, m_nServerPort); if (m_strConnectResult.Length == 0 && m_Socket.IsValid) { m_SocketInputStream = new SocketInputStream(m_Socket); m_SocketOutputStream = new SocketOutputStream(m_Socket); m_connectStatus = ConnectStatus.CONNECTED; break; } else { LogModule.WarningLog(m_strConnectResult); } m_Socket.close(); Thread.Sleep(m_nConnectSleep); m_connectStatus = ConnectStatus.DISCONNECTED; break; } m_bConnectFinish = true; }
private DicomConnection(Socket socket, CancellationToken cancellationToken) { Socket = socket; SocketInputStream = new SocketInputStream(socket, cancellationToken); SocketOutputStream = new SocketOutputStream(socket, cancellationToken); Input = new BufferedStreamReader(SocketInputStream); Output = new BufferedStreamWriter(SocketOutputStream); }
public void Dispose() { SocketOutputStream.Dispose(); SocketInputStream.Dispose(); Socket.Dispose(); }
/// <summary>Sends a packet with up to maxChunks chunks of data.</summary> /// <param name="pkt">buffer used for writing packet data</param> /// <param name="maxChunks">maximum number of chunks to send</param> /// <param name="out">stream to send data to</param> /// <param name="transferTo">use transferTo to send data</param> /// <param name="throttler">used for throttling data transfer bandwidth</param> /// <exception cref="System.IO.IOException"/> private int SendPacket(ByteBuffer pkt, int maxChunks, OutputStream @out, bool transferTo , DataTransferThrottler throttler) { int dataLen = (int)Math.Min(endOffset - offset, (chunkSize * (long)maxChunks)); int numChunks = NumberOfChunks(dataLen); // Number of chunks be sent in the packet int checksumDataLen = numChunks * checksumSize; int packetLen = dataLen + checksumDataLen + 4; bool lastDataPacket = offset + dataLen == endOffset && dataLen > 0; // The packet buffer is organized as follows: // _______HHHHCCCCD?D?D?D? // ^ ^ // | \ checksumOff // \ headerOff // _ padding, since the header is variable-length // H = header and length prefixes // C = checksums // D? = data, if transferTo is false. int headerLen = WritePacketHeader(pkt, dataLen, packetLen); // Per above, the header doesn't start at the beginning of the // buffer int headerOff = pkt.Position() - headerLen; int checksumOff = pkt.Position(); byte[] buf = ((byte[])pkt.Array()); if (checksumSize > 0 && checksumIn != null) { ReadChecksum(buf, checksumOff, checksumDataLen); // write in progress that we need to use to get last checksum if (lastDataPacket && lastChunkChecksum != null) { int start = checksumOff + checksumDataLen - checksumSize; byte[] updatedChecksum = lastChunkChecksum.GetChecksum(); if (updatedChecksum != null) { System.Array.Copy(updatedChecksum, 0, buf, start, checksumSize); } } } int dataOff = checksumOff + checksumDataLen; if (!transferTo) { // normal transfer IOUtils.ReadFully(blockIn, buf, dataOff, dataLen); if (verifyChecksum) { VerifyChecksum(buf, dataOff, dataLen, numChunks, checksumOff); } } try { if (transferTo) { SocketOutputStream sockOut = (SocketOutputStream)@out; // First write header and checksums sockOut.Write(buf, headerOff, dataOff - headerOff); // no need to flush since we know out is not a buffered stream FileChannel fileCh = ((FileInputStream)blockIn).GetChannel(); LongWritable waitTime = new LongWritable(); LongWritable transferTime = new LongWritable(); sockOut.TransferToFully(fileCh, blockInPosition, dataLen, waitTime, transferTime); datanode.metrics.AddSendDataPacketBlockedOnNetworkNanos(waitTime.Get()); datanode.metrics.AddSendDataPacketTransferNanos(transferTime.Get()); blockInPosition += dataLen; } else { // normal transfer @out.Write(buf, headerOff, dataOff + dataLen - headerOff); } } catch (IOException e) { if (e is SocketTimeoutException) { } else { /* * writing to client timed out. This happens if the client reads * part of a block and then decides not to read the rest (but leaves * the socket open). * * Reporting of this case is done in DataXceiver#run */ /* Exception while writing to the client. Connection closure from * the other end is mostly the case and we do not care much about * it. But other things can go wrong, especially in transferTo(), * which we do not want to ignore. * * The message parsing below should not be considered as a good * coding example. NEVER do it to drive a program logic. NEVER. * It was done here because the NIO throws an IOException for EPIPE. */ string ioem = e.Message; if (!ioem.StartsWith("Broken pipe") && !ioem.StartsWith("Connection reset")) { Log.Error("BlockSender.sendChunks() exception: ", e); } datanode.GetBlockScanner().MarkSuspectBlock(volumeRef.GetVolume().GetStorageID(), block); } throw IoeToSocketException(e); } if (throttler != null) { // rebalancing so throttle throttler.Throttle(packetLen); } return(dataLen); }