Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/tcp_server_socket.cc @ 10641

Last change on this file since 10641 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 2.9 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
28ObjectListDefinition(TcpServerSocket);
29TcpServerSocket::TcpServerSocket( int port ) : ServerSocket( port )
30{
31  init();
32  listen(port);
33}
34
35/**
36 * Default destructor
37 */
38TcpServerSocket::~TcpServerSocket( )
39{
40  /* Quit SDL_net */
41  // NOTE: what if other instances of NetworkSocket running?
42  SDLNet_Quit();
43  PRINTF(5)("SDL_net shutdown\n");
44
45  _isListening = false;
46}
47
48void TcpServerSocket::init( )
49{
50  /* set the class id for the base object */
51  this->registerObject(this, TcpServerSocket::_objectList);
52
53  terminateThread = false;
54  listenSocket = NULL;
55  _isListening = false;
56
57  if(SDLNet_Init()==-1)
58  {
59    PRINTF(1)("SDLNet_Init: %s\n", SDLNet_GetError());
60    return;
61  }
62  else
63    PRINTF(5)("SDL_net initialized\n");
64
65  PRINTF(0)("TcpServerSocket created\n");
66}
67
68
69/**
70 * Tells the NetworkSocket to listen on a specific port for incoming connections.
71 * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
72 * @param port
73 */
74bool TcpServerSocket::listen(unsigned int port)
75{
76  PRINTF(0)("TcpServerSocket::listen()\n");
77  _isListening = true;
78  //check if not already connected or listening
79  if (listenSocket)
80  {
81    PRINTF(1)("TcpServerSocket::listen: tcpSocket!=NULL! maybe you already called listen or did not call close()!\n");
82    _isListening = false;
83    return false;
84  }
85
86  IPaddress ip;
87
88  if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
89  {
90    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
91    _isListening = false;
92    return false;
93  }
94
95  listenSocket = SDLNet_TCP_Open(&ip);
96
97  if (!listenSocket)
98  {
99    PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
100    _isListening = false;
101    return false;
102  }
103
104  return true;
105}
106
107
108NetworkSocket* TcpServerSocket::getNewSocket( )
109{
110  if ( !listenSocket )
111  {
112    PRINTF(1)("listenSocket == NULL! Maybe you forgot to call listen()\n");
113    close();
114    return NULL;
115  }
116
117  TCPsocket sock = SDLNet_TCP_Accept(listenSocket);
118
119  if ( !sock )
120  {
121    return NULL;
122  }
123  else
124  {
125    return new TcpSocket(sock);
126  }
127}
128
129
130
131void TcpServerSocket::close( )
132{
133  if ( listenSocket )
134  {
135    SDLNet_TCP_Close( listenSocket );
136    listenSocket = NULL;
137  }
138
139  _isListening = false;
140}
141
Note: See TracBrowser for help on using the repository browser.