Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
main.cp
#include <iostream.h> |
#include <OpenTransport.h> |
#include <OpenTptInternet.h> |
#include "TNetworkStream.h" |
// |
// TNetworkSession - OpenTransport Network Session class |
// |
class TNetworkSession |
{ |
// CONSTRUCTORS AND DESTRUCTORS |
public: |
TNetworkSession(); |
virtual ~TNetworkSession(); |
// FUNCTIONS |
void Close(); |
void sendSomeData(); |
// ACCESSORS |
public: |
EndpointRef GetEndpoint() { return fEndPoint; }; |
TNetworkIOStream& GetStreamRef() { return fioStream; }; |
// OPEN TRANSPORT CALLBACK |
private: |
static pascal void NotifyProc (TNetworkSession* , OTEventCode , OTResult , void* ); |
// PROTECTED FIELDS |
protected: |
EndpointRef fEndPoint; // Endpoint ref |
TEndpointInfo fInfo; // Endpoint information |
TNetworkIOStream fioStream; // ioStream object |
}; |
// --------------------------------------------------------------------------- |
// NotifyProc (queue up an event) |
// --------------------------------------------------------------------------- |
// TNetworkSession OT Notifier Proc |
pascal void TNetworkSession::NotifyProc(void* contextPtr , OTEventCode theEvent, OTResult theResult, void* theParam) |
{ |
TNetworkSession* theSession = contextPtr; |
try |
{ |
switch ( theEvent ){ |
///// OpenEndpoint Completed |
case T_OPENCOMPLETE: |
// record endpoint |
theSession->fEndPoint = (EndpointRef) theParam; |
// Enable AckSends |
::OTAckSends(theSession->fEndPoint); |
// attach endpoint to stream; |
theSession->fioStream.attach(theSession->fEndPoint, & theSession->fInfo); |
// .... your code here.. |
break; |
//// No-copy memory was released |
case T_MEMORYRELEASED: |
TNetworkBuf::Release(theParam); |
break; |
////..Other ... |
default: |
break; |
} |
} |
catch(...) // catch everything |
{ |
DebugStr("\p TNetworkSession::NotifyProc -- Other Exception.. "); |
} |
} |
// --------------------------------------------------------------------------- |
// TNetworkSession |
// --------------------------------------------------------------------------- |
// Default Constructor |
TNetworkSession::TNetworkSession() |
{ |
// Setup a new end point |
fEndPoint = kOTInvalidEndpointRef; |
::OTAsyncOpenEndpoint( ::OTCreateConfiguration(kTCPName) ,0, &fInfo,(*OTNotifyProcPtr) NotifyProc, this ); |
// more code here |
} |
// --------------------------------------------------------------------------- |
// TNetworkAcceptor::Close( ) |
// --------------------------------------------------------------------------- |
// Session completed.. |
void TNetworkSession::Close() |
{ |
OTResult state = ::OTGetEndpointState(fEndPoint); |
// If your not shutdown yet |
if(state == T_DATAXFER) { |
// flush out all waiting data |
fioStream << flush; |
// and disconnect the endpoint |
if( (fInfo.flags == T_COTS_ORD) || (fInfo.flags == T_TRANS_ORD)) |
::OTSndOrderlyDisconnect(fEndPoint); |
else |
::OTSndDisconnect (fEndPoint,nil); |
} |
} |
// --------------------------------------------------------------------------- |
// TNetworkAcceptor::sendSomeData( ) |
// --------------------------------------------------------------------------- |
// Send some networking data |
#define CRLF "\r\n" |
void TNetworkSession::sendSomeData() |
{ |
TNetworkIOStream& nio = GetStreamRef(); |
nio << "HTTP/1.0 200 OK" << CRLF; |
nio << "MIME-Version: 1.0" << CRLF; |
nio << "Content-type: text/html" << CRLF; |
nio << "Server: Knucklehead /1.0" << CRLF; |
nio << CRLF << flush; |
} |
// |
// |
// |
void main() |
{ |
TNetworkSession *theSession; |
theSession = new TNetworkSession; |
theSession->sendSomeData(); |
theSession->Close; |
delete theSession; |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14