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