00001
00002
00003
00004 #include "stdafx.h"
00005 #include "TransClient.h"
00006 #include "ConnectDialog.h"
00007 #include "ProfileDialog.h"
00008 #include "globalvar.h"
00009 #include <winsock.h>
00010
00011 #include "MainFrm.h"
00012
00013 #ifdef _DEBUG
00014 #define new DEBUG_NEW
00015 #undef THIS_FILE
00016 static char THIS_FILE[] = __FILE__;
00017 #endif
00018
00019 #define VIDEO_WIDTH 150 //the width of the combo box
00020 #define IDC_SNAP_COMBO 100
00021
00022 extern char ServerAddress[20];
00023 extern CClient m_client;
00024 extern HWND pPlayer;
00025
00027
00028
00029 IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
00030
00031 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
00032
00033 ON_WM_CREATE()
00034 ON_COMMAND(IDM_CONNECT, OnConnect)
00035 ON_COMMAND(IDM_DISCONNECT, OnDisconnect)
00036 ON_COMMAND(IDM_STOP, OnStop)
00037 ON_COMMAND(IDM_SETTING, OnSetting)
00038 ON_COMMAND(IDM_REQUEST, OnRequest)
00039 ON_COMMAND(ID_APP_EXIT, OnAppExit)
00040
00041 END_MESSAGE_MAP()
00042
00043 static UINT indicators[] =
00044 {
00045 ID_SEPARATOR,
00046 ID_INDICATOR_CAPS,
00047 ID_INDICATOR_NUM,
00048 ID_INDICATOR_SCRL,
00049 };
00050
00052
00053
00054 CMainFrame::CMainFrame()
00055 {
00056
00057 m_SocketIsAvailable = false;
00058 VideoIsRequested = false;
00059 }
00060
00061 CMainFrame::~CMainFrame()
00062 {
00063 }
00064
00065 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
00066 {
00067 int index;
00068 CRect rect;
00069
00070 if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
00071 return -1;
00072
00073 if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
00074 | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
00075 !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
00076 {
00077 TRACE0("Failed to create toolbar\n");
00078 return -1;
00079 }
00080
00081 if (!m_wndStatusBar.Create(this) ||
00082 !m_wndStatusBar.SetIndicators(indicators,
00083 sizeof(indicators)/sizeof(UINT)))
00084 {
00085 TRACE0("Failed to create status bar\n");
00086 return -1;
00087 }
00088
00089
00090
00091 m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
00092 EnableDocking(CBRS_ALIGN_ANY);
00093 DockControlBar(&m_wndToolBar);
00094
00095
00096
00097
00098
00099
00100 index = 0;
00101
00102 while(m_wndToolBar.GetItemID(index)!=ID_COMBO)
00103 index++;
00104
00105
00106 m_wndToolBar.SetButtonInfo(index, ID_COMBO, TBBS_SEPARATOR, VIDEO_WIDTH);
00107 m_wndToolBar.GetItemRect(index, &rect);
00108
00109
00110 rect.top += 7;
00111 rect.bottom += 100;
00112
00113
00114 if (!m_wndToolBar.m_wndVideo.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_AUTOHSCROLL |
00115 CBS_DROPDOWNLIST | CBS_HASSTRINGS | CBS_SORT,
00116 rect, &m_wndToolBar, IDC_SNAP_COMBO))
00117 {
00118 TRACE0("Failed to create combo-box\n");
00119 return FALSE;
00120 }
00121 m_wndToolBar.m_wndVideo.ShowWindow(SW_SHOW);
00122
00123 return 0;
00124 }
00125
00126 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
00127 {
00128 cs.style = WS_OVERLAPPED | WS_SYSMENU | WS_BORDER;
00129 if( !CFrameWnd::PreCreateWindow(cs) )
00130 return FALSE;
00131
00132
00133
00134 return TRUE;
00135 }
00136
00138
00139
00140 #ifdef _DEBUG
00141 void CMainFrame::AssertValid() const
00142 {
00143 CFrameWnd::AssertValid();
00144 }
00145
00146 void CMainFrame::Dump(CDumpContext& dc) const
00147 {
00148 CFrameWnd::Dump(dc);
00149 }
00150
00151 #endif //_DEBUG
00152
00154
00155
00156 void CMainFrame::OnConnect()
00157 {
00158
00159 char temp[20];
00160 char inbuf[100];
00161 int error = 0;
00162
00163 if(m_SocketIsAvailable)
00164 {
00165 MessageBox("Connection has been created!","Connect",MB_OK);
00166 return;
00167 }
00168
00169 WSADATA wsd;
00170
00171 if(WSAStartup(0x0101,&wsd)!=0)
00172 {
00173 MessageBox("Unable to start socket.","Socket Error",MB_OK);
00174 return;
00175 }
00176 else
00177 {
00178 try
00179 {
00180 m_client.m_ControlSock.Cleanup();
00181 m_client.m_ControlSock.Create(SOCK_STREAM);
00182 }
00183 catch(const char* e)
00184 {
00185 MessageBox(e,"Socket Error",MB_OK);
00186 return;
00187 }
00188 }
00189
00190 CConnectDialog ConnectDlg;
00191
00192 if(ConnectDlg.DoModal()==IDOK)
00193 {
00194 try
00195 {
00196 if(ConnectDlg.m_LocalHost==0)
00197 {
00198 gethostname ( temp, 20 );
00199
00200
00201 m_client.m_ControlSock.Connect(m_client.m_ControlSock.GetHostByName(temp,23));
00202 sprintf(ServerAddress, "%s\n", "127.0.0.1");
00203 }
00204 else
00205 {
00206 sprintf(temp,"%d.%d.%d.%d",ConnectDlg.m_nAddr[0],ConnectDlg.m_nAddr[1],ConnectDlg.m_nAddr[2],ConnectDlg.m_nAddr[3]);
00207 CSockAddr saServerCtrl(temp,23);
00208 m_client.m_ControlSock.Connect(saServerCtrl);
00209 sprintf(ServerAddress, "%s\n", temp);
00210 }
00211
00212 m_client.m_ControlSock.Print("connect");
00213
00214
00215 m_client.m_ControlSock.ReadLine(inbuf,sizeof inbuf,1800);
00216
00217 while(stricmp(inbuf,"end"))
00218 {
00219 AddVideo(inbuf);
00220 m_client.m_ControlSock.ReadLine(inbuf,sizeof inbuf,1800);
00221 }
00222 m_SocketIsAvailable = true;
00223 }
00224 catch(const char* e)
00225 {
00226 m_client.m_ControlSock.Cleanup();
00227 MessageBox(e,"Socket Error",MB_OK);
00228 m_SocketIsAvailable = false;
00229 return;
00230 }
00231 }
00232 }
00233
00234 void CMainFrame::OnDisconnect()
00235 {
00236
00237 char inbuf[100];
00238 if(m_SocketIsAvailable)
00239 {
00240
00241 try
00242 {
00243 m_client.m_ControlSock.Print("break");
00244
00245 m_client.m_ControlSock.ReadLine(inbuf,sizeof inbuf,1800);
00246 if(!stricmp(inbuf,"end"))
00247 {
00248
00249 m_client.m_ControlSock.Cleanup();
00250 m_SocketIsAvailable = false;
00251 VideoIsRequested = false;
00252 m_wndToolBar.m_wndVideo.ResetContent();
00253 }
00254 else
00255 {
00256 MessageBox("Handshake error","Client",MB_OK);
00257 return;
00258 }
00259 }
00260 catch(const char* e)
00261 {
00262 m_client.m_ControlSock.Cleanup();
00263 MessageBox(e,"Socket Error",MB_OK);
00264 m_SocketIsAvailable = false;
00265 VideoIsRequested = false;
00266 m_wndToolBar.m_wndVideo.ResetContent();
00267 return;
00268 }
00269 }
00270 return;
00271 }
00272
00273 void CMainFrame::OnStop()
00274 {
00275
00276 char inbuf[100];
00277 if(m_SocketIsAvailable)
00278 {
00279
00280 try
00281 {
00282 m_client.m_ControlSock.Print("stop");
00283
00284 m_client.m_ControlSock.ReadLine(inbuf,sizeof inbuf,1800);
00285 if(!stricmp(inbuf,"end"))
00286 {
00287 VideoIsRequested = false;
00288 }
00289 else
00290 {
00291 MessageBox("Handshake error","Client",MB_OK);
00292 return;
00293 }
00294 }
00295 catch(const char* e)
00296 {
00297 m_client.m_ControlSock.Cleanup();
00298 MessageBox(e,"Socket Error",MB_OK);
00299 m_SocketIsAvailable = false;
00300 VideoIsRequested = false;
00301
00302 return;
00303 }
00304 }
00305 return;
00306 }
00307
00308 void CMainFrame::OnSetting()
00309 {
00310
00311 CProfileDialog ProfileDlg(NULL, &m_client.m_profile);
00312 if(ProfileDlg.DoModal()==IDOK)
00313 {
00314 m_client.SaveProfile();
00315 }
00316 }
00317
00318 void CMainFrame::OnRequest()
00319 {
00320
00321 char inbuf[100];
00322 if(VideoIsRequested)
00323 {
00324 MessageBox("You have already requested a video.","",MB_OK);
00325 return;
00326 }
00327
00328
00329 char temp[100];
00330 char fn[100];
00331
00332 if(m_wndToolBar.m_wndVideo.GetLBText(m_wndToolBar.m_wndVideo.GetCurSel(), fn)!=CB_ERR)
00333 {
00334 if(m_SocketIsAvailable)
00335 {
00336
00337
00338
00339 sprintf(temp,"profile:%d:%d:%d:%d",m_client.m_profile.FrameSize,
00340 m_client.m_profile.FrameRate,m_client.m_profile.color,m_client.m_profile.BitRate);
00341
00342 m_client.m_ControlSock.Print(temp);
00343
00344 m_client.m_ControlSock.ReadLine(inbuf, sizeof inbuf, 1000);
00345 if(stricmp(inbuf,"end"))
00346 {
00347 MessageBox("Handshake error","Client",MB_OK);
00348 return;
00349 }
00350
00351
00352
00353 sprintf(temp,"request:");
00354 strcat(temp, fn);
00355 try
00356 {
00357 m_client.m_ControlSock.Print(temp);
00358 m_client.m_ControlSock.ReadLine(inbuf,sizeof inbuf,1000);
00359
00360 m_client.m_DataPort = atoi(inbuf);
00361
00362 m_client.m_ControlSock.ReadLine(inbuf,sizeof inbuf,1000);
00363
00364 if(!stricmp(inbuf,"end"))
00365 {
00366 m_DecodingThread = new CDecodingThread(&m_client);
00367 m_DecodingThread->m_bAutoDelete = true;
00368 m_DecodingThread->CreateThread();
00369 VideoIsRequested = true;
00370 }
00371 else
00372 {
00373 MessageBox("Handshake error","Client",MB_OK);
00374 return;
00375 }
00376 }
00377 catch(const char* e)
00378 {
00379 return;
00380 }
00381 }
00382 }
00383 return;
00384 }
00385
00386 void CMainFrame::AddVideo(const char *video)
00387 {
00388 m_wndToolBar.m_wndVideo.AddString(video);
00389 }
00390
00391 void CMainFrame::OnAppExit()
00392 {
00393
00394 m_wndToolBar.m_wndVideo.ResetContent();
00395 m_client.m_ControlSock.Cleanup();
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 DestroyWindow();
00423 }