Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/lib/network/tcp_server_socket.cc @ 10300

Last change on this file since 10300 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 2.8 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer:
14*/
15
16/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
17   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
18*/
19#define DEBUG_MODULE_NETWORK
20
21#include "tcp_server_socket.h"
22#include "tcp_socket.h"
23
24
25/* header for debug output */
26#include "debug.h"
27
28TcpServerSocket::TcpServerSocket( int port ) : ServerSocket( port )
29{
30  init();
31  listen(port);
32}
33
34/**
35 * Default destructor
36 */
37TcpServerSocket::~TcpServerSocket( )
38{
39  /* Quit SDL_net */
40  // NOTE: what if other instances of NetworkSocket running?
41  SDLNet_Quit();
42  PRINTF(5)("SDL_net shutdown\n");
43
44  _isListening = false;
45}
46
47void TcpServerSocket::init( )
48{
49  /* set the class id for the base object */
50  this->setClassID(CL_SERVER_SOCKET, "TcpServerSocket");
51
52  terminateThread = false;
53  listenSocket = NULL;
54  _isListening = false;
55
56  if(SDLNet_Init()==-1)
57  {
58    PRINTF(1)("SDLNet_Init: %s\n", SDLNet_GetError());
59    return;
60  }
61  else
62    PRINTF(5)("SDL_net initialized\n");
63
64  PRINTF(0)("TcpServerSocket created\n");
65}
66
67
68/**
69 * Tells the NetworkSocket to listen on a specific port for incoming connections.
70 * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
71 * @param port
72 */
73bool TcpServerSocket::listen(unsigned int port)
74{
75  PRINTF(0)("TcpServerSocket::listen()\n");
76  _isListening = true;
77  //check if not already connected or listening
78  if (listenSocket)
79  {
80    PRINTF(1)("TcpServerSocket::listen: tcpSocket!=NULL! maybe you already called listen or did not call close()!\n");
81    _isListening = false;
82    return false;
83  }
84
85  IPaddress ip;
86
87  if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
88  {
89    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
90    _isListening = false;
91    return false;
92  }
93
94  listenSocket = SDLNet_TCP_Open(&ip);
95
96  if (!listenSocket)
97  {
98    PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
99    _isListening = false;
100    return false;
101  }
102
103  return true;
104}
105
106
107NetworkSocket* TcpServerSocket::getNewSocket( )
108{
109  if ( !listenSocket )
110  {
111    PRINTF(1)("listenSocket == NULL! Maybe you forgot to call listen()\n");
112    close();
113    return NULL;
114  }
115
116  TCPsocket sock = SDLNet_TCP_Accept(listenSocket);
117
118  if ( !sock )
119  {
120    return NULL;
121  }
122  else
123  {
124    return new TcpSocket(sock);
125  }
126}
127
128void TcpServerSocket::close( )
129{
130  if ( listenSocket )
131  {
132    SDLNet_TCP_Close( listenSocket );
133    listenSocket = NULL;
134  }
135
136  _isListening = false;
137}
138
Note: See TracBrowser for help on using the repository browser.