本文整理汇总了C++中NPT_InputStreamReference::Read方法的典型用法代码示例。如果您正苦于以下问题:C++ NPT_InputStreamReference::Read方法的具体用法?C++ NPT_InputStreamReference::Read怎么用?C++ NPT_InputStreamReference::Read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPT_InputStreamReference
的用法示例。
在下文中一共展示了NPT_InputStreamReference::Read方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: buffer
/*----------------------------------------------------------------------
| ShowResponse
+---------------------------------------------------------------------*/
static void
ShowResponse(NPT_HttpResponse* response)
{
bool check_available = true;//true;
// show entity
NPT_HttpEntity* entity = response->GetEntity();
if (entity == NULL) return;
NPT_Console::OutputF("ENTITY: length=%lld, type=%s, encoding=%s\n",
entity->GetContentLength(),
entity->GetContentType().GetChars(),
entity->GetContentEncoding().GetChars());
NPT_DataBuffer buffer(65536);
NPT_Result result;
NPT_InputStreamReference input;
entity->GetInputStream(input);
NPT_TimeStamp start;
NPT_System::GetCurrentTimeStamp(start);
float total_read = 0.0f;
for (;;) {
NPT_Size bytes_read = 0;
NPT_LargeSize available = 0;
NPT_Size to_read = 65536;
if (check_available) {
input->GetAvailable(available);
if ((NPT_Size)available < to_read) to_read = (NPT_Size)available;
if (to_read == 0) {
to_read = 1;
NPT_TimeStamp sleep_time(0.01f);
NPT_System::Sleep(sleep_time);
}
}
result = input->Read(buffer.UseData(), to_read, &bytes_read);
if (NPT_FAILED(result)) break;
total_read += bytes_read;
NPT_TimeStamp now;
NPT_System::GetCurrentTimeStamp(now);
NPT_TimeStamp duration = now-start;
NPT_Console::OutputF("%6d avail, read %6d bytes, %6.3f KB/s\n", (int)available, bytes_read, (float)((total_read/1024.0)/(double)duration));
}
}
示例2: bytes
/*----------------------------------------------------------------------
| DumpBody
+---------------------------------------------------------------------*/
static NPT_Result
ReadBody(PLT_Downloader& downloader, NPT_InputStreamReference& stream, NPT_Size& size)
{
NPT_LargeSize avail;
char buffer[2048];
NPT_Result ret = NPT_ERROR_WOULD_BLOCK;
/* reset output param first */
size = 0;
/*
we test for availability first to avoid
getting stuck in Read forever in case blocking is true
and the download is done writing to the stream
*/
NPT_CHECK(stream->GetAvailable(avail));
if (avail) {
ret = stream->Read(buffer, 2048, &size);
NPT_LOG_FINER_2("Read %d bytes (result = %d)\n", size, ret);
return ret;
} else {
Plt_DowloaderState state = downloader.GetState();
switch (state) {
case PLT_DOWNLOADER_ERROR:
return NPT_FAILURE;
case PLT_DOWNLOADER_SUCCESS:
/* no more data expected */
return NPT_ERROR_EOS;
default:
NPT_System::Sleep(NPT_TimeInterval(0, 10000));
break;
}
}
return NPT_SUCCESS;
}
示例3: Bind
/*----------------------------------------------------------------------
| TcpServerLoop
+---------------------------------------------------------------------*/
static void
TcpServerLoop(int port)
{
NPT_TcpServerSocket listener;
NPT_Result result = listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, port));
if (NPT_FAILED(result)) {
NPT_Debug("ERROR: Bind() failed (%d)\n", result);
}
NPT_Socket* client;
for (;;) {
NPT_Debug("waiting for client on port %d\n", port);
NPT_Result result = listener.WaitForNewClient(client);
NPT_SocketInfo socket_info;
client->GetInfo(socket_info);
NPT_Debug("client connected from %s:%d\n",
socket_info.remote_address.GetIpAddress().ToString().GetChars(),
socket_info.remote_address.GetPort());
NPT_InputStreamReference input;
client->GetInputStream(input);
NPT_OutputStreamReference output;
client->GetOutputStream(output);
do {
char buffer[1024];
NPT_Size bytes_read;
result = input->Read(buffer, sizeof(buffer), &bytes_read);
if (NPT_SUCCEEDED(result)) {
NPT_Debug("read %ld bytes\n", bytes_read);
output->Write(buffer, bytes_read);
}
} while (NPT_SUCCEEDED(result));
delete client;
}
}
示例4: packet
//.........这里部分代码省略.........
current_endpoint->info.serial_port.speed = (unsigned int)speed;
} else {
NPT_Debug("ERROR: missing argument for 'serial' endpoint\n");
exit(1);
}
} else {
NPT_Debug("ERROR: invalid argument (%s)\n", arg);
exit(1);
}
if (current_endpoint == &in_endpoint) {
current_endpoint = &out_endpoint;
} else {
current_endpoint = NULL;
}
}
if (current_endpoint) {
NPT_Debug("ERROR: missing endpoint specification\n");
exit(1);
}
// data pump
NPT_Result result;
// allocate buffer
unsigned char* buffer;
buffer = (unsigned char*)malloc(packet_size);
if (buffer == NULL) {
NPT_Debug("ERROR: out of memory\n");
exit(1);
}
// get output stream
NPT_OutputStreamReference out;
result = GetEndPointStreams(&out_endpoint, NULL, &out);
if (NPT_FAILED(result)) {
NPT_Debug("ERROR: failed to get stream for output (%d)", result);
exit(1);
}
unsigned long offset = 0;
unsigned long total = 0;
if (in_endpoint.type == ENDPOINT_TYPE_UDP_SERVER ||
in_endpoint.type == ENDPOINT_TYPE_MULTICAST_SERVER) {
NPT_UdpSocket* udp_socket;
result = GetEndPointUdpSocket(&in_endpoint, udp_socket);
// packet loop
NPT_DataBuffer packet(32768);
NPT_SocketAddress address;
do {
result = udp_socket->Receive(packet, &address);
if (NPT_SUCCEEDED(result)) {
if (Options.verbose) {
NPT_String ip = address.GetIpAddress().ToString();
NPT_Debug("Received %d bytes from %s\n", packet.GetDataSize(), ip.GetChars());
}
result = out->Write(packet.GetData(), packet.GetDataSize(), NULL);
offset += packet.GetDataSize();
total += packet.GetDataSize();
}
} while (NPT_SUCCEEDED(result));
} else {
// get the input stream
NPT_InputStreamReference in;
result = GetEndPointStreams(&in_endpoint, &in, NULL);
if (NPT_FAILED(result)) {
NPT_Debug("ERROR: failed to get stream for input (%d)\n", result);
exit(1);
}
// stream loop
do {
NPT_Size bytes_read;
NPT_Size bytes_written;
// send
result = in->Read(buffer, packet_size, &bytes_read);
if (Options.show_progress) {
NPT_Debug("[%d]\r", total);
}
if (NPT_SUCCEEDED(result) && bytes_read) {
result = out->Write(buffer, bytes_read, &bytes_written);
if (Options.show_progress) {
NPT_Debug("[%d]\r", total);
}
offset += bytes_written;
total += bytes_written;
} else {
printf("[%d] *******************\n", result);
exit(1);
}
} while (NPT_SUCCEEDED(result));
}
delete buffer;
return 0;
}
示例5: serveFile2
void serveFile2(AbortableTask *task, const NPT_String& filePath, const NPT_String& mimeType, const FrontEnd::RequestContext& reqCtx, const NPT_HttpRequest *req, NPT_HttpResponse& resp, HttpOutput *httpOutput)
{
#if 1
NPT_File f(filePath);
NPT_FileInfo fileInfo;
if (NPT_FAILED(f.GetInfo(fileInfo)) || fileInfo.m_Type != NPT_FileInfo::FILE_TYPE_REGULAR || NPT_FAILED(f.Open(NPT_FILE_OPEN_MODE_READ))) {
setStatusCode(resp, 404);
httpOutput->writeResponseHeader(resp);
return;
}
NPT_InputStreamReference fileInput;
f.GetInputStream(fileInput);
serveStream(task, fileInput.AsPointer(), fileInfo.m_ModificationTime, mimeType, reqCtx, req, resp, httpOutput);
#else
bool isGetMethod = req->GetMethod().Compare("GET") == 0;
bool isHeadMethod = req->GetMethod().Compare("HEAD") == 0;
if (isGetMethod || isHeadMethod) {
NPT_Result nr;
NPT_File f(filePath);
NPT_FileInfo fileInfo;
if (NPT_FAILED(f.GetInfo(fileInfo)) || fileInfo.m_Type != NPT_FileInfo::FILE_TYPE_REGULAR || NPT_FAILED(f.Open(NPT_FILE_OPEN_MODE_READ))) {
setStatusCode(resp, 404);
httpOutput->writeResponseHeader(resp);
return;
}
NPT_InputStreamReference fileInput;
f.GetInputStream(fileInput);
NPT_UInt64 offset, length;
NPT_HttpHeader *hdrRange = req->GetHeaders().GetHeader("RANGE");
if (hdrRange) {
if (!parseRangeHeader(hdrRange->GetValue(), fileInfo.m_Size, offset, length)) {
setStatusCode(resp, 416);
httpOutput->writeResponseHeader(resp);
return;
}
setStatusCode(resp, 206);
resp.GetHeaders().SetHeader(NPT_HTTP_HEADER_CONTENT_RANGE, NPT_String::Format("bytes %s-%s/%s", NPT_String::FromIntegerU(offset).GetChars(), NPT_String::FromIntegerU(offset + length - 1).GetChars(), NPT_String::FromIntegerU(fileInfo.m_Size).GetChars()));
fileInput->Seek(offset);
} else {
offset = 0;
length = fileInfo.m_Size;
setStatusCode(resp, 200);
}
resp.GetHeaders().SetHeader(NPT_HTTP_HEADER_CONTENT_TYPE, mimeType);
resp.GetHeaders().SetHeader(NPT_HTTP_HEADER_CONTENT_LENGTH, NPT_String::FromIntegerU(length));
resp.GetHeaders().SetHeader("Last-Modified", NPT_DateTime(fileInfo.m_ModificationTime).ToString(NPT_DateTime::FORMAT_RFC_1123));
resp.GetHeaders().SetHeader("Accept-Ranges", "bytes");
resp.GetHeaders().SetHeader("EXT", "");
if (reqCtx.transferMode != FrontEnd::TM_None) {
const char *transferMode = "Streaming";
switch (reqCtx.transferMode) {
case FrontEnd::TM_Background:
transferMode = "Background";
break;
case FrontEnd::TM_Interactive:
transferMode = "Interactive";
break;
}
resp.GetHeaders().SetHeader("transferMode.dlna.org", transferMode);
}
if (reqCtx.getcontentFeaturesReq) {
NPT_String contentFeatures("DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000");
resp.GetHeaders().SetHeader("contentFeatures.dlna.org", contentFeatures);
}
httpOutput->writeResponseHeader(resp);
if (isGetMethod) {
bool abortFlag = false;
ServeFileAbortCallback abortCallback(&abortFlag);
if (task->registerAbortCallback(&abortCallback)) {
NPT_DataBuffer buffer(4096);
NPT_UInt64 cbRemain = length;
for (;;) {
if (abortFlag) {
break;
}
if (cbRemain == 0) {
break;
}
NPT_Size cbRead;
NPT_UInt64 cbToRead = cbRemain;
if (cbToRead > buffer.GetBufferSize()) {
cbToRead = buffer.GetBufferSize();
}
nr = fileInput->Read(buffer.UseData(), buffer.GetBufferSize(), &cbRead);
if (NPT_FAILED(nr)) {
//.........这里部分代码省略.........
示例6: standard_out
/*----------------------------------------------------------------------
| ShowResponse
+---------------------------------------------------------------------*/
static void
ShowResponse(NPT_HttpResponse* response, ShowMode mode)
{
// show response info
NPT_Debug("RESPONSE: protocol=%s, code=%d, reason=%s\n",
response->GetProtocol().GetChars(),
response->GetStatusCode(),
response->GetReasonPhrase().GetChars());
// show headers
NPT_HttpHeaders& headers = response->GetHeaders();
NPT_List<NPT_HttpHeader*>::Iterator header = headers.GetHeaders().GetFirstItem();
while (header) {
NPT_Debug("%s: %s\n",
(const char*)(*header)->GetName(),
(const char*)(*header)->GetValue());
++header;
}
// show entity
NPT_HttpEntity* entity = response->GetEntity();
if (entity != NULL) {
NPT_Debug("ENTITY: length=%lld, type=%s, encoding=%s\n",
entity->GetContentLength(),
entity->GetContentType().GetChars(),
entity->GetContentEncoding().GetChars());
switch (mode) {
case SHOW_MODE_LOAD: {
NPT_DataBuffer body;
NPT_Result result =entity->Load(body);
if (NPT_FAILED(result)) {
NPT_Debug("ERROR: failed to load entity (%d)\n", result);
} else {
NPT_Debug("BODY: loaded %d bytes\n", (int)body.GetDataSize());
// dump the body
NPT_OutputStreamReference output;
NPT_File standard_out(NPT_FILE_STANDARD_OUTPUT);
standard_out.Open(NPT_FILE_OPEN_MODE_WRITE);
standard_out.GetOutputStream(output);
NPT_Debug("%s", (char *)body.GetData());
// ÔÝʱ²»Ö§³Ö
// output->Write(body.GetData(), body.GetDataSize());
}
break;
}
case SHOW_MODE_STREAM_BLOCKING: {
NPT_DataBuffer buffer(4096);
NPT_Result result;
NPT_InputStreamReference input;
entity->GetInputStream(input);
do {
NPT_Size bytes_read = 0;
result = input->Read(buffer.UseData(), 4096, &bytes_read);
NPT_Debug("read %d bytes\n", bytes_read);
} while (NPT_SUCCEEDED(result));
break;
}
}
}
}
示例7: address
/*----------------------------------------------------------------------
| main
+---------------------------------------------------------------------*/
int
main(int /*argc*/, char** /*argv*/)
{
// setup debugging
#if defined(WIN32) && defined(_DEBUG)
int flags = _crtDbgFlag |
_CRTDBG_ALLOC_MEM_DF |
_CRTDBG_DELAY_FREE_MEM_DF |
_CRTDBG_CHECK_ALWAYS_DF;
_CrtSetDbgFlag(flags);
//AllocConsole();
//freopen("CONOUT$", "w", stdout);
#endif
NPT_Result result;
TcpServerThread* server_thread = NULL;
NPT_TcpClientSocket* tcp_client = NULL;
NPT_TcpServerSocket* tcp_server = NULL;
CancellerThread* canceller = NULL;
NPT_SocketAddress address(NPT_IpAddress(127,0,0,1), 10000);
#if 0
result = RemoteIpAddress.ResolveName("www.google.com");
CHECK(result == NPT_SUCCESS);
NPT_Console::Output("--- test for immediate connection\n");
NPT_Console::Output("[01] starting write server thread\n");
server_thread = new TcpServerThread();
server_thread->Start();
NPT_Console::Output("[01] waiting for server to be ready...\n");
server_thread->m_Ready.WaitUntilEquals(1);
NPT_Console::Output("[01] server thread ready\n");
NPT_Console::Output("[01] waiting a while...\n");
NPT_System::Sleep(3.0);
tcp_client = new NPT_TcpClientSocket();
NPT_Console::Output("[01] connection to 127.0.0.1:10000\n");
result = tcp_client->Connect(address);
NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
CHECK(result == NPT_SUCCESS);
delete tcp_client;
NPT_Console::Output("[01] terminating server\n");
server_thread->m_Interrupted = true;
server_thread->Wait();
delete server_thread;
NPT_Console::Output("\n--- test for refused local connection\n");
address.SetPort(89);
tcp_client = new NPT_TcpClientSocket();
NPT_Console::Output("[01] connecting to 127.0.0.1:89\n");
result = tcp_client->Connect(address);
NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
CHECK(result == NPT_ERROR_CONNECTION_REFUSED);
delete tcp_client;
/*NPT_Console::Output("\n--- test for refused remote connection\n");
address.SetIpAddress(RemoteIpAddress);
address.SetPort(81);
tcp_client = new NPT_TcpClientSocket();
NPT_Console::Output("[01] connecting to www.google.com:81\n");
result = tcp_client->Connect(address);
NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
CHECK(result == NPT_ERROR_CONNECTION_REFUSED);
delete tcp_client;*/
NPT_Console::Output("\n--- test for connection timeout\n");
address.SetIpAddress(NPT_IpAddress(1,1,1,1));
NPT_Console::Output("[01] connecting to 1.1.1.1:89\n");
tcp_client = new NPT_TcpClientSocket();
result = tcp_client->Connect(address, 3000);
NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
CHECK(result == NPT_ERROR_TIMEOUT);
delete tcp_client;
NPT_Console::Output("\n--- test for remote connection\n");
address.SetIpAddress(RemoteIpAddress);
address.SetPort(80);
NPT_Console::Output("[01] connecting to www.google.com:80\n");
tcp_client = new NPT_TcpClientSocket();
result = tcp_client->Connect(address);
NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
CHECK(result == NPT_SUCCESS);
delete tcp_client;
#endif
for (int i=0; i<2; i++) {
NPT_Console::OutputF("\n--- test for cancelled connection, shutdown=%d\n", i);
address.SetIpAddress(NPT_IpAddress(1,1,1,1));
address.SetPort(89);
NPT_Console::Output("[01] connecting to 1.1.1.1:89\n");
tcp_client = new NPT_TcpClientSocket(NPT_SOCKET_FLAG_CANCELLABLE);
canceller = new CancellerThread(tcp_client, 3.0f, i==1);
result = tcp_client->Connect(address);
NPT_Console::OutputF("[01] connect returns %d : %s\n", result, NPT_ResultText(result));
CHECK(result == NPT_ERROR_CANCELLED);
canceller->Wait();
delete canceller;
//.........这里部分代码省略.........