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
21 #include "inetcomm_private.h"
25 static const WCHAR wszClassName
[] = {'T','h','o','r','C','o','n','n','W','n','d','C','l','a','s','s',0};
27 #define IX_READ (WM_USER + 0)
28 #define IX_READLINE (WM_USER + 1)
29 #define IX_WRITE (WM_USER + 2)
31 HRESULT
InternetTransport_Init(InternetTransport
*This
)
33 This
->pCallback
= NULL
;
34 This
->Status
= IXP_DISCONNECTED
;
36 This
->fCommandLogging
= FALSE
;
37 This
->fnCompletion
= NULL
;
42 HRESULT
InternetTransport_GetServerInfo(InternetTransport
*This
, LPINETSERVER pInetServer
)
44 if (This
->Status
== IXP_DISCONNECTED
)
45 return IXP_E_NOT_CONNECTED
;
47 *pInetServer
= This
->ServerInfo
;
51 HRESULT
InternetTransport_InetServerFromAccount(InternetTransport
*This
,
52 IImnAccount
*pAccount
, LPINETSERVER pInetServer
)
54 FIXME("(%p, %p): stub\n", pAccount
, pInetServer
);
58 HRESULT
InternetTransport_Connect(InternetTransport
*This
,
59 LPINETSERVER pInetServer
, boolean fAuthenticate
, boolean fCommandLogging
)
62 struct addrinfo
*ai_cur
;
63 struct addrinfo hints
;
67 if (This
->Status
!= IXP_DISCONNECTED
)
68 return IXP_E_ALREADY_CONNECTED
;
70 This
->ServerInfo
= *pInetServer
;
71 This
->fCommandLogging
= fCommandLogging
;
73 This
->hwnd
= CreateWindowW(wszClassName
, wszClassName
, 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, 0);
75 return HRESULT_FROM_WIN32(GetLastError());
76 SetWindowLongPtrW(This
->hwnd
, GWLP_USERDATA
, (LONG_PTR
)This
);
79 hints
.ai_family
= PF_UNSPEC
;
80 hints
.ai_socktype
= SOCK_STREAM
;
81 hints
.ai_protocol
= IPPROTO_TCP
;
84 hints
.ai_canonname
= NULL
;
87 snprintf(szPort
, sizeof(szPort
), "%d", (unsigned short)pInetServer
->dwPort
);
89 InternetTransport_ChangeStatus(This
, IXP_FINDINGHOST
);
91 ret
= getaddrinfo(pInetServer
->szServerName
, szPort
, &hints
, &ai
);
94 ERR("getaddrinfo failed: %d\n", ret
);
95 return IXP_E_CANT_FIND_HOST
;
98 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai
->ai_next
)
102 if (TRACE_ON(inetcomm
))
106 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
107 host
, sizeof(host
), service
, sizeof(service
),
108 NI_NUMERICHOST
| NI_NUMERICSERV
);
109 TRACE("trying %s:%s\n", host
, service
);
112 InternetTransport_ChangeStatus(This
, IXP_CONNECTING
);
114 so
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
117 WARN("socket() failed\n");
122 /* FIXME: set to async */
124 if (0 > connect(This
->Socket
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
126 WARN("connect() failed\n");
127 closesocket(This
->Socket
);
130 InternetTransport_ChangeStatus(This
, IXP_CONNECTED
);
132 /* FIXME: call WSAAsyncSelect */
135 TRACE("connected\n");
141 return IXP_E_CANT_FIND_HOST
;
144 HRESULT
InternetTransport_HandsOffCallback(InternetTransport
*This
)
146 if (!This
->pCallback
)
149 ITransportCallback_Release(This
->pCallback
);
150 This
->pCallback
= NULL
;
155 HRESULT
InternetTransport_DropConnection(InternetTransport
*This
)
157 if (This
->Status
== IXP_DISCONNECTED
)
158 return IXP_E_NOT_CONNECTED
;
160 shutdown(This
->Socket
, SD_BOTH
);
162 closesocket(This
->Socket
);
164 DestroyWindow(This
->hwnd
);
167 InternetTransport_ChangeStatus(This
, IXP_DISCONNECTED
);
172 HRESULT
InternetTransport_GetStatus(InternetTransport
*This
,
173 IXPSTATUS
*pCurrentStatus
)
175 *pCurrentStatus
= This
->Status
;
179 HRESULT
InternetTransport_ChangeStatus(InternetTransport
*This
, IXPSTATUS Status
)
181 This
->Status
= Status
;
183 ITransportCallback_OnStatus(This
->pCallback
, Status
,
184 (IInternetTransport
*)&This
->u
.vtbl
);
188 HRESULT
InternetTransport_ReadLine(InternetTransport
*This
,
189 INETXPORT_COMPLETION_FUNCTION fnCompletion
)
191 if (This
->Status
== IXP_DISCONNECTED
)
192 return IXP_E_NOT_CONNECTED
;
194 if (This
->fnCompletion
)
197 This
->fnCompletion
= fnCompletion
;
199 This
->cbBuffer
= 1024;
200 This
->pBuffer
= HeapAlloc(GetProcessHeap(), 0, This
->cbBuffer
);
201 This
->iCurrentBufferOffset
= 0;
203 if (WSAAsyncSelect(This
->Socket
, This
->hwnd
, IX_READLINE
, FD_READ
) == SOCKET_ERROR
)
205 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
206 /* FIXME: handle error */
211 HRESULT
InternetTransport_Write(InternetTransport
*This
, const char *pvData
,
212 int cbSize
, INETXPORT_COMPLETION_FUNCTION fnCompletion
)
216 if (This
->Status
== IXP_DISCONNECTED
)
217 return IXP_E_NOT_CONNECTED
;
219 if (This
->fnCompletion
)
222 /* FIXME: do this asynchronously */
223 ret
= send(This
->Socket
, pvData
, cbSize
, 0);
224 if (ret
== SOCKET_ERROR
)
226 ERR("send failed with error %d\n", WSAGetLastError());
227 /* FIXME: handle error */
230 fnCompletion((IInternetTransport
*)&This
->u
.vtbl
, NULL
, 0);
235 HRESULT
InternetTransport_DoCommand(InternetTransport
*This
,
236 LPCSTR pszCommand
, INETXPORT_COMPLETION_FUNCTION fnCompletion
)
238 if (This
->Status
== IXP_DISCONNECTED
)
239 return IXP_E_NOT_CONNECTED
;
241 if (This
->fnCompletion
)
244 if (This
->pCallback
&& This
->fCommandLogging
)
246 ITransportCallback_OnCommand(This
->pCallback
, CMD_SEND
, (LPSTR
)pszCommand
, 0,
247 (IInternetTransport
*)&This
->u
.vtbl
);
249 return InternetTransport_Write(This
, pszCommand
, strlen(pszCommand
), fnCompletion
);
252 static LRESULT CALLBACK
InternetTransport_WndProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
256 InternetTransport
*This
= (InternetTransport
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
259 if (!This
->fnCompletion
)
262 while (This
->iCurrentBufferOffset
< This
->cbBuffer
)
264 if (recv(This
->Socket
, &This
->pBuffer
[This
->iCurrentBufferOffset
], 1, 0) <= 0)
266 if (WSAGetLastError() == WSAEWOULDBLOCK
)
269 ERR("recv failed with error %d\n", WSAGetLastError());
270 /* FIXME: handle error */
273 This
->iCurrentBufferOffset
++;
275 if (This
->iCurrentBufferOffset
== This
->cbBuffer
)
277 INETXPORT_COMPLETION_FUNCTION fnCompletion
= This
->fnCompletion
;
280 This
->fnCompletion
= NULL
;
281 pBuffer
= This
->pBuffer
;
282 This
->pBuffer
= NULL
;
283 fnCompletion((IInternetTransport
*)&This
->u
.vtbl
, pBuffer
,
284 This
->iCurrentBufferOffset
);
285 HeapFree(GetProcessHeap(), 0, pBuffer
);
289 if (WSAAsyncSelect(This
->Socket
, hwnd
, uMsg
, FD_READ
) == SOCKET_ERROR
)
291 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
292 /* FIXME: handle error */
296 else if (uMsg
== IX_READLINE
)
298 InternetTransport
*This
= (InternetTransport
*)GetWindowLongPtrW(hwnd
, GWLP_USERDATA
);
301 if (!This
->fnCompletion
)
304 while (This
->iCurrentBufferOffset
< This
->cbBuffer
- 1)
308 if (recv(This
->Socket
, &This
->pBuffer
[This
->iCurrentBufferOffset
], 1, 0) <= 0)
310 if (WSAGetLastError() == WSAEWOULDBLOCK
)
313 ERR("recv failed with error %d\n", WSAGetLastError());
314 /* FIXME: handle error */
318 if (This
->pBuffer
[This
->iCurrentBufferOffset
] == '\n')
320 INETXPORT_COMPLETION_FUNCTION fnCompletion
= This
->fnCompletion
;
323 This
->fnCompletion
= NULL
;
324 This
->pBuffer
[This
->iCurrentBufferOffset
++] = '\0';
325 pBuffer
= This
->pBuffer
;
326 This
->pBuffer
= NULL
;
328 fnCompletion((IInternetTransport
*)&This
->u
.vtbl
, pBuffer
,
329 This
->iCurrentBufferOffset
);
331 HeapFree(GetProcessHeap(), 0, pBuffer
);
334 if (This
->pBuffer
[This
->iCurrentBufferOffset
] != '\r')
335 This
->iCurrentBufferOffset
++;
338 FD_SET(This
->Socket
, &infd
);
340 if (This
->iCurrentBufferOffset
== This
->cbBuffer
- 1)
343 if (WSAAsyncSelect(This
->Socket
, hwnd
, uMsg
, FD_READ
) == SOCKET_ERROR
)
345 ERR("WSAAsyncSelect failed with error %d\n", WSAGetLastError());
346 /* FIXME: handle error */
351 return DefWindowProcW(hwnd
, uMsg
, wParam
, lParam
);
354 BOOL
InternetTransport_RegisterClass(HINSTANCE hInstance
)
359 if (WSAStartup(MAKEWORD(2, 2), &wsadata
))
362 memset(&cls
, 0, sizeof(cls
));
363 cls
.hInstance
= hInstance
;
364 cls
.lpfnWndProc
= InternetTransport_WndProc
;
365 cls
.lpszClassName
= wszClassName
;
367 return RegisterClassW(&cls
);
370 void InternetTransport_UnregisterClass(HINSTANCE hInstance
)
372 UnregisterClassW(wszClassName
, hInstance
);