Fix example, thanks Juan Gabriel Alzate Romero
[supercollider.git] / Headers / server / SC_ComPort.h
blob43d31ba8d58bfe84d3a565a2857bf5fc22d5c0db
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifndef _SC_ComPort_
23 #define _SC_ComPort_
25 #if defined (__APPLE__) && defined (__GNUC__)
26 #define USE_RENDEZVOUS
27 #endif
29 #include <sys/types.h>
30 #ifdef SC_WIN32
31 # include <winsock2.h>
32 #else
33 # include <sys/socket.h>
34 #endif
35 #include "OSC_Packet.h"
36 #include "SC_Sem.h"
38 //////////////////////////////////////////////////////////////////////////////////////////////////////////
40 class SC_CmdPort
42 protected:
43 pthread_t mThread;
44 struct World *mWorld;
46 void Start();
47 virtual ReplyFunc GetReplyFunc()=0;
48 public:
49 SC_CmdPort(struct World *inWorld);
50 virtual ~SC_CmdPort() {}
52 virtual void* Run()=0;
55 //////////////////////////////////////////////////////////////////////////////////////////////////////////
57 class SC_ComPort : public SC_CmdPort
59 protected:
60 int mPortNum;
61 int mSocket;
62 struct sockaddr_in mBindSockAddr;
64 #ifdef USE_RENDEZVOUS
65 pthread_t mRendezvousThread;
66 #endif
68 public:
69 SC_ComPort(struct World *inWorld, int inPortNum);
70 virtual ~SC_ComPort();
72 int Socket() { return mSocket; }
74 int PortNum() const { return mPortNum; }
75 #ifdef USE_RENDEZVOUS
76 // default implementation does nothing (this is correct for
77 // SC_TcpConnectionPort). Subclasses may override.
78 virtual void PublishToRendezvous() { };
79 #endif
82 //////////////////////////////////////////////////////////////////////////////////////////////////////////
84 const size_t kMaxUDPSize = 65535;
86 class SC_UdpInPort : public SC_ComPort
88 protected:
89 struct sockaddr_in mReplySockAddr;
90 unsigned char mReadBuf[kMaxUDPSize];
91 virtual ReplyFunc GetReplyFunc();
93 public:
94 SC_UdpInPort(struct World *inWorld, int inPortNum);
95 ~SC_UdpInPort();
97 int PortNum() const { return mPortNum; }
99 void* Run();
100 #ifdef USE_RENDEZVOUS
101 virtual void PublishToRendezvous();
102 #endif
106 //////////////////////////////////////////////////////////////////////////////////////////////////////////
108 class SC_TcpInPort : public SC_ComPort
110 SC_Semaphore mConnectionAvailable;
111 int mBacklog;
113 protected:
114 virtual ReplyFunc GetReplyFunc();
116 public:
117 SC_TcpInPort(struct World *inWorld, int inPortNum, int inMaxConnections, int inBacklog);
119 virtual void* Run();
121 void ConnectionTerminated();
122 #ifdef USE_RENDEZVOUS
123 virtual void PublishToRendezvous();
124 #endif
127 //////////////////////////////////////////////////////////////////////////////////////////////////////////
129 class SC_TcpConnectionPort : public SC_ComPort
131 SC_TcpInPort *mParent;
132 unsigned char mReadBuf[kMaxUDPSize];
134 protected:
135 virtual ReplyFunc GetReplyFunc();
137 public:
138 SC_TcpConnectionPort(struct World *inWorld, SC_TcpInPort *inParent, int inSocket);
139 virtual ~SC_TcpConnectionPort();
141 virtual void* Run();
144 //////////////////////////////////////////////////////////////////////////////////////////////////////////
146 #ifdef SC_DARWIN
148 #include <CoreFoundation/CFMessagePort.h>
150 class SC_MachMessagePort : public SC_CmdPort
152 CFMessagePortRef mServerPort;
153 CFMessagePortRef mReplyPort;
155 protected:
156 virtual ReplyFunc GetReplyFunc();
158 public:
159 SC_MachMessagePort(struct World *inWorld, CFStringRef serverPortName, CFStringRef replyPortName);
160 virtual ~SC_MachMessagePort();
162 virtual void* Run();
164 private:
165 static CFDataRef messagePortCallBack(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info);
168 #endif // SC_DARWIN
170 //////////////////////////////////////////////////////////////////////////////////////////////////////////
172 #endif