1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef A_NETWORK_SESSION_H_
18
19#define A_NETWORK_SESSION_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <utils/KeyedVector.h>
23#include <utils/RefBase.h>
24#include <utils/Thread.h>
25
26#include <netinet/in.h>
27
28namespace android {
29
30struct AMessage;
31
32// Helper class to manage a number of live sockets (datagram and stream-based)
33// on a single thread. Clients are notified about activity through AMessages.
34struct ANetworkSession : public RefBase {
35    ANetworkSession();
36
37    status_t start();
38    status_t stop();
39
40    status_t createRTSPClient(
41            const char *host, unsigned port, const sp<AMessage> &notify,
42            int32_t *sessionID);
43
44    status_t createRTSPServer(
45            const struct in_addr &addr, unsigned port,
46            const sp<AMessage> &notify, int32_t *sessionID);
47
48    status_t createUDPSession(
49            unsigned localPort, const sp<AMessage> &notify, int32_t *sessionID);
50
51    status_t createUDPSession(
52            unsigned localPort,
53            const char *remoteHost,
54            unsigned remotePort,
55            const sp<AMessage> &notify,
56            int32_t *sessionID);
57
58    status_t connectUDPSession(
59            int32_t sessionID, const char *remoteHost, unsigned remotePort);
60
61    // passive
62    status_t createTCPDatagramSession(
63            const struct in_addr &addr, unsigned port,
64            const sp<AMessage> &notify, int32_t *sessionID);
65
66    // active
67    status_t createTCPDatagramSession(
68            unsigned localPort,
69            const char *remoteHost,
70            unsigned remotePort,
71            const sp<AMessage> &notify,
72            int32_t *sessionID);
73
74    status_t destroySession(int32_t sessionID);
75
76    status_t sendRequest(
77            int32_t sessionID, const void *data, ssize_t size = -1);
78
79    enum NotificationReason {
80        kWhatError,
81        kWhatConnected,
82        kWhatClientConnected,
83        kWhatData,
84        kWhatDatagram,
85        kWhatBinaryData,
86    };
87
88protected:
89    virtual ~ANetworkSession();
90
91private:
92    struct NetworkThread;
93    struct Session;
94
95    Mutex mLock;
96    sp<Thread> mThread;
97
98    int32_t mNextSessionID;
99
100    int mPipeFd[2];
101
102    KeyedVector<int32_t, sp<Session> > mSessions;
103
104    enum Mode {
105        kModeCreateUDPSession,
106        kModeCreateTCPDatagramSessionPassive,
107        kModeCreateTCPDatagramSessionActive,
108        kModeCreateRTSPServer,
109        kModeCreateRTSPClient,
110    };
111    status_t createClientOrServer(
112            Mode mode,
113            const struct in_addr *addr,
114            unsigned port,
115            const char *remoteHost,
116            unsigned remotePort,
117            const sp<AMessage> &notify,
118            int32_t *sessionID);
119
120    void threadLoop();
121    void interrupt();
122
123    static status_t MakeSocketNonBlocking(int s);
124
125    DISALLOW_EVIL_CONSTRUCTORS(ANetworkSession);
126};
127
128}  // namespace android
129
130#endif  // A_NETWORK_SESSION_H_
131