summaryrefslogtreecommitdiffstats
path: root/kenolaba/Network.h
blob: d7390b520f212c45ffd3db15f493b608154f0f75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Simple Network Support
 * Install a listening socket; receive positions on incoming
 * connections (incoming positions are treated as if pasted in)
 */

#ifndef _NETWORK_H_
#define _NETWORK_H_

#include <sys/types.h>
#include <netinet/in.h>

#include <tqobject.h>
#include <tqptrlist.h>
#include <tqsocketnotifier.h>

class Listener {
 public:
  Listener(const char*,int,struct sockaddr_in,bool=true);
  
  char host[100];
  int port;
  struct sockaddr_in sin;
  bool reachable;
};
  

class Network: public TQObject
{
  TQ_OBJECT
  
    
 public:
  enum { defaultPort = 23412 };

  /* install listening TCP socket on port */
  Network(int port = defaultPort);
  ~Network();

  bool isOK() { return (fd>=0); }
  void addListener(const char* host, int port);  
  void broadcast(const char* pos);

 signals:
  void gotPosition(const char* pos);

 private slots:
  void gotConnection();
  
 private:
  bool sendString(struct sockaddr_in sin, char* str, int len);

  TQPtrList<Listener> listeners;
  struct sockaddr_in mySin;
  int fd, myPort;
  TQSocketNotifier *sn;
};

#endif