private void Shutdown(O2GSession session, StatusListener statusListener, TableListener tableListener, O2GTableManager tableManager) { if (tableListener != null) { tableListener.UnsubscribeEvents(tableManager); } if (statusListener != null) { if (session != null) { session.logout(); } statusListener.WaitEvents(); } }
private void DoWork() { O2GSession session = null; O2GTableManager tableManager = null; StatusListener statusListener = null; TableListener tableListener = null; bool canUnsubscribeSessionStatus = false; try { session = O2GTransport.createSession(); session.useTableManager(O2GTableManagerMode.Yes, null); var sessionId = Guid.NewGuid().ToString("N"); statusListener = new StatusListener(session, sessionId); statusListener.OnStatus += (s, e) => { var status = e.Item.ToStatus(); OnStatus?.Invoke(this, new GenericArgs <Status>(status)); if (status == Status.Disconnected) { OnDisconnected?.Invoke(this, EventArgs.Empty); } }; session.subscribeSessionStatus(statusListener); session.login(userName, password, URL, connection.ToString()); if (statusListener.WaitEvents() && statusListener.Connected) { tableListener = new TableListener(testingMode); tableListener.OnOffer += (s, e) => OnOffer?.Invoke(this, new GenericArgs <Offer>(e.Item)); // improve this plus get rid of response listener tableListener.OnOfferIds += (s, e) => { //var request = GetSetSubscriptionStatusRequest(session, e.Item); //if (request == null) // throw new Exception("Cannot create request"); //var responseListener = new ResponseListener(); //session.subscribeResponse(responseListener); //responseListener.SetRequestID(request.RequestID); //session.sendRequest(request); //if (!responseListener.WaitEvents()) // throw new Exception("Response waiting timeout expired"); }; tableManager = session.getTableManager(); var managerStatus = tableManager.getStatus(); // max wait while (managerStatus == O2GTableManagerStatus.TablesLoading) { Thread.Sleep(50); managerStatus = tableManager.getStatus(); } if (managerStatus == O2GTableManagerStatus.TablesLoadFailed) { throw new Exception("TableManager refresh failed!"); } tableListener.SetSymbols( Enum.GetValues(typeof(Symbol)).Cast <Symbol>().ToList()); tableListener.SubscribeEvents(tableManager); O2GOffersTable offers = null; offers = (O2GOffersTable)tableManager.getTable(O2GTableType.Offers); tableListener.HandleOffers(offers); cancelEvent.WaitOne(); OnCancelled?.Invoke(this, EventArgs.Empty); } } catch (Exception error) { OnError?.Invoke(this, new GenericArgs <Exception>(error)); } finally { Shutdown(session, statusListener, tableListener, tableManager); if (canUnsubscribeSessionStatus) { session.unsubscribeSessionStatus(statusListener); } if (session != null) { session.Dispose(); } } }