Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / include / lang / SC_ComPort.h
blobc5f14a440187b2ab2b225a367fe150a700e63be4
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
21 #ifndef _SC_ComPort_
22 #define _SC_ComPort_
24 #ifdef _WIN32
25 # include <winsock2.h>
26 typedef int socklen_t;
27 # define bzero( ptr, count ) memset( ptr, 0, count )
28 #else
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #endif
33 #include "SC_Msg.h"
34 #include "SC_Sem.h"
35 #include "boost/atomic.hpp"
37 //////////////////////////////////////////////////////////////////////////////////////////////////////////
39 class SC_CmdPort
41 protected:
42 pthread_t mThread;
44 void Start();
45 virtual ReplyFunc GetReplyFunc()=0;
46 public:
47 SC_CmdPort();
48 virtual ~SC_CmdPort();
50 virtual void* Run()=0;
53 //////////////////////////////////////////////////////////////////////////////////////////////////////////
55 class SC_ComPort : public SC_CmdPort
57 protected:
58 int mPortNum;
59 int mSocket;
60 struct sockaddr_in mBindSockAddr;
62 public:
63 SC_ComPort(int inPortNum);
64 virtual ~SC_ComPort();
66 int Socket() { return mSocket; }
68 int PortNum() const { return mPortNum; }
69 int RealPortNum() const { return ntohs(mBindSockAddr.sin_port); }
72 //////////////////////////////////////////////////////////////////////////////////////////////////////////
74 const int kTextBufSize = 65536;
76 class SC_UdpInPort : public SC_ComPort
78 char buf[kTextBufSize];
79 struct sockaddr_in mReplySockAddr;
80 virtual ReplyFunc GetReplyFunc();
82 public:
83 SC_UdpInPort(int inPortNum);
84 ~SC_UdpInPort();
86 int PortNum() const { return mPortNum; }
88 void* Run();
91 //////////////////////////////////////////////////////////////////////////////////////////////////////////
93 class SC_UdpCustomInPort : public SC_ComPort
95 char buf[kTextBufSize];
96 struct sockaddr_in mReplySockAddr;
97 virtual ReplyFunc GetReplyFunc();
98 boost::atomic<bool> mRunning;
100 public:
101 SC_UdpCustomInPort(int inPortNum);
102 ~SC_UdpCustomInPort();
104 int PortNum() const { return mPortNum; }
106 void* Run();
109 //////////////////////////////////////////////////////////////////////////////////////////////////////////
111 class SC_TcpInPort : public SC_ComPort
113 SC_Semaphore mConnectionAvailable;
114 int mBacklog;
116 protected:
117 virtual ReplyFunc GetReplyFunc();
119 public:
120 SC_TcpInPort(int inPortNum, int inMaxConnections, int inBacklog);
122 virtual void* Run();
124 void ConnectionTerminated();
127 //////////////////////////////////////////////////////////////////////////////////////////////////////////
129 class SC_TcpConnectionPort : public SC_ComPort
131 SC_TcpInPort *mParent;
133 protected:
134 virtual ReplyFunc GetReplyFunc();
136 public:
137 SC_TcpConnectionPort(SC_TcpInPort *inParent, int inSocket);
138 virtual ~SC_TcpConnectionPort();
140 virtual void* Run();
144 //////////////////////////////////////////////////////////////////////////////////////////////////////////
146 class SC_TcpClientPort : public SC_ComPort
148 public:
149 typedef void (*ClientNotifyFunc)(void* clientData);
151 public:
152 SC_TcpClientPort(int inSocket, ClientNotifyFunc notifyFunc=0, void* clientData=0);
153 virtual ~SC_TcpClientPort();
155 virtual void* Run();
156 void Close();
158 protected:
159 virtual ReplyFunc GetReplyFunc();
161 private:
162 struct sockaddr_in mReplySockAddr;
163 int mCmdFifo[2];
164 ClientNotifyFunc mClientNotifyFunc;
165 void* mClientData;
168 //////////////////////////////////////////////////////////////////////////////////////////////////////////
170 #endif