File: socketstream.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (235 lines) | stat: -rw-r--r-- 6,963 bytes parent folder | download | duplicates (4)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/streams/socketstream.cpp
// Purpose:     Test wxSocketInputStream/wxSocketOutputStream
// Author:      Vadim Zeitlin
// Copyright:   (c) 2008 Vadim Zeitlin
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
// and "wx/cppunit.h"
#include "testprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers
#ifndef WX_PRECOMP
    #include "wx/log.h"
#endif

#include "wx/socket.h"
#include "wx/sckstrm.h"
#include "wx/thread.h"

#include "bstream.h"

namespace
{

const int TEST_PORT_READ = 0x7778;  // arbitrary, chosen because == "wx"
const int TEST_PORT_WRITE = 0x7779; // well, "wy"

// these cond and mutex are used to minimize the risk of the main thread
// Connect()-ing before this thread starts Accept()-ing connections but
// unfortunately we can't make this truly safe, see comment in
// SocketServerThread::Entry()
wxMutex gs_mutex;
wxCondition gs_cond(gs_mutex);
} // anonymous namespace

// return address for the given port on local host
static inline wxIPV4address LocalAddress(int port)
{
    wxIPV4address addr;
    addr.LocalHost();
    addr.Service(port);

    return addr;
}

// A thread which creates a listening socket on the specified port and executes
// the given function with each socket which connects to it
class SocketServerThread : public wxThread
{
public:
    // port is the port to listen on and function will be called on each
    // accepted socket
    SocketServerThread(int port, void (*accept)(wxSocketBase&))
        : wxThread(wxTHREAD_JOINABLE),
          m_port(port),
          m_accept(accept)
    {
        Create();
        Run();
    }

protected:
    virtual void *Entry()
    {
        wxSocketServer srv(LocalAddress(m_port), wxSOCKET_REUSEADDR);
        CPPUNIT_ASSERT( srv.IsOk() );

        // FIXME: this is still not atomic, of course and the main thread could
        //        call Connect() before we have time to Accept() but there is
        //        no way to fix it with current API
        {
            wxMutexLocker lock(gs_mutex);
            gs_cond.Signal();
        }

        wxSocketBase *socket = srv.Accept();
        if ( socket )
        {
            (*m_accept)(*socket);
            delete socket;
        }

        return NULL;
    }

    int m_port;
    void (*m_accept)(wxSocketBase&);

    DECLARE_NO_COPY_CLASS(SocketServerThread)
};

// The test case for socket streams
class socketStream :
        public BaseStreamTestCase<wxSocketInputStream, wxSocketOutputStream>
{
public:
    socketStream();
    virtual ~socketStream();

    virtual void setUp();
    virtual void tearDown();

    // repeat all socket tests several times with different socket flags, so we
    // define this macro which is used several times in the test suite
    //
    // there must be some more elegant way to do this but I didn't find it...
#define ALL_SOCKET_TESTS()                                                    \
        CPPUNIT_TEST(Input_GetC);                                             \
        CPPUNIT_TEST(Input_Eof);                                              \
        CPPUNIT_TEST(Input_Read);                                             \
        CPPUNIT_TEST(Input_LastRead);                                         \
        CPPUNIT_TEST(Input_CanRead);                                          \
        CPPUNIT_TEST(Input_Peek);                                             \
        CPPUNIT_TEST(Input_Ungetch);                                          \
                                                                              \
        CPPUNIT_TEST(Output_PutC);                                            \
        CPPUNIT_TEST(Output_Write);                                           \
        CPPUNIT_TEST(Output_LastWrite)

    CPPUNIT_TEST_SUITE(socketStream);
        ALL_SOCKET_TESTS();
        // some tests don't pass with NOWAIT flag but this is probably not a
        // bug (TODO: check this)
#if 0
        CPPUNIT_TEST( PseudoTest_SetNoWait );
        ALL_SOCKET_TESTS();
#endif
        CPPUNIT_TEST( PseudoTest_SetWaitAll );
        ALL_SOCKET_TESTS();
    CPPUNIT_TEST_SUITE_END();

private:
    // Implement base class functions.
    virtual wxSocketInputStream  *DoCreateInStream();
    virtual wxSocketOutputStream *DoCreateOutStream();

    // socket thread functions
    static void WriteSocket(wxSocketBase& socket)
    {
        socket.Write("hello, world!", 13);
    }

    static void ReadSocket(wxSocketBase& socket)
    {
        char ch;
        while ( socket.Read(&ch, 1).LastCount() == 1 )
            ;
    }

    void PseudoTest_SetNoWait() { ms_flags = wxSOCKET_NOWAIT; }
    void PseudoTest_SetWaitAll() { ms_flags = wxSOCKET_WAITALL; }

    wxSocketClient *m_readSocket,
                   *m_writeSocket;
    wxThread *m_writeThread,
             *m_readThread;

    static wxSocketFlags ms_flags;
};

wxSocketFlags socketStream::ms_flags = wxSOCKET_NONE;

socketStream::socketStream()
{
    m_readSocket =
    m_writeSocket = NULL;

    m_writeThread =
    m_readThread = NULL;

    wxSocketBase::Initialize();
}

socketStream::~socketStream()
{
    wxSocketBase::Shutdown();
}

void socketStream::setUp()
{
    // create the socket threads and wait until they are ready to accept
    // connections (if we called Connect() before this happens, it would fail)
    {
        wxMutexLocker lock(gs_mutex);

        m_writeThread =
            new SocketServerThread(TEST_PORT_READ, &socketStream::WriteSocket);
        CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, gs_cond.Wait() );

        m_readThread =
            new SocketServerThread(TEST_PORT_WRITE, &socketStream::ReadSocket);
        CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, gs_cond.Wait() );
    }

    m_readSocket = new wxSocketClient(ms_flags);
    CPPUNIT_ASSERT( m_readSocket->Connect(LocalAddress(TEST_PORT_READ)) );

    m_writeSocket = new wxSocketClient(ms_flags);
    CPPUNIT_ASSERT( m_writeSocket->Connect(LocalAddress(TEST_PORT_WRITE)) );
}

void socketStream::tearDown()
{
    wxDELETE(m_readSocket);
    wxDELETE(m_writeSocket);

    m_writeThread->Wait();
    wxDELETE(m_writeThread);

    m_readThread->Wait();
    wxDELETE(m_readThread);
}

wxSocketInputStream *socketStream::DoCreateInStream()
{
    wxSocketInputStream *pStrInStream = new wxSocketInputStream(*m_readSocket);
    CPPUNIT_ASSERT(pStrInStream->IsOk());
    return pStrInStream;
}

wxSocketOutputStream *socketStream::DoCreateOutStream()
{
    wxSocketOutputStream *pStrOutStream = new wxSocketOutputStream(*m_writeSocket);
    CPPUNIT_ASSERT(pStrOutStream->IsOk());
    return pStrOutStream;
}

// Register the stream sub suite, by using some stream helper macro.
STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(socketStream)