]> git.sesse.net Git - casparcg/blob - protocol/util/SocketInfo.cpp
2.1.0: -common: Restructured files. Moved most files into root for easier inclusion...
[casparcg] / protocol / util / SocketInfo.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Nicklas P Andersson\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 #include "SocketInfo.h"\r
24 #include "AsyncEventServer.h"\r
25 \r
26 namespace caspar {\r
27 namespace IO {\r
28 \r
29 SocketInfo::SocketInfo(SOCKET socket, AsyncEventServer* pServer) : socket_(socket), pServer_(pServer), recvLeftoverOffset_(0), currentlySendingOffset_(0)\r
30 {\r
31         event_ = WSACreateEvent();\r
32         if(event_ == WSA_INVALID_EVENT) {\r
33                 throw std::exception("Failed to create WSAEvent");\r
34         }\r
35 }\r
36 \r
37 SocketInfo::~SocketInfo() {\r
38 \r
39         WSACloseEvent(event_);\r
40         event_ = 0;\r
41 \r
42         closesocket(socket_);\r
43         socket_ = 0;\r
44 }\r
45 \r
46 void SocketInfo::Send(const std::wstring& data) {\r
47         if(pServer_ != 0 && data.length() > 0) {\r
48                 {\r
49                         //The lock has to be let go before DoSend is called since that too tries to lock to object\r
50                         tbb::mutex::scoped_lock lock(mutex_);\r
51                         sendQueue_.push(data);\r
52                 }\r
53 \r
54                 pServer_->DoSend(*this);\r
55         }\r
56 }\r
57 \r
58 void SocketInfo::Disconnect() {\r
59         if(pServer_)\r
60                 pServer_->DisconnectClient(*this);\r
61 }\r
62 \r
63 }       //namespace IO\r
64 }       //namespace caspar