1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include "StreamTransceiver.h"
#include "StreamEndpointI.h"
#include "StreamAcceptor.h"
#include <IceUtil/StringUtil.h>
#include <Ice/Instance.h>
#include <Ice/UniqueRef.h>
#include <Ice/Network.h>
#include <Ice/Exception.h>
#include <Ice/Properties.h>
#include <CoreFoundation/CoreFoundation.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
NativeInfoPtr
IceObjC::StreamAcceptor::getNativeInfo()
{
return this;
}
void
IceObjC::StreamAcceptor::close()
{
if(_fd != INVALID_SOCKET)
{
closeSocketNoThrow(_fd);
_fd = INVALID_SOCKET;
}
}
EndpointIPtr
IceObjC::StreamAcceptor::listen()
{
try
{
const_cast<Address&>(_addr) = doBind(_fd, _addr);
doListen(_fd, _backlog);
}
catch(...)
{
_fd = INVALID_SOCKET;
throw;
}
_endpoint = _endpoint->endpoint(this);
return _endpoint;
}
TransceiverPtr
IceObjC::StreamAcceptor::accept()
{
SOCKET fd = doAccept(_fd);
setBlock(fd, false);
setTcpBufSize(fd, _instance);
//
// Create the read/write streams
//
UniqueRef<CFReadStreamRef> readStream;
UniqueRef<CFWriteStreamRef> writeStream;
try
{
CFStreamCreatePairWithSocket(ICE_NULLPTR, fd, &readStream.get(), &writeStream.get());
_instance->setupStreams(readStream.get(), writeStream.get(), true, "");
return new StreamTransceiver(_instance, readStream.release(), writeStream.release(), fd);
}
catch(const Ice::LocalException& ex)
{
if(fd != INVALID_SOCKET)
{
closeSocketNoThrow(fd);
}
throw;
}
}
string
IceObjC::StreamAcceptor::protocol() const
{
return _instance->protocol();
}
string
IceObjC::StreamAcceptor::toString() const
{
return addrToString(_addr);
}
string
IceObjC::StreamAcceptor::toDetailedString() const
{
ostringstream os;
os << "local address = " << toString();
vector<string> intfs = getHostsForEndpointExpand(inetAddrToString(_addr), _instance->protocolSupport(), true);
if(!intfs.empty())
{
os << "\nlocal interfaces = ";
os << IceUtilInternal::joinString(intfs, ", ");
}
return os.str();
}
int
IceObjC::StreamAcceptor::effectivePort() const
{
return getPort(_addr);
}
IceObjC::StreamAcceptor::StreamAcceptor(const StreamEndpointIPtr& endpoint,
const InstancePtr& instance,
const string& host,
int port) :
_endpoint(endpoint),
_instance(instance),
_addr(getAddressForServer(host, port, instance->protocolSupport(), instance->preferIPv6(), true))
{
#ifdef SOMAXCONN
_backlog = instance->properties()->getPropertyAsIntWithDefault("Ice.TCP.Backlog", SOMAXCONN);
#else
_backlog = instance->properties()->getPropertyAsIntWithDefault("Ice.TCP.Backlog", 511);
#endif
try
{
_fd = createSocket(false, _addr);
setBlock(_fd, false);
setTcpBufSize(_fd, _instance);
setReuseAddress(_fd, true);
}
catch(...)
{
_fd = INVALID_SOCKET;
throw;
}
}
IceObjC::StreamAcceptor::~StreamAcceptor()
{
assert(_fd == INVALID_SOCKET);
}
|