Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 26, 2024
2 parents 1985a2f + 1e14607 commit 9b3e241
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 16 deletions.
10 changes: 3 additions & 7 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
# package name contains capital letter and such names are conventional.
---
exclude_paths:
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/ConnectSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/InetAddrSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/StrerrorSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Posix/ErrnoSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/SockaddrIn.java"
- "eo-runtime/src/test/java/EOorg/EOeolang/EOnet/EOsocketTest.java"
- "eo-runtime/src/test/java/EOorg/EOeolang/EOnet/package-info.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/ConnectSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/InetAddrSyscall.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOsys/Win32/WSAGetLastErrorSyscall.java"
87 changes: 84 additions & 3 deletions eo-runtime/src/main/eo/org/eolang/net/socket.eo
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

+alias org.eolang.sys.posix
+alias org.eolang.sys.os
+alias org.eolang.sys.win32
+alias org.eolang.txt.sprintf
+architect [email protected]
+home https:/objectionary/eo
Expand Down Expand Up @@ -68,7 +69,7 @@
inet-addr.as-i32
posix.sockaddr-in > sockaddr
posix.af-inet.as-i16
^.^.htons port
^.^.htons ^.port
inet-addr-as-int
code. > connected
posix
Expand Down Expand Up @@ -104,10 +105,90 @@
closed.eq -1
error
sprintf
"Couldn't close a posix socket '%d' connected to '%s:%d'"
* sd ^.address ^.port
"Couldn't close a posix socket '%d' connected to '%s:%d', reason: '%s'"
*
sd
^.address
^.port
code.
posix
"strerror"
* (posix "errno" *).code
true

[descriptor] > scoped-posix-socket

[address port] > win-socket
[scope] > connect
code. > started-up
win32
"WSAStartup"
* win32.winsock-version-2-2
code. > sd
win32
"socket"
* win32.af-inet win32.sock-stream win32.ipproto-tcp
code. > inet-addr
win32
"inet_addr"
* ^.address
if. > inet-addr-as-int
inet-addr.eq win32.inaddr-none
error
sprintf
"Couldn't convert an IPv4 address '%s' into a 32-bit integer via 'inet_addr' win32 function call, WSA error code: %d"
* ^.address last-error-code
inet-addr.as-i32
win32.sockaddr-in > sockaddr
win32.af-inet.as-i16
^.^.htons ^.port
inet-addr-as-int
code. > connected
win32
"connect"
* sd sockaddr sockaddr.size
code. > closed
win32
"closesocket"
* sd
(win32 "WSACleanup" *).code > cleaned-up
(win32 "WSAGetLastError" *).code > last-error-code
if. > @
(started-up.eq 0).not
error
sprintf
"Couldn't initialize Winsock via 'WSAStartup' call, WSA error code: %d"
* started-up
try
if.
sd.eq -1
error
sprintf
"Couldn't create a win32 socket, WSA error code: %d"
* last-error-code
try
if.
connected.eq -1
error
sprintf
"Couldn't connect to '%s:%d' on win32 socket '%d', WSA error code: %d"
* ^.address ^.port sd last-error-code
as-bytes.
dataized
scope
^.scoped-win-socket sd
ex > [ex]
if.
closed.eq win32.socket-error
error
sprintf
"Couldn't close a win32 socket '%d', WSA error code: %d"
* sd last-error-code
true
ex > [ex]
if.
cleaned-up.eq win32.socket-error
error "Couldn't cleanup Winsock resources"
true

[descriptor] > scoped-win-socket
13 changes: 13 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/sys/win32.eo
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
6 > ipproto-tcp
-1 > invalid-socket
-1 > socket-error
-1 > inaddr-none
02-02 > winsock-version-2-2

[] > @ /return

Expand All @@ -51,3 +53,14 @@
# Structure for "GetSystemTime" function call.
[year month day day-of-week hour minute second milliseconds] > system-time
$ > self

# The win32 `sockaddr_in` structure.
[sin-family sin-port sin-addr] > sockaddr-in
00-00-00-00-00-00-00-00 > sin-zero
plus. > size
plus.
plus.
sin-family.size
sin-port.size
sin-addr.size
sin-zero.size
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
package EOorg.EOeolang.EOsys; // NOPMD

import EOorg.EOeolang.EOsys.Win32.ClosesocketFuncCall;
import EOorg.EOeolang.EOsys.Win32.ConnectFuncCall;
import EOorg.EOeolang.EOsys.Win32.GetCurrentProcessIdFuncCall;
import EOorg.EOeolang.EOsys.Win32.GetEnvironmentVariableFuncCall;
import EOorg.EOeolang.EOsys.Win32.GetSystemTimeFuncCall;
import EOorg.EOeolang.EOsys.Win32.InetAddrFuncCall;
import EOorg.EOeolang.EOsys.Win32.ReadFileFuncCall;
import EOorg.EOeolang.EOsys.Win32.SocketFuncCall;
import EOorg.EOeolang.EOsys.Win32.WSACleanupFuncCall;
import EOorg.EOeolang.EOsys.Win32.WSAGetLastErrorFuncCall;
import EOorg.EOeolang.EOsys.Win32.WSAStartupFuncCall;
import EOorg.EOeolang.EOsys.Win32.WriteFileFuncCall;
import java.util.HashMap;
Expand Down Expand Up @@ -69,8 +72,11 @@ public final class EOwin32$EOφ extends PhDefault implements Atom {
EOwin32$EOφ.FUNCTIONS.put("GetSystemTime", GetSystemTimeFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("WSAStartup", WSAStartupFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("WSACleanup", WSACleanupFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("WSAGetLastError", WSAGetLastErrorFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("socket", SocketFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("connect", ConnectFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("closesocket", ClosesocketFuncCall::new);
EOwin32$EOφ.FUNCTIONS.put("inet_addr", InetAddrFuncCall::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public Phi make(final Phi... params) {
final Phi result = this.posix.take("return").copy();
result.put(
0,
new Data.ToPhi(CStdLib.INSTANCE.inet_addr(new Dataized(params[0]).asString()))
new Data.ToPhi(
Integer.reverseBytes(
CStdLib.INSTANCE.inet_addr(new Dataized(params[0]).asString())
)
)
);
result.put(1, new PhDefault());
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
* @checkstyle TrailingCommentCheck (3 lines)
*/
package EOorg.EOeolang.EOsys.Win32; // NOPMD

import EOorg.EOeolang.EOsys.SockaddrIn;
import EOorg.EOeolang.EOsys.Syscall;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.PhDefault;
import org.eolang.Phi;

/**
* The 'connect' WS2_32 function call.
* @see <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect">here for details</a>
* @since 0.40.0
*/
public final class ConnectFuncCall implements Syscall {
/**
* Win32 object.
*/
private final Phi win;

/**
* Ctor.
* @param win Win32 object
*/
public ConnectFuncCall(final Phi win) {
this.win = win;
}

@Override
public Phi make(final Phi... params) {
final Phi result = this.win.take("return").copy();
result.put(
0,
new Data.ToPhi(
Winsock.INSTANCE.connect(
new Dataized(params[0]).asNumber().intValue(),
new SockaddrIn(
new Dataized(params[1].take("sin-family")).take(Short.class),
new Dataized(params[1].take("sin-port")).take(Short.class),
new Dataized(params[1].take("sin-addr")).take(Integer.class),
new Dataized(params[1].take("sin-zero")).take()
),
new Dataized(params[2]).asNumber().intValue()
)
)
);
result.put(1, new PhDefault());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
* @checkstyle TrailingCommentCheck (3 lines)
*/
package EOorg.EOeolang.EOsys.Win32; // NOPMD

import EOorg.EOeolang.EOsys.Syscall;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.PhDefault;
import org.eolang.Phi;

/**
* The 'inet_addr' WS2_32 function call.
* @see <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-inet_addr">here for details</a>
* @since 0.40.0
* @checkstyle AbbreviationAsWordInNameCheck (100 lines)
*/
public final class InetAddrFuncCall implements Syscall {
/**
* Win32 object.
*/
private final Phi win;

/**
* Ctor.
* @param win Win32 object
*/
public InetAddrFuncCall(final Phi win) {
this.win = win;
}

@Override
public Phi make(final Phi... params) {
final Phi result = this.win.take("return").copy();
result.put(
0,
new Data.ToPhi(
Integer.reverseBytes(
Winsock.INSTANCE.inet_addr(new Dataized(params[0]).asString())
)
)
);
result.put(1, new PhDefault());
return result;
}
}
Loading

0 comments on commit 9b3e241

Please sign in to comment.