Hi, I'm working with ICS and BCB6
All works fine but sometimes I don't receive OnDataAvailable event anymore. In attach the code. Please could you help me Regards Ing. Alessandro Miorelli Software Engineer
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "ThreadSocket.h" #include <syncobjs.hpp> #include "my_class.h" #include "libGenFunction.h" #include "Debug.h" #pragma package(smart_init) #define _WIN32_DCOM __fastcall SocketThread::SocketThread(bool CreateSuspended, TRace* race, AnsiString threadName, int idDevice) : TThread(CreateSuspended) { _race = race; _connection = NULL; _idDevice = idDevice; _deviceType = 0; E = NULL; sprintf(ThreadName, threadName.c_str()); } //--------------------------------------------------------------------------- __fastcall SocketThread::~SocketThread() { delete device; } //--------------------------------------------------------------------------- void SocketThread::SetName() { THREADNAME_INFO info; info.dwType = 0x1000; info.szName = ThreadName; info.dwThreadID = -1; info.dwFlags = 0; __try { RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info); } __except (EXCEPTION_CONTINUE_EXECUTION) { } } //--------------------------------------------------------------------------- #define MAX_TIMEOUT 100 void __fastcall SocketThread::Execute() { SetName(); CoInitialize(NULL); _connection = TRace::GetConnection(_race->DatabaseType, _race->DatabasePath, _race->DatabaseFailoverPath, _race->DatabaseInitialCatalog, _race->DatabasePassword, _race->UserID); device = new Device(_connection, _race, DA_DISPOSITIVO_LAN); // Create the TWSocket we will use to commicate with the server FWSocket = new TWSocket((void *)NULL); // FWSocket->ThreadAttach(); FWSocket->MultiThreaded = true; FWSocket->SendFlags = TSocketSendFlags() << wsSendUrgent; FWSocket->OnDataAvailable = FWSocketDataAvailable; FWSocket->OnSessionClosed = FWSocketSessionClosed; FWSocket->OnSessionConnected = FWSocketSessionConnected; FWSocket->OnSocksError = FWSocketSocksError; FWSocket->ComponentOptions = TWSocketOptions() << wsoTcpNoDelay; FWSocket->OnBgException = FWSocketBackgroundException; // Connect to the server FWSocket->Addr = _serverHostName; FWSocket->Port = _serverPort; FWSocket->Proto = "tcp"; FWSocket->Connect(); ClassStringData *StringDataReceived; MSG msg; char *infoToSend; int length; int timeout = 0; msg.message = 0; while (!Terminated && msg.message != WM_CLOSE) { if (GetMessageA(&msg, NULL, NULL, NULL) && msg.message == WM_STRING_TO_SEND) { __try { StringDataReceived = (ClassStringData *)msg.lParam; StringDataReceived->ReadStringData(&infoToSend, &length); int dataSent = 0; int totalDataSent = 0; if (!Terminated && FWSocket->State == wsConnected) { do { dataSent = FWSocket->Send(&infoToSend[totalDataSent], length - totalDataSent); if (dataSent < 0) { Sleep(1); if (timeout < MAX_TIMEOUT) timeout++; } else { totalDataSent += dataSent; if (length != totalDataSent) { Sleep(1); if (timeout < MAX_TIMEOUT) timeout++; } else { timeout = 0; } } } while (length != totalDataSent && FWSocket->State == wsConnected && !Terminated && timeout < MAX_TIMEOUT); } } catch (Exception &exception) { } delete(ClassStringData *)StringDataReceived; } else if (msg.message != WM_CLOSE) { //FWSocket->ProcessMessage(); TranslateMessage(&msg); DispatchMessage(&msg); } } if(FWSocket->State == wsConnected) FWSocket->Close(); // We are done, destroy the objects we created delete FWSocket; CoUninitialize(); } //--------------------------------------------------------------------------- void __fastcall SocketThread::HandleException() { Debug::ProcessExceptionMessage(E, "ThreadSocket", IDException, true); } //--------------------------------------------------------------------------- // This event handler is called by the TWSocket when some data has been // received by the lower level. void __fastcall SocketThread::FWSocketDataAvailable(TObject *Sender, WORD Error) { if(Error != 0) { Debug::DebugString(AnsiString("FWSocketDataAvailable Error ") + IntToStr(Error)); return; } int length = FWSocket->RcvdCount; if(length == 0) { Debug::DebugString(AnsiString("FWSocketDataAvailable length ") + IntToStr(length)); return; } char *str = new char[length + 1]; int dataReceived; int totalDataReceived = 0; int timeout = 0; do { Debug::DebugString(AnsiString("Wait dataReceived")); dataReceived = FWSocket->Receive(&str[totalDataReceived], length - totalDataReceived); if (dataReceived < 0) { Debug::DebugString(AnsiString("dataReceived < 0")); Sleep(1); if (timeout < MAX_TIMEOUT) timeout++; } else { Debug::DebugString(AnsiString("dataReceived: ") + IntToStr(dataReceived)); totalDataReceived += dataReceived; if (length != totalDataReceived) { Sleep(1); if (timeout < MAX_TIMEOUT) { timeout++; Debug::DebugString(AnsiString("timeout: ") + IntToStr(timeout)); } } else { timeout = 0; } } str[totalDataReceived] = 0; Debug::DebugString(AnsiString("Received: ") + AnsiString(str)); } while (length != totalDataReceived && FWSocket->State == wsConnected && !Terminated && timeout < MAX_TIMEOUT); if(timeout == 0) { str[length] = 0; Debug::DebugString(AnsiString("Received: ") + AnsiString(str)); if (_deviceType == 0) _race->HandleInternetData(str, _idDevice); else device->ReceiveData(str, length, _deviceType - 1); } delete[] str; } //--------------------------------------------------------------------------- // This event handler is called by TWSocket when the connection is // established with the remote host void __fastcall SocketThread::FWSocketSessionConnected(TObject *Sender, WORD Error) { Debug::DebugString(AnsiString("SocketSessionConnected: ") + IntToStr(Error)); if(Error != 0) { AnsiString text = "Socks error #" + IntToStr(Error); ClassStringData *stringData = new ClassStringData(text); PostMessage(_race->FormClientHandle, WM_DEVICE_ERROR, _idDevice, (long)stringData); } else PostMessage(_race->FormClientHandle, WM_DEVICE_CONNECTED, _idDevice, NULL); } //--------------------------------------------------------------------------- // This event handler is called by TWSocket when the connection is broken void __fastcall SocketThread::FWSocketSessionClosed(TObject *Sender, WORD Error) { Debug::DebugString(AnsiString("FWSocketSessionClosed: ") + IntToStr(Error)); FWSocket->Close(); PostMessage(_race->FormClientHandle, WM_DEVICE_DISCONNECTED, _idDevice, NULL); } //--------------------------------------------------------------------------- void __fastcall SocketThread::FWSocketSocksError(TObject *Sender, int Error, AnsiString Msg) { Debug::DebugString(AnsiString("FWSocketSocksError: ") + IntToStr(Error) + " " + Msg); AnsiString text = "Socks error #" + IntToStr(Error) + " " + Msg; ClassStringData *stringData = new ClassStringData(text); PostMessage(_race->FormClientHandle, WM_DEVICE_ERROR, _idDevice, (long)stringData); } //--------------------------------------------------------------------------- void SocketThread::Connect() { Suspended = false; if(FWSocket != NULL) { FWSocket->Addr = _serverHostName; FWSocket->Port = _serverPort; FWSocket->Connect(); } } //--------------------------------------------------------------------------- void SocketThread::Disconnect() { FWSocket->Close(); Suspended = true; } //--------------------------------------------------------------------------- bool SocketThread::IsConnected() { if(FWSocket == NULL || Suspended) return false; return FWSocket->State == wsConnected; } void __fastcall SocketThread::SetDeviceType(int deviceType) { _deviceType = deviceType; } void __fastcall SocketThread::FWSocketBackgroundException(TObject* Sender, Exception* E, bool& CanClose) { Debug::DebugString(AnsiString("FWSocketBackgroundException: ") + E->ClassName() + ": " + E->Message); }
-- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be