Permalink
Browse files

sclang: init Windows sockets before use and cleanup after

  • Loading branch information...
1 parent d92e2e7 commit eb064e119add376cce13237fdbb62f08fa8971a0 @jleben jleben committed Mar 19, 2011
View
@@ -49,6 +49,7 @@ void pprintf(unsigned char *str, char *fmt, ...);
SC_DLLEXPORT_C void schedInit();
SC_DLLEXPORT_C void init_OSC(int port);
+SC_DLLEXPORT_C void cleanup_OSC();
SC_DLLEXPORT_C bool pyr_init_mem_pools(int runtime_space, int runtime_grow);
SC_DLLEXPORT_C void schedRun();
@@ -72,6 +72,7 @@ class SC_DLLEXPORT SC_LanguageClient
// initialize language runtime
void initRuntime(const Options& opt=Options());
+ void shutdownRuntime();
// return application name
const char* getName() const { return mName; }
@@ -775,13 +775,30 @@ void init_OSC(int port);
void init_OSC(int port)
{
postfl("init_OSC\n");
+
+#ifdef _WIN32
+ WSAData wsaData;
+ int nCode;
+ if ((nCode = WSAStartup(MAKEWORD(1, 1), &wsaData)) != 0) {
+ error( "sclang: init_OSC: WSAStartup() failed with error code %d.\n", nCode );
+ }
+#endif
+
try {
gUDPport = new SC_UdpInPort(port);
} catch (...) {
postfl("No networking.");
}
}
+void cleanup_OSC()
+{
+ postfl( "cleaning up OSC\n");
+#ifdef _WIN32
+ WSACleanup();
+#endif
+}
+
int prGetHostByName(VMGlobals *g, int numArgsPushed);
int prGetHostByName(VMGlobals *g, int numArgsPushed)
{
@@ -792,7 +809,14 @@ int prGetHostByName(VMGlobals *g, int numArgsPushed)
if (err) return err;
struct hostent *he = gethostbyname(hostname);
- if (!he) return errFailed;
+ if (!he) {
+#ifdef _WIN32
+ int err = WSAGetLastError();
+ error("gethostbyname(\"%s\") failed with error code %i.\n",
+ hostname, err);
+#endif
+ return errFailed;
+ }
SetInt(a, ntohl(*(int*)he->h_addr));
@@ -103,6 +103,11 @@ void SC_LanguageClient::initRuntime(const Options& opt)
}
}
+void SC_LanguageClient::shutdownRuntime()
+{
+ cleanup_OSC();
+}
+
bool SC_LanguageClient::readLibraryConfig(const char* filePath, const char* fileName)
{
SC_LibraryConfigFile file(&::post);
@@ -259,6 +259,8 @@ int SC_TerminalClient::run(int argc, char** argv)
shutdownLibrary();
flush();
+ shutdownRuntime();
+
return mReturnCode;
}
@@ -181,6 +181,8 @@ int main()
compileLibrary();
runLibrary(s_run); fflush(postfile);
+ cleanup_OSC();
+
return 0;
}

0 comments on commit eb064e1

Please sign in to comment.