[Libreoffice-commits] core.git: Branch 'private/tbsdy/workbench' - 2 commits - Repository.mk sal/Executable_socket.mk sal/Module_sal.mk sal/workben

Chris Sherlock chris.sherlock79 at gmail.com
Tue Aug 29 11:48:56 UTC 2017


 Repository.mk                      |    1 
 sal/Executable_socket.mk           |   31 ++++++++++
 sal/Module_sal.mk                  |    1 
 sal/workben/osl/socket/socket.cxx  |  106 +++++++++++++++++++++++++++++++++++++
 sal/workben/osl/thread/monitor.cxx |    9 +--
 5 files changed, 142 insertions(+), 6 deletions(-)

New commits:
commit 2ae3e6b01effa46822e2544649d804331e2a50bb
Author: Chris Sherlock <chris.sherlock79 at gmail.com>
Date:   Tue Aug 29 21:44:27 2017 +1000

    sal workbench: include basic socket example
    
    Change-Id: Ie3441f25d2ff9589fecff27a52c49dfb8d301050

diff --git a/Repository.mk b/Repository.mk
index 2ab6ac0f7518..7abc44ab59ed 100644
--- a/Repository.mk
+++ b/Repository.mk
@@ -91,6 +91,7 @@ $(eval $(call gb_Helper_register_executables,NONE, \
     setgetenv \
     signal \
     monitor \
+    socket \
 ))
 
 $(eval $(call gb_Helper_register_executables_for_install,SDK,sdk, \
diff --git a/sal/Executable_socket.mk b/sal/Executable_socket.mk
new file mode 100644
index 000000000000..275ae8abcc34
--- /dev/null
+++ b/sal/Executable_socket.mk
@@ -0,0 +1,31 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+
+$(eval $(call gb_Executable_Executable,socket))
+
+$(eval $(call gb_Executable_set_include,socket,\
+    $$(INCLUDE) \
+    -I$(SRCDIR)/sal/inc \
+))
+
+$(eval $(call gb_Library_add_defs,socket,\
+    -DSAL_DLLIMPLEMENTATION \
+))
+
+$(eval $(call gb_Executable_use_libraries,socket,\
+    sal \
+))
+
+$(eval $(call gb_Executable_add_exception_objects,socket,\
+    sal/workben/osl/socket/socket \
+))
+
+$(call gb_Executable_get_clean_target,socket) :
+	rm -f $(WORKDIR)/LinkTarget/Executable/socket
+# vim: set ts=4 sw=4 et:
diff --git a/sal/Module_sal.mk b/sal/Module_sal.mk
index 8cecbd8d0068..6dbc681b2a9f 100644
--- a/sal/Module_sal.mk
+++ b/sal/Module_sal.mk
@@ -33,6 +33,7 @@ $(eval $(call gb_Module_add_targets,sal,\
     Executable_setgetenv \
     Executable_signal \
     Executable_monitor \
+    Executable_socket \
 ))
 
 $(eval $(call gb_Module_add_check_targets,sal,\
diff --git a/sal/workben/osl/socket/socket.cxx b/sal/workben/osl/socket/socket.cxx
new file mode 100644
index 000000000000..6b0b0fc7bb1c
--- /dev/null
+++ b/sal/workben/osl/socket/socket.cxx
@@ -0,0 +1,106 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ *   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements. See the NOTICE file distributed
+ *   with this work for additional information regarding copyright
+ *   ownership. The ASF licenses this file to you under the Apache
+ *   License, Version 2.0 (the "License"); you may not use this file
+ *   except in compliance with the License. You may obtain a copy of
+ *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <sal/main.h>
+#include <rtl/ustring.h>
+#include <osl/thread.h>
+#include <osl/conditn.h>
+#include <osl/socket.h>
+
+#include <cstdio>
+
+oslThread serverThread;
+oslCondition serverReady;
+
+void server(void*);
+void client();
+
+SAL_IMPLEMENT_MAIN()
+{
+    fprintf(stdout, "Demonstrates sockets.\n");
+
+    serverReady = osl_createCondition();
+    serverThread = osl_createThread(server, nullptr);
+    osl_waitCondition(serverReady, nullptr);
+    client();
+    osl_joinWithThread(serverThread);
+
+    return 0;
+}
+
+void client()
+{
+    oslSocket socket = osl_createSocket(osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp);
+
+    rtl_uString *pstrLocalHostAddr = nullptr;
+    rtl_string2UString(&pstrLocalHostAddr, "127.0.0.1", 9, osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS);
+
+    // create high socket on localhost address
+    oslSocketAddr addr = osl_createInetSocketAddr(pstrLocalHostAddr, 30000);
+
+    if (osl_connectSocketTo(socket, addr, nullptr) != osl_Socket_Ok)
+    {
+        fprintf(stderr, "**Client**    Could not bind address to socket.\n");
+        exit(1);
+    }
+
+    char sendBuffer = 'c';
+    sal_Int32 nSentChar = osl_sendSocket(socket, &sendBuffer, 1, osl_Socket_MsgNormal);
+    fprintf(stdout, "**Client**    Sent %d character.\n", nSentChar);
+}
+
+void server(void* /* pData */)
+{
+    oslSocket socket = osl_createSocket(osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp);
+
+    rtl_uString *pstrLocalHostAddr = nullptr;
+    rtl_string2UString(&pstrLocalHostAddr, "127.0.0.1", 9, osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS);
+
+    fprintf(stdout, "**Server**    Listening on localhost...\n");
+    // create high socket on localhost address
+    fprintf(stdout, "**Server**    Create socket\n");
+    oslSocketAddr addr = osl_createInetSocketAddr(pstrLocalHostAddr, 30000);
+
+    fprintf(stdout, "**Server**    Bind address to socket\n");
+    if (osl_bindAddrToSocket(socket, addr) == sal_False)
+    {
+        fprintf(stderr, "Could not bind address to socket.\n");
+        exit(1);
+    }
+
+    fprintf(stdout, "**Server**    Listen on socket...\n");
+    if (osl_listenOnSocket(socket, -1) == sal_False)
+    {
+        fprintf(stderr, "**Client** Could not listen on socket.\n");
+        exit(1);
+    }
+
+    osl_setCondition(serverReady);
+
+    fprintf(stdout, "**Server**    Accept connection...\n");
+    oslSocket inboundSocket = osl_acceptConnectionOnSocket(socket, &addr);
+
+    fprintf(stdout, "**Server**    Receive data...\n");
+    char buffer;
+    osl_receiveSocket(inboundSocket, &buffer, 1, osl_Socket_MsgNormal);
+
+    fprintf(stdout, "**Server**    Received character %c\n", buffer);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
commit b56a5a313c300958ae49ecd0b85aaa3829b2e7f4
Author: Chris Sherlock <chris.sherlock79 at gmail.com>
Date:   Tue Aug 29 19:59:52 2017 +1000

    sal workbench: remove mistaken while in monitor.cxx
    
    Change-Id: I934c97793938221c549fb624fc01afd23d3d75b5

diff --git a/sal/workben/osl/thread/monitor.cxx b/sal/workben/osl/thread/monitor.cxx
index 8060883b67f7..99e0d80f5d44 100644
--- a/sal/workben/osl/thread/monitor.cxx
+++ b/sal/workben/osl/thread/monitor.cxx
@@ -47,13 +47,10 @@ SAL_IMPLEMENT_MAIN()
     queueMutex = osl_createMutex();
     fullOrEmpty = osl_createCondition();
 
-    while(true)
-    {
-        producer = osl_createThread(produce, nullptr);
-        consumer = osl_createThread(consume, nullptr);
+    producer = osl_createThread(produce, nullptr);
+    consumer = osl_createThread(consume, nullptr);
 
-        osl_joinWithThread(consumer);
-    }
+    osl_joinWithThread(consumer);
 
     return 0;
 }


More information about the Libreoffice-commits mailing list