Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <QDebug>
  2. #include <QMessageBox>
  3. #include <winsock2.h>
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <windows.h>
  6. #include "application.h"
  7. #include "defines.h"
  8.  
  9.  
  10. Application::Application(int argc, char** argv) :
  11.     QApplication(argc, argv)
  12. {
  13. }
  14.  
  15. bool
  16. Application::winEventFilter (MSG* msg, long*) {
  17.     SOCKET acceptedSocket = 0;
  18.     WSABUF winsockBuffer;
  19.     DWORD nReceived = 0;
  20.     DWORD nSent = 0;
  21.     DWORD flags = 0;
  22.     WSAOVERLAPPED* ol;
  23.     if (msg->message == WM_SOCKET_TCP) {
  24.         switch (WSAGETSELECTEVENT(msg->lParam)) {
  25.         case FD_ACCEPT:
  26.             if ((acceptedSocket = accept(msg->wParam, NULL, NULL)) == INVALID_SOCKET) {
  27.                 QMessageBox(QMessageBox::Critical, tr("Error"), tr("Error accepting on a socket")).exec();
  28.                 break;
  29.             }
  30.             WSAAsyncSelect(acceptedSocket, msg->hwnd, WM_SOCKET_TCP, FD_READ | FD_WRITE | FD_CLOSE);
  31.             break;
  32.         case FD_READ:
  33.             winsockBuffer.len = DATA_BUFSIZE;
  34.             winsockBuffer.buf = (char*) malloc(sizeof(char) * DATA_BUFSIZE);
  35.             ol = (WSAOVERLAPPED*) malloc(sizeof(WSAOVERLAPPED));
  36.             ol->hEvent = (HANDLE) winsockBuffer.buf;
  37.             if (WSARecv((SOCKET) msg->wParam, &winsockBuffer, 1, &nReceived, &flags, ol, readCompleteRoutine) == SOCKET_ERROR) {
  38.                 if (WSAGetLastError() != WSA_IO_PENDING) {
  39.                     qDebug() << "error with WSARecv: " << WSAGetLastError();
  40.                     return true;
  41.                 }
  42.             }
  43.             break;
  44.         case FD_WRITE:
  45.             // set up a buffer with the specified size, full of the char 'n'
  46.             winsockBuffer.len = HIWORD(msg->lParam);
  47.             winsockBuffer.buf = (char*) malloc(sizeof(char) * HIWORD(msg->lParam));
  48.             memset(winsockBuffer.buf, 'n', (int) HIWORD(msg->lParam));
  49.             // allocate space for a new overlapped structure
  50.             ol = (WSAOVERLAPPED*) malloc(sizeof(WSAOVERLAPPED));
  51.             // since the event isnt used, store the buffer there to free it in the completion routine.
  52.             ol->hEvent = (HANDLE) winsockBuffer.buf;
  53.             if (WSASend((SOCKET) msg->wParam, &winsockBuffer, 1, &nSent, flags, ol, sendCompleteRoutine) == SOCKET_ERROR) {
  54.                 if (WSAGetLastError() != WSA_IO_PENDING) {
  55.                     qDebug() << "error with WSASend: " << WSAGetLastError();
  56.                     return true;
  57.                 }
  58.             }
  59.             break;
  60.         case FD_CLOSE:
  61.             closesocket((SOCKET) msg->wParam);
  62.             break;
  63.         }
  64.         return true;
  65.     }
  66.     return false;
  67. }
  68.  
  69. void CALLBACK sendCompleteRoutine(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD) {
  70.     char* buf = (char*) lpOverlapped->hEvent;
  71.     free(buf);
  72.     free(lpOverlapped);
  73. }
  74.  
  75. void CALLBACK readCompleteRoutine(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD) {
  76.     qDebug() << "finished read" << cbTransferred;
  77.     char* buf = (char*) lpOverlapped->hEvent;
  78.     buf[cbTransferred] = '\0';
  79.     qDebug() << buf;
  80.     free(buf);
  81.     free(lpOverlapped);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement