void ARTSPConnection::performConnect(
const sp<AMessage> &reply,
AString host,
unsigned port) {
struct hostent *ent = gethostbyname(host.c_str());
if (ent == NULL) {
ALOGE(
"Unknown host %s", host.c_str());
reply->setInt32(
"result", -ENOENT);
reply->post();
mState = DISCONNECTED;
return;
}
mSocket = socket(AF_INET, SOCK_STREAM,
0);
if (mUIDValid) {
HTTPBase::RegisterSocketUserTag(mSocket, mUID,
(uint32_t)*(uint32_t*)
"RTSP");
HTTPBase::RegisterSocketUserMark(mSocket, mUID);
}
MakeSocketBlocking(mSocket,
false);
*
unsigned short sin_port;
*
struct in_addr sin_addr;
*
unsigned char sin_zero[
8];
*};
*/
struct sockaddr_in remote;
memset(remote.sin_zero,
0,
sizeof(remote.sin_zero));
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = *(in_addr_t *)ent->h_addr;
remote.sin_port = htons(port);
int err = ::connect(
mSocket, (
const struct sockaddr *)&remote,
sizeof(remote));
reply->setInt32(
"server-ip", ntohl(remote.sin_addr.s_addr));
if (err <
0) {
if (errno == EINPROGRESS) {
sp<AMessage> msg =
new AMessage(kWhatCompleteConnection,
this);
msg->setMessage(
"reply", reply);
msg->setInt32(
"connection-id", mConnectionID);
msg->post();
return;
}
reply->setInt32(
"result", -errno);
mState = DISCONNECTED;
if (mUIDValid) {
HTTPBase::UnRegisterSocketUserTag(mSocket);
HTTPBase::UnRegisterSocketUserMark(mSocket);
}
close(mSocket);
mSocket = -
1;
}
else {
reply->setInt32(
"result", OK);
mState = CONNECTED;
mNextCSeq =
1;
postReceiveReponseEvent();
}
reply->post();
}
转载请注明原文地址: https://ju.6miu.com/read-2374.html