Version: 0.6.0
network.h
1 /******************************************************************************
2  * Project: wxGIS (GIS Remote)
3  * Purpose: network classes.
4  * Author: Dmitry Baryshnikov (aka Bishop), polimax@mail.ru
5  ******************************************************************************
6 * Copyright (C) 2010,2012-2014 Dmitry Baryshnikov
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  ****************************************************************************/
21 #pragma once
22 
23 #include "wxgis/net/message.h"
24 #include "wxgis/core/pointer.h"
25 
26 #include "wx/thread.h"
27 #include "wx/socket.h"
28 
29 #define SLEEP 140
30 #define WAITFOR 500
31 #define BUFF_SIZE 16384
32 #define USE_STREAMS
33 
34 class WXDLLIMPEXP_GIS_NET INetConnection;
35 
42 class WXDLLIMPEXP_GIS_NET wxNetReaderThread : public wxThread
43 {
44 public:
45  wxNetReaderThread(INetConnection* pNetConnection);
46  virtual void *Entry();
47  virtual void OnExit();
48 protected:
49  INetConnection* m_pNetConnection;
50 };
51 
58 class WXDLLIMPEXP_GIS_NET wxNetWriterThread : public wxThread
59 {
60 public:
61  wxNetWriterThread(INetConnection* pNetConnection);
62  virtual void *Entry();
63  virtual void OnExit();
64 protected:
65  INetConnection* m_pNetConnection;
66 };
67 
74 class WXDLLIMPEXP_GIS_NET INetConnection :
76  public wxEvtHandler
77 {
78  DECLARE_ABSTRACT_CLASS(INetConnection)
79  friend class wxNetReaderThread;
80  friend class wxNetWriterThread;
81 public:
82  INetConnection(void);
83  virtual ~INetConnection(void);
84  virtual bool Connect(void){return false;};
85  virtual bool Disconnect(void){return false;};
86  virtual bool IsConnected(void){return m_bIsConnected;};
87  virtual void SendNetMessageAsync(const wxNetMessage & msg);
88  virtual wxNetMessage SendNetMessageSync(const wxNetMessage & msg);
89  typedef std::priority_queue< wxNetMessage, std::deque<wxNetMessage> > WXGISMSGQUEUE;
90  virtual int GetId(void) const {return m_nUserId;};
91  virtual void SetId(const int nUserId){m_nUserId = nUserId;};
92 protected:
93  bool CreateAndRunThreads(void);
94  void DestroyThreads(void);
95  virtual bool ProcessOutputNetMessage(void);
96  virtual bool ProcessInputNetMessage(void);
97 protected:
98  WXGISMSGQUEUE m_aoMessages;
99  int m_nUserId; //user ID for server, and -1 for client
100  wxCriticalSection m_dataCS; // protects field above
101  wxCriticalSection m_msgCS; // protects field above
102  bool m_bIsConnected, m_bIsConnecting;
103  wxSocketBase* m_pSock;//TODO: should be something universal XMPP, TCP, etc.
104  wxNetWriterThread* m_pOutThread;
105  wxNetReaderThread* m_pInThread;
106  wxVector<wxNetMessage> m_oaSyncMessages;
107  wxArrayLong m_laWaitIds;
108  char m_Buffer[BUFF_SIZE];
109 };
110 
111 bool WXDLLIMPEXP_GIS_NET SendUDP(IPaddress addr, wxNetMessage & msg, bool broadcast);
112 wxString WXDLLIMPEXP_GIS_NET GetSocketErrorMsg(int pSockError);
Definition: network.h:42
Definition: pointer.h:34
Definition: network.h:74
Definition: message.h:42
Definition: network.h:58