1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * Implementation of the ServerSocket class |
---|
5 | * |
---|
6 | * ====================================================================== |
---|
7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
9 | * |
---|
10 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
11 | * |
---|
12 | * See the file "license.terms" for information on usage and |
---|
13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | * ====================================================================== |
---|
15 | */ |
---|
16 | #include "ServerSocket.h" |
---|
17 | #include "SocketException.h" |
---|
18 | |
---|
19 | ServerSocket::ServerSocket ( int port ) |
---|
20 | { |
---|
21 | connected = false; |
---|
22 | if ( ! Socket::create() ) |
---|
23 | { |
---|
24 | throw SocketException ( "Could not create server socket." ); |
---|
25 | } |
---|
26 | |
---|
27 | if ( ! Socket::bind ( port ) ) |
---|
28 | { |
---|
29 | throw SocketException ( "Could not bind to port." ); |
---|
30 | } |
---|
31 | |
---|
32 | if ( ! Socket::listen() ) |
---|
33 | { |
---|
34 | throw SocketException ( "Could not listen to socket." ); |
---|
35 | } |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | ServerSocket::~ServerSocket() |
---|
40 | { |
---|
41 | connected = false; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | const ServerSocket& ServerSocket::operator << ( const std::string& s ) const |
---|
46 | { |
---|
47 | if ( ! Socket::send ( s ) ) |
---|
48 | { |
---|
49 | throw SocketException ( "Could not write to socket." ); |
---|
50 | } |
---|
51 | |
---|
52 | return *this; |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | const ServerSocket& ServerSocket::operator >> ( std::string& s ) const |
---|
58 | { |
---|
59 | if ( ! Socket::recv ( s ) ) |
---|
60 | { |
---|
61 | throw SocketException ( "Could not read from socket." ); |
---|
62 | } |
---|
63 | |
---|
64 | return *this; |
---|
65 | } |
---|
66 | |
---|
67 | bool ServerSocket::send (char* s, int size) const |
---|
68 | { |
---|
69 | //printf("ServerSocket::send: 0\n"); |
---|
70 | bool ret = Socket::send (s, size); |
---|
71 | //printf("ServerSocket::send: 1\n"); |
---|
72 | if (!ret) |
---|
73 | { |
---|
74 | //printf("ServerSocket::send: 2\n"); |
---|
75 | throw SocketException ( "Could not write to socket." ); |
---|
76 | } |
---|
77 | |
---|
78 | return ret; |
---|
79 | } |
---|
80 | |
---|
81 | int ServerSocket::recv ( char* s, int size) const |
---|
82 | { |
---|
83 | bool ret = Socket::recv (s, size); |
---|
84 | if (!ret) |
---|
85 | { |
---|
86 | throw SocketException ( "Could not read from socket." ); |
---|
87 | } |
---|
88 | |
---|
89 | return ret; |
---|
90 | } |
---|
91 | |
---|
92 | bool ServerSocket::accept ( ServerSocket& sock ) |
---|
93 | { |
---|
94 | /* |
---|
95 | if ( ! Socket::accept ( sock ) ) |
---|
96 | { |
---|
97 | throw SocketException ( "Could not accept socket." ); |
---|
98 | } |
---|
99 | */ |
---|
100 | bool ret = Socket::accept( sock ); |
---|
101 | sock.set_connected(ret); |
---|
102 | return ret; |
---|
103 | } |
---|
104 | |
---|
105 | void ServerSocket::set_non_blocking(bool val){ |
---|
106 | Socket::set_non_blocking(val); |
---|
107 | } |
---|
108 | |
---|
109 | bool ServerSocket::is_connected(){ |
---|
110 | return connected; |
---|
111 | } |
---|
112 | |
---|
113 | bool ServerSocket::set_connected(bool val){ |
---|
114 | return connected = val; |
---|
115 | } |
---|