common: prevent buffer overflow
[supercollider.git] / include / server / SC_ComPort.h
blob99cabce20a5bf7d9707e462d5e533604b2fc79d9
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
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 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _SC_ComPort_
23 #define _SC_ComPort_
25 #include <sys/types.h>
26 #ifdef _WIN32
27 # include <winsock2.h>
28 #else
29 # include <sys/socket.h>
30 #endif
31 #include "OSC_Packet.h"
32 #include "SC_Sem.h"
34 //////////////////////////////////////////////////////////////////////////////////////////////////////////
36 class SC_CmdPort
38 protected:
39 pthread_t mThread;
40 struct World *mWorld;
42 void Start();
43 virtual ReplyFunc GetReplyFunc()=0;
44 public:
45 SC_CmdPort(struct World *inWorld);
46 virtual ~SC_CmdPort() {}
48 virtual void* Run()=0;
51 //////////////////////////////////////////////////////////////////////////////////////////////////////////
53 class SC_ComPort : public SC_CmdPort
55 protected:
56 int mPortNum;
57 int mSocket;
58 struct sockaddr_in mBindSockAddr;
60 #ifdef USE_RENDEZVOUS
61 pthread_t mRendezvousThread;
62 #endif
64 public:
65 SC_ComPort(struct World *inWorld, int inPortNum);
66 virtual ~SC_ComPort();
68 int Socket() { return mSocket; }
70 int PortNum() const { return mPortNum; }
71 #ifdef USE_RENDEZVOUS
72 // default implementation does nothing (this is correct for
73 // SC_TcpConnectionPort). Subclasses may override.
74 virtual void PublishToRendezvous() { };
75 #endif
78 //////////////////////////////////////////////////////////////////////////////////////////////////////////
80 const size_t kMaxUDPSize = 65535;
82 class SC_UdpInPort : public SC_ComPort
84 protected:
85 struct sockaddr_in mReplySockAddr;
86 unsigned char mReadBuf[kMaxUDPSize];
87 virtual ReplyFunc GetReplyFunc();
89 public:
90 SC_UdpInPort(struct World *inWorld, int inPortNum);
91 ~SC_UdpInPort();
93 int PortNum() const { return mPortNum; }
95 void* Run();
96 #ifdef USE_RENDEZVOUS
97 virtual void PublishToRendezvous();
98 #endif
102 //////////////////////////////////////////////////////////////////////////////////////////////////////////
104 class SC_TcpInPort : public SC_ComPort
106 SC_Semaphore mConnectionAvailable;
107 int mBacklog;
109 protected:
110 virtual ReplyFunc GetReplyFunc();
112 public:
113 SC_TcpInPort(struct World *inWorld, int inPortNum, int inMaxConnections, int inBacklog);
115 virtual void* Run();
117 void ConnectionTerminated();
118 #ifdef USE_RENDEZVOUS
119 virtual void PublishToRendezvous();
120 #endif
123 //////////////////////////////////////////////////////////////////////////////////////////////////////////
125 class SC_TcpConnectionPort : public SC_ComPort
127 SC_TcpInPort *mParent;
128 unsigned char mReadBuf[kMaxUDPSize];
130 protected:
131 virtual ReplyFunc GetReplyFunc();
133 public:
134 SC_TcpConnectionPort(struct World *inWorld, SC_TcpInPort *inParent, int inSocket);
135 virtual ~SC_TcpConnectionPort();
137 virtual void* Run();
140 #endif