1 /* 2 * Copyright 2009, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Michael Lotz <mmlr@mlotz.ch> 7 */ 8 9 #include "NetSender.h" 10 11 #include "StreamingRingBuffer.h" 12 13 #include <NetEndpoint.h> 14 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 19 #define TRACE(x...) /*debug_printf("NetSender: " x)*/ 20 #define TRACE_ERROR(x...) debug_printf("NetSender: " x) 21 22 23 NetSender::NetSender(BNetEndpoint *endpoint, StreamingRingBuffer *source) 24 : 25 fEndpoint(endpoint), 26 fSource(source), 27 fSenderThread(-1), 28 fStopThread(false) 29 { 30 fSenderThread = spawn_thread(_NetworkSenderEntry, "network sender", 31 B_NORMAL_PRIORITY, this); 32 resume_thread(fSenderThread); 33 } 34 35 36 NetSender::~NetSender() 37 { 38 fStopThread = true; 39 40 suspend_thread(fSenderThread); 41 resume_thread(fSenderThread); 42 } 43 44 45 int32 46 NetSender::_NetworkSenderEntry(void *data) 47 { 48 return ((NetSender *)data)->_NetworkSender(); 49 } 50 51 52 status_t 53 NetSender::_NetworkSender() 54 { 55 while (!fStopThread) { 56 uint8 buffer[4096]; 57 int32 readSize = fSource->Read(buffer, sizeof(buffer), true); 58 if (readSize < 0) { 59 TRACE_ERROR("read failed, stopping sender thread: %s\n", 60 strerror(readSize)); 61 return readSize; 62 } 63 64 while (readSize > 0) { 65 int32 sendSize = fEndpoint->Send(buffer, readSize); 66 if (sendSize < 0) { 67 TRACE_ERROR("sending data failed: %s\n", strerror(sendSize)); 68 return sendSize; 69 } 70 71 readSize -= sendSize; 72 } 73 } 74 75 return B_OK; 76 } 77