+ LEAVE('i', len);
+ return len;
+ }
+
+ // Wait
+ VFS_SelectNode(Node, VFS_SELECT_READ|VFS_SELECT_ERROR, NULL, "TCP_Client_Read");
+
+ // Lock list and read as much as possible (up to `Length`)
+ Mutex_Acquire( &conn->lRecievedPackets );
+ len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
+
+ if( len == 0 || conn->RecievedBuffer->Length == 0 ) {
+ LOG("Marking as none avaliable (len = %i)", len);
+ VFS_MarkAvaliable(Node, 0);
+ }
+
+ // Release the lock (we don't need it any more)
+ Mutex_Release( &conn->lRecievedPackets );
+
+ LEAVE('i', len);
+ return len;
+}
+
+/**
+ * \brief Send a data packet on a connection
+ */
+void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, void *Data)
+{
+ char buf[sizeof(tTCPHeader)+Length];
+ tTCPHeader *packet = (void*)buf;
+
+ packet->SourcePort = htons(Connection->LocalPort);
+ packet->DestPort = htons(Connection->RemotePort);
+ packet->DataOffset = (sizeof(tTCPHeader)/4)*16;
+ packet->WindowSize = htons(TCP_WINDOW_SIZE);
+
+ packet->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
+ packet->SequenceNumber = htonl(Connection->NextSequenceSend);
+ packet->Flags = TCP_FLAG_PSH|TCP_FLAG_ACK; // Hey, ACK if you can!
+
+ memcpy(packet->Options, Data, Length);
+
+ Log_Debug("TCP", "Send sequence 0x%08x", Connection->NextSequenceSend);
+#if HEXDUMP_OUTGOING
+ Debug_HexDump("TCP_INT_SendDataPacket: Data = ", Data, Length);
+#endif
+
+ TCP_SendPacket( Connection, sizeof(tTCPHeader)+Length, packet );
+
+ Connection->NextSequenceSend += Length;
+}
+
+/**
+ * \brief Send some bytes on a connection
+ */
+Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
+{
+ tTCPConnection *conn = Node->ImplPtr;
+ size_t rem = Length;
+
+ ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
+
+// #if DEBUG
+// Debug_HexDump("TCP_Client_Write: Buffer = ",
+// Buffer, Length);
+// #endif
+
+ // Check if connection is open
+ while( conn->State == TCP_ST_SYN_RCVD || conn->State == TCP_ST_SYN_SENT )
+ Threads_Yield();
+
+ if( conn->State != TCP_ST_OPEN ) {
+ VFS_MarkError(Node, 1);
+ LEAVE('i', -1);
+ return -1;
+ }
+
+ do
+ {
+ int len = (rem < TCP_MAX_PACKET_SIZE) ? rem : TCP_MAX_PACKET_SIZE;
+
+ #if 0
+ // Wait for space in the buffer
+ Semaphore_Signal( &Connection->SentBufferSpace, len );