2 * Internet Messaging Transport Base Class
4 * Copyright 2006 Robert Shearman for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wine/debug.h"
34 #include "inetcomm_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
38 static const WCHAR wszClassName[] = {'T','h','o','r','C','o','n','n','W','n','d','C','l','a','s','s',0};
40 #define IX_READ (WM_USER + 0)
41 #define IX_READLINE (WM_USER + 1)
42 #define IX_WRITE (WM_USER + 2)
44 HRESULT InternetTransport_Init(InternetTransport *This)
46 This->pCallback = NULL;
47 This->Status = IXP_DISCONNECTED;
49 This->fCommandLogging = FALSE;
50 This->fnCompletion = NULL;
55 HRESULT InternetTransport_GetServerInfo(InternetTransport *This, LPINETSERVER pInetServer)
57 if (This->Status == IXP_DISCONNECTED)
58 return IXP_E_NOT_CONNECTED;
60 *pInetServer = This->ServerInfo;
64 HRESULT InternetTransport_InetServerFromAccount(InternetTransport *This,
65 IImnAccount *pAccount, LPINETSERVER pInetServer)
67 FIXME("(%p, %p): stub\n", pAccount, pInetServer);
71 HRESULT InternetTransport_Connect(InternetTransport *This,
72 LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
75 struct addrinfo *ai_cur;
76 struct addrinfo hints;
80 if (This->Status != IXP_DISCONNECTED)
81 return IXP_E_ALREADY_CONNECTED;
83 This->ServerInfo = *pInetServer;
84 This->fCommandLogging = fCommandLogging;
86 This->hwnd = CreateWindowW(wszClassName, wszClassName, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0);
88 return HRESULT_FROM_WIN32(GetLastError());
89 SetWindowLongPtrW(This->hwnd, GWLP_USERDATA, (LONG_PTR)This);
92 hints.ai_family = PF_UNSPEC;
93 hints.ai_socktype = SOCK_STREAM;
94 hints.ai_protocol = IPPROTO_TCP;
97 hints.ai_canonname = NULL;
100 snprintf(szPort, sizeof(szPort), "%d", (unsigned short)pInetServer->dwPort);
102 InternetTransport_ChangeStatus(This, IXP_FINDINGHOST);
104 ret = getaddrinfo(pInetServer->szServerName, szPort, &hints, &ai);
107 ERR("getaddrinfo failed: %d\n", ret);
108 return IXP_E_CANT_FIND_HOST;
111 for (ai_cur = ai; ai_cur; ai_cur = ai->ai_next)
115 if (TRACE_ON(inetcomm))
119 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
120 host, sizeof(host), service, sizeof(service),
121 NI_NUMERICHOST | NI_NUMERICSERV);
122 TRACE("trying %s:%s\n", host, service);
125 InternetTransport_ChangeStatus(This, IXP_CONNECTING);
127 so = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
130 WARN("socket() failed\n");
135 /* FIXME: set to async */
137 if (0 > connect(This->Socket, ai_cur->ai_addr, ai_cur->ai_addrlen))
139 WARN("connect() failed\n");
140 closesocket(This->Socket);
143 InternetTransport_ChangeStatus(This, IXP_CONNECTED);
145 /* FIXME: call WSAAsyncSelect */
148 TRACE("connected\n");
154 return IXP_E_CANT_FIND_HOST;
157 HRESULT InternetTransport_HandsOffCallback(InternetTransport *This)
159 if (!This->pCallback)
162 ITransportCallback_Release(This->pCallback);
163 This->pCallback = NULL;
168 HRESULT InternetTransport_DropConnection(InternetTransport *This)
172 if (This->Status == IXP_DISCONNECTED)
173 return IXP_E_NOT_CONNECTED;
175 ret = shutdown(This->Socket, SD_BOTH);
177 ret = closesocket(This->Socket);
179 DestroyWindow(This->hwnd);
182 InternetTransport_ChangeStatus(This, IXP_DISCONNECTED);
187 HRESULT InternetTransport_GetStatus(InternetTransport *This,
188 IXPSTATUS *pCurrentStatus)
190 *pCurrentStatus = This->Status;
194 HRESULT InternetTransport_ChangeStatus(InternetTransport *This, IXPSTATUS Status)
196 This->Status = Status;
198 ITransportCallback_OnStatus(This->pCallback, Status,
199 (IInternetTransport *)&This->u.vtbl);
203 HRESULT InternetTransport_ReadLine(InternetTransport *This,
204 INETXPORT_COMPLETION_FUNCTION fnCompletion)
206 if (This->Status == IXP_DISCONNECTED)
207 return IXP_E_NOT_CONNECTED;
209 if (This->fnCompletion)
212 This->fnCompletion = fnCompletion;
214 This->cbBuffer = 1024;
215 This->pBuffer = HeapAlloc(GetProcessHeap(), 0, This->cbBuffer);
216 This->iCurrentBufferOffset = 0;
218 if (WSAAsyncSelect(This->Socket, This->hwnd, IX_READLINE, FD_READ) == SOCKET_ERROR)
220 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
221 /* FIXME: handle error */
226 HRESULT InternetTransport_Write(InternetTransport *This, const char *pvData,
227 int cbSize, INETXPORT_COMPLETION_FUNCTION fnCompletion)
231 if (This->Status == IXP_DISCONNECTED)
232 return IXP_E_NOT_CONNECTED;
234 if (This->fnCompletion)
237 /* FIXME: do this asynchronously */
238 ret = send(This->Socket, pvData, cbSize, 0);
239 if (ret == SOCKET_ERROR)
241 ERR("send failed with error %d\n", WSAGetLastError());
242 /* FIXME: handle error */
245 fnCompletion((IInternetTransport *)&This->u.vtbl, NULL, 0);
250 HRESULT InternetTransport_DoCommand(InternetTransport *This,
251 LPCSTR pszCommand, INETXPORT_COMPLETION_FUNCTION fnCompletion)
253 if (This->Status == IXP_DISCONNECTED)
254 return IXP_E_NOT_CONNECTED;
256 if (This->fnCompletion)
259 if (This->pCallback && This->fCommandLogging)
261 ITransportCallback_OnCommand(This->pCallback, CMD_SEND, (LPSTR)pszCommand, 0,
262 (IInternetTransport *)&This->u.vtbl);
264 return InternetTransport_Write(This, pszCommand, strlen(pszCommand), fnCompletion);
267 static LRESULT CALLBACK InternetTransport_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
271 InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
274 if (!This->fnCompletion)
277 while (This->iCurrentBufferOffset < This->cbBuffer)
279 if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
281 if (WSAGetLastError() == WSAEWOULDBLOCK)
284 ERR("recv failed with error %d\n", WSAGetLastError());
285 /* FIXME: handle error */
288 This->iCurrentBufferOffset++;
290 if (This->iCurrentBufferOffset == This->cbBuffer)
292 INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
295 This->fnCompletion = NULL;
296 pBuffer = This->pBuffer;
297 This->pBuffer = NULL;
298 fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
299 This->iCurrentBufferOffset);
300 HeapFree(GetProcessHeap(), 0, pBuffer);
304 if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
306 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
307 /* FIXME: handle error */
311 else if (uMsg == IX_READLINE)
313 InternetTransport *This = (InternetTransport *)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
316 if (!This->fnCompletion)
319 while (This->iCurrentBufferOffset < This->cbBuffer - 1)
323 if (recv(This->Socket, &This->pBuffer[This->iCurrentBufferOffset], 1, 0) <= 0)
325 if (WSAGetLastError() == WSAEWOULDBLOCK)
328 ERR("recv failed with error %d\n", WSAGetLastError());
329 /* FIXME: handle error */
333 if (This->pBuffer[This->iCurrentBufferOffset] == '\n')
335 INETXPORT_COMPLETION_FUNCTION fnCompletion = This->fnCompletion;
338 This->fnCompletion = NULL;
339 This->pBuffer[This->iCurrentBufferOffset++] = '\0';
340 pBuffer = This->pBuffer;
341 This->pBuffer = NULL;
343 fnCompletion((IInternetTransport *)&This->u.vtbl, pBuffer,
344 This->iCurrentBufferOffset);
346 HeapFree(GetProcessHeap(), 0, pBuffer);
349 if (This->pBuffer[This->iCurrentBufferOffset] != '\r')
350 This->iCurrentBufferOffset++;
353 FD_SET(This->Socket, &infd);
355 if (This->iCurrentBufferOffset == This->cbBuffer - 1)
358 if (WSAAsyncSelect(This->Socket, hwnd, uMsg, FD_READ) == SOCKET_ERROR)
360 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
361 /* FIXME: handle error */
366 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
369 BOOL InternetTransport_RegisterClass(HINSTANCE hInstance)
374 if (WSAStartup(MAKEWORD(2, 2), &wsadata))
377 memset(&cls, 0, sizeof(cls));
378 cls.hInstance = hInstance;
379 cls.lpfnWndProc = InternetTransport_WndProc;
380 cls.lpszClassName = wszClassName;
382 return RegisterClassW(&cls);
385 void InternetTransport_UnregisterClass(HINSTANCE hInstance)
387 UnregisterClassW(wszClassName, hInstance);