00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/Debug.h>
00018 #include <spl/net/PortListener.h>
00019 #include <spl/net/SocketSet.h>
00020 #include <spl/threading/Thread.h>
00021 #include <spl/Log.h>
00022
00023 #ifdef DEBUG
00024 static volatile int _debug_msgcount;
00025
00026 class ReflectorStListener;
00027 typedef RefCountPtrCast<ReflectorStListener, IStreamReadListener, IStreamReadListenerPtr> ReflectorStListenerPtr;
00028
00029 class ReflectorStListener : public IStreamReadListener
00030 {
00031 private:
00032 SocketSet *m_ss;
00033
00034 public:
00035 ReflectorStListener(SocketSet *ss)
00036 : m_ss(ss)
00037 {
00038 }
00039
00040 virtual void IStreamRead_OnRead( const Array<byte>& buf, int len )
00041 {
00042 _debug_msgcount++;
00043 m_ss->Broadcast( buf, len );
00044 }
00045
00046 virtual void IStreamRead_OnClose()
00047 {
00048 }
00049
00050 virtual void IStreamRead_OnError( const String& msg )
00051 {
00052 UNIT_ASSERT(msg.GetChars(), false);
00053 }
00054
00055 #if defined(DEBUG) || defined(_DEBUG)
00056 void CheckMem() const
00057 {
00058 }
00059 void ValidateMem() const
00060 {
00061 }
00062 #endif
00063 };
00064
00065 class ReflectorStServer : public IPortListenerListener
00066 {
00067 protected:
00068 SocketSet m_ss;
00069 PortListener m_plistener;
00070
00071 public:
00072 inline ReflectorStServer()
00073 : m_plistener(8011), m_ss()
00074 {
00075 m_plistener.Delegates().Add(this);
00076 }
00077
00078 inline ~ReflectorStServer()
00079 {
00080 m_ss.CloseAndDelete();
00081 m_plistener.Stop();
00082 }
00083
00084 inline void Start()
00085 {
00086 m_plistener.Start();
00087 }
00088
00089 virtual void IPortListener_OnConnect( TcpSocketPtr sock )
00090 {
00091 Array<byte> buf((const byte *)"HI\n", 4);
00092 sock->SetLingerOn();
00093 sock->SetBlocking();
00094 m_ss.AddSocket( ReflectorStListenerPtr(new ReflectorStListener(&m_ss)), sock );
00095 sock->GetStream()->Write( buf, 0, 4 );
00096 }
00097
00098 virtual void IPortListener_OnStop()
00099 {
00100 m_ss.CloseAndDelete();
00101 }
00102
00103 #if defined(DEBUG) || defined(_DEBUG)
00104 void CheckMem() const
00105 {
00106 m_plistener.CheckMem();
00107 m_ss.CheckMem();
00108 }
00109 void ValidateMem() const
00110 {
00111 m_plistener.ValidateMem();
00112 m_ss.ValidateMem();
00113 }
00114 #endif
00115 };
00116
00117 static void testSS1()
00118 {
00119 DEBUG_CLEAR_MEM_CHECK_POINTS();
00120 UNIT_ASSERT_MEM_NOTED("testSS1");
00121
00122 _debug_msgcount = 0;
00123 Array<byte> buf(255);
00124 ReflectorStServer srv;
00125
00126 DEBUG_CLEAR_MEM_CHECK_POINTS();
00127 srv.CheckMem();
00128 buf.CheckMem();
00129 DEBUG_DUMP_MEM_LEAKS();
00130 UNIT_ASSERT_MEM_NOTED("testSS1");
00131
00132 srv.Start();
00133 Thread::YYield();
00134
00135 DEBUG_CLEAR_MEM_CHECK_POINTS();
00136 srv.CheckMem();
00137 buf.CheckMem();
00138 DEBUG_DUMP_MEM_LEAKS();
00139 UNIT_ASSERT_MEM_NOTED("testSS1");
00140
00141 TcpSocket cnt( "127.0.0.1", 8011 );
00142 cnt.SetBlocking();
00143 cnt.Connect();
00144
00145 Thread::YYield();
00146 DEBUG_CLEAR_MEM_CHECK_POINTS();
00147 srv.CheckMem();
00148 buf.CheckMem();
00149 DEBUG_DUMP_MEM_LEAKS();
00150 UNIT_ASSERT_MEM_NOTED("socketTest");
00151 int count = cnt.GetStream()->Read( buf, 0, 255 );
00152
00153 Thread::YYield();
00154 DEBUG_CLEAR_MEM_CHECK_POINTS();
00155 srv.CheckMem();
00156 buf.CheckMem();
00157 DEBUG_DUMP_MEM_LEAKS();
00158 UNIT_ASSERT_MEM_NOTED("socketTest");
00159
00160 UNIT_TEST( "socketTest recv H", buf[0] == 'H' );
00161 UNIT_TEST( "socketTest recv I", buf[1] == 'I' );
00162
00163 buf[0] = 'Q';
00164 buf[1] = '\0';
00165 cnt.GetStream()->Write( buf, 0, 2 );
00166 Thread::YYield();
00167 DEBUG_CLEAR_MEM_CHECK_POINTS();
00168 srv.CheckMem();
00169 buf.CheckMem();
00170 DEBUG_DUMP_MEM_LEAKS();
00171 UNIT_ASSERT_MEM_NOTED("socketTest");
00172
00173 buf[0] = 'A';
00174 count = cnt.GetStream()->Read( buf, 0, 255 );
00175 UNIT_TEST( "socketTest recv Q", buf[0] == 'Q' );
00176 UNIT_TEST( "socketTest msg count", _debug_msgcount == 1 );
00177
00178 Thread::YYield();
00179 DEBUG_CLEAR_MEM_CHECK_POINTS();
00180 srv.CheckMem();
00181 buf.CheckMem();
00182 DEBUG_DUMP_MEM_LEAKS();
00183 UNIT_ASSERT_MEM_NOTED("socketTest");
00184
00185 cnt.Close();
00186 Thread::YYield();
00187 DEBUG_CLEAR_MEM_CHECK_POINTS();
00188 Thread::YYield();
00189 srv.CheckMem();
00190 buf.CheckMem();
00191 DEBUG_DUMP_MEM_LEAKS();
00192 UNIT_ASSERT_MEM_NOTED("socketTest");
00193 srv.IPortListener_OnStop();
00194
00195 Thread::YYield();
00196 DEBUG_CLEAR_MEM_CHECK_POINTS();
00197 buf.CheckMem();
00198 srv.CheckMem();
00199 UNIT_ASSERT_MEM_NOTED("socketTest");
00200 }
00201
00202 static void testSS2()
00203 {
00204 _debug_msgcount = 0;
00205 Array<byte> buf(255);
00206 ReflectorStServer srv;
00207 srv.Start();
00208 Thread::YYield();
00209
00210 DEBUG_CLEAR_MEM_CHECK_POINTS();
00211 srv.CheckMem();
00212 buf.CheckMem();
00213 UNIT_ASSERT_MEM_NOTED("socketTest2");
00214
00215 TcpSocket cnt( "127.0.0.1", 8011 );
00216 cnt.SetBlocking();
00217 cnt.Connect();
00218 int count = cnt.GetStream()->Read( buf, 0, 255 );
00219
00220 Thread::YYield();
00221 DEBUG_CLEAR_MEM_CHECK_POINTS();
00222 srv.CheckMem();
00223 buf.CheckMem();
00224 UNIT_ASSERT_MEM_NOTED("socketTest2");
00225
00226 UNIT_TEST( "socketTest2 H", buf[0] == 'H' );
00227 UNIT_TEST( "socketTest2 I", buf[1] == 'I' );
00228
00229 TcpSocket cnt2( "127.0.0.1", 8011 );
00230 cnt2.SetBlocking();
00231 cnt.Connect();
00232 buf[0] = 'X';
00233 count = cnt2.GetStream()->Read( buf, 0, 255 );
00234
00235 UNIT_TEST( "socketTest2a H", buf[0] == 'H' );
00236 UNIT_TEST( "socketTest2a I", buf[1] == 'I' );
00237
00238 DEBUG_CLEAR_MEM_CHECK_POINTS();
00239 srv.CheckMem();
00240 buf.CheckMem();
00241 UNIT_ASSERT_MEM_NOTED("socketTest2");
00242
00243 buf[0] = 'Q';
00244 buf[1] = '\0';
00245 cnt.GetStream()->Write( buf, 0, 2 );
00246 buf[0] = 'A';
00247 count = cnt.GetStream()->Read( buf, 0, 255 );
00248 UNIT_TEST( "socketTest2 Q", buf[0] == 'Q' );
00249 UNIT_TEST( "socketTest2 msg count", _debug_msgcount == 1 );
00250
00251 DEBUG_CLEAR_MEM_CHECK_POINTS();
00252 srv.CheckMem();
00253 buf.CheckMem();
00254 UNIT_ASSERT_MEM_NOTED("socketTest2");
00255
00256 buf[0] = 'Q';
00257 buf[1] = '\0';
00258 cnt2.GetStream()->Write( buf, 0, 2 );
00259 buf[0] = 'A';
00260 count = cnt2.GetStream()->Read( buf, 0, 255 );
00261 UNIT_TEST( "socketTest2a Q", buf[0] == 'Q' );
00262 UNIT_TEST( "socketTest2a msg count", _debug_msgcount == 2 );
00263
00264 cnt2.Close();
00265
00266 DEBUG_CLEAR_MEM_CHECK_POINTS();
00267 srv.CheckMem();
00268 buf.CheckMem();
00269 UNIT_ASSERT_MEM_NOTED("socketTest2");
00270
00271 buf[0] = 'Q';
00272 buf[1] = '\0';
00273 cnt.GetStream()->Write( buf, 0, 2 );
00274 buf[0] = 'A';
00275 count = cnt.GetStream()->Read( buf, 0, 255 );
00276 UNIT_TEST( "socketTest2b Q", buf[0] == 'Q' );
00277 UNIT_TEST( "socketTest2b msg count", _debug_msgcount == 3 );
00278
00279 Thread::YYield();
00280 cnt.Close();
00281 Thread::YYield();
00282
00283 DEBUG_CLEAR_MEM_CHECK_POINTS();
00284 srv.CheckMem();
00285 buf.CheckMem();
00286 UNIT_ASSERT_MEM_NOTED("socketTest2");
00287
00288 srv.IPortListener_OnStop();
00289
00290 DEBUG_CLEAR_MEM_CHECK_POINTS();
00291 srv.CheckMem();
00292 buf.CheckMem();
00293 UNIT_ASSERT_MEM_NOTED("socketTest2");
00294 }
00295
00296 void socketSetTestHarness()
00297 {
00298 testSS1();
00299 testSS2();
00300 }
00301 #endif