Exemplo n.º 1
0
        private bool receiveTcpMessages()
        {
            bool received = false;
            IList <TcpConnectionState> tcpConnectionStatesToBeDeleted = new LinkedList <TcpConnectionState>();

            foreach (TcpConnectionState tcpConnectionState in tcpConnectionStates)
            {
                if (log.TraceEnabled)
                {
                    log.trace(string.Format("receiveTcpMessages polling {0}", tcpConnectionState));
                }

                if (tcpConnectionState.pendingConnection)
                {
                    try
                    {
                        tcpConnectionState.connect();
                        SocketChannel socketChannel = tcpConnectionState.socketChannel;
                        if (socketChannel != null && socketChannel.finishConnect())
                        {
                            tcpConnectionState.sourceSequenceNumber++;
                            // Send SYN-ACK acknowledge
                            sendAcknowledgeTCP(tcpConnectionState, true);
                            tcpConnectionState.destinationSequenceNumber++;
                            tcpConnectionState.pendingConnection = false;
                        }
                    }
                    catch (IOException e)
                    {
                        // connect failed, do not send any TCP SYN-ACK, forget the connection state
                        tcpConnectionStatesToBeDeleted.Add(tcpConnectionState);
                        //if (log.DebugEnabled)
                        {
                            Console.WriteLine(string.Format("Pending TCP connection {0} failed: {1}", tcpConnectionState, e.ToString()));
                        }
                    }
                }

                try
                {
                    if (!tcpConnectionState.pendingConnection)
                    {
                        // Write any pending data
                        sbyte[] pendingWriteData = tcpConnectionState.pendingWriteData;
                        if (pendingWriteData != null)
                        {
                            tcpConnectionState.pendingWriteData = null;

                            //if (log.DebugEnabled)
                            {
                                Console.WriteLine(string.Format("receiveTcpMessages sending pending write data: {0}", Utilities.getMemoryDump(pendingWriteData)));
                            }
                            tcpConnectionState.write(pendingWriteData);
                        }

                        // Receive any available data
                        sbyte[] receivedData = tcpConnectionState.read();
                        if (receivedData != null)
                        {
                            received = true;
                            sendTcpData(tcpConnectionState, receivedData);
                        }
                    }
                }
                catch (IOException e)
                {
                    // Ignore exceptions
                    Console.WriteLine("receiveTcpMessages", e);
                }
            }

//JAVA TO C# CONVERTER TODO TASK: There is no .NET equivalent to the java.util.Collection 'removeAll' method:
            tcpConnectionStates.removeAll(tcpConnectionStatesToBeDeleted);

            return(received);
        }