2018-05-16 00:04:18 +00:00
|
|
|
#include <ConditionalMacros.h>
|
|
|
|
#if TARGET_API_MAC_CARBON
|
|
|
|
#define OTCARBONAPPLICATION 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "OpenTptStream.h"
|
|
|
|
|
|
|
|
#include <OpenTransport.h>
|
|
|
|
#include <OpenTransportProviders.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <TextUtils.h>
|
|
|
|
|
2019-01-09 19:12:59 +00:00
|
|
|
#ifdef DEBUG_CONSOLE
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-16 00:04:18 +00:00
|
|
|
OpenTptStream::OpenTptStream()
|
|
|
|
{
|
|
|
|
InitOpenTransport();
|
|
|
|
OSStatus err;
|
|
|
|
|
|
|
|
|
|
|
|
//endpoint = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, nullptr, &err);
|
|
|
|
endpoint = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, nullptr, &err);
|
|
|
|
|
|
|
|
InetAddress addr, retAddr;
|
|
|
|
OTInitInetAddress(&addr, 1984, 0);
|
|
|
|
|
|
|
|
TBind req, ret;
|
|
|
|
|
|
|
|
req.addr.len = sizeof(addr);
|
|
|
|
req.addr.buf = (UInt8*) &addr;
|
|
|
|
req.qlen = 1;
|
|
|
|
ret.addr.maxlen = sizeof(retAddr);
|
|
|
|
ret.addr.buf = (UInt8*) &retAddr;
|
|
|
|
|
|
|
|
|
|
|
|
endpoint->Bind(&req, &ret);
|
|
|
|
|
|
|
|
endpoint->SetSynchronous();
|
|
|
|
endpoint->SetNonBlocking();
|
|
|
|
tryListening();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenTptStream::tryListening()
|
|
|
|
{
|
|
|
|
if(connected)
|
|
|
|
DebugStr("\pDouble connect");
|
|
|
|
|
|
|
|
InetAddress clientAddr;
|
|
|
|
memset(&call, 0, sizeof(call));
|
|
|
|
call.addr.maxlen = sizeof(clientAddr);
|
|
|
|
call.addr.len = sizeof(clientAddr);
|
|
|
|
call.addr.buf = (UInt8*) &clientAddr;
|
|
|
|
|
|
|
|
OSStatus err;
|
|
|
|
|
|
|
|
err = endpoint->Listen(&call);
|
2019-01-09 19:12:59 +00:00
|
|
|
#ifdef DEBUG_CONSOLE
|
|
|
|
printf("Listen: err = %d.\n", (int)err);
|
|
|
|
#endif
|
|
|
|
|
2018-05-16 00:04:18 +00:00
|
|
|
if(err < 0)
|
|
|
|
return; // hopefully, kOTNoData
|
|
|
|
|
|
|
|
endpoint->SetBlocking();
|
|
|
|
|
|
|
|
err = endpoint->Accept(endpoint, &call);
|
2019-01-09 19:12:59 +00:00
|
|
|
#ifdef DEBUG_CONSOLE
|
|
|
|
printf("Accept: err = %d.\n", (int)err);
|
|
|
|
#endif
|
|
|
|
|
2018-05-16 00:04:18 +00:00
|
|
|
endpoint->SetNonBlocking();
|
|
|
|
|
|
|
|
connected = true;
|
|
|
|
tryReading();
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenTptStream::~OpenTptStream()
|
|
|
|
{
|
|
|
|
endpoint->SetSynchronous();
|
|
|
|
endpoint->SetNonBlocking();
|
|
|
|
if(connected)
|
|
|
|
endpoint->SndDisconnect(&call);
|
|
|
|
endpoint->Unbind();
|
|
|
|
OTCloseProvider(endpoint);
|
|
|
|
CloseOpenTransport();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenTptStream::write(const void* p, size_t n)
|
|
|
|
{
|
|
|
|
endpoint->SetBlocking();
|
|
|
|
endpoint->Snd((void*)p, n, 0);
|
|
|
|
endpoint->SetNonBlocking();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenTptStream::tryReading()
|
|
|
|
{
|
|
|
|
OTResult result;
|
|
|
|
OTFlags flags;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result = endpoint->Rcv(readBuffer, kReadBufferSize, &flags);
|
|
|
|
if(result > 0)
|
|
|
|
{
|
|
|
|
notifyReceive(readBuffer, result);
|
|
|
|
}
|
|
|
|
} while(result > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenTptStream::idle()
|
|
|
|
{
|
|
|
|
OSStatus err;
|
|
|
|
switch(endpoint->Look())
|
|
|
|
{
|
|
|
|
case T_DISCONNECT:
|
|
|
|
endpoint->RcvDisconnect(nullptr);
|
|
|
|
connected = false;
|
|
|
|
tryListening();
|
|
|
|
break;
|
|
|
|
case T_ORDREL:
|
|
|
|
err = endpoint->RcvOrderlyDisconnect();
|
|
|
|
if(err == noErr)
|
|
|
|
endpoint->SndOrderlyDisconnect();
|
|
|
|
connected = false;
|
|
|
|
tryListening();
|
|
|
|
break;
|
|
|
|
case T_LISTEN:
|
|
|
|
tryListening();
|
|
|
|
break;
|
|
|
|
case T_DATA:
|
|
|
|
tryReading();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|