Skip to content

Commit

Permalink
feat(#3251): socket object
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Sep 24, 2024
1 parent a0ad01b commit f339985
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 6 deletions.
2 changes: 1 addition & 1 deletion eo-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ SOFTWARE.
<version>3.0.22</version>
</dependency>
</dependencies>
</plugin>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin>
</plugins>
</build>
<profiles>
Expand Down
2 changes: 2 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/i16.eo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
as-bytes > @
$ > as-i16
$.times -1.as-i64.as-i32.as-i16 > neg
$.as-i32.as-i64 > as-i64
$.as-i64.as-number > as-number

# Convert this `i16` to `i32`.
# The object is an atom because it's not possible to check what
Expand Down
1 change: 1 addition & 0 deletions eo-runtime/src/main/eo/org/eolang/i32.eo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
as-bytes > @
$ > as-i32
$.times -1.as-i64.as-i32 > neg
$.as-i64.as-number > as-number

# Convert this `i32` to `i64`.
# The object is an atom because it's not possible to check what
Expand Down
1 change: 1 addition & 0 deletions eo-runtime/src/main/eo/org/eolang/i64.eo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
as-bytes > @
$ > as-i64
$.times -1.as-i64 > neg
$.as-i32.as-i16 > as-i16

# Convert this `i64` to `i32`.
# The `error` is returned if the `i64` number is more than
Expand Down
108 changes: 108 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/net/socket.eo
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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.

+alias org.eolang.sys.posix
+alias org.eolang.sys.os
+alias org.eolang.txt.sprintf
+architect [email protected]
+home https://github.com/objectionary/eo
+package org.eolang.net
+rt jvm org.eolang:eo-runtime:0.0.0
+rt node eo2js-runtime:0.0.0
+version 0.0.0

# Socket.
[address port] > socket
if. > @
os.is-windows
win-socket address port
posix-socket address port

[port] > htons
port.as-i16.as-bytes > short-port
as-i16. > @
or.
(short-port.and FF-).left 8
(short-port.right 8).and FF-

[address port] > posix-socket
[scope] > connect
code. > sd
posix
"socket"
* posix.af-inet posix.sock-stream posix.ipproto-tcp
code. > inet-addr
posix
"inet_addr"
* ^.address
if. > inet-addr-as-int
inet-addr.eq posix.inaddr-none
error
sprintf
"Couldn't convert an IPv4 address '%s' into a 32-bit integer via 'inet_addr' posix syscall, reason: %s"
* ^.address strerror
inet-addr.as-i32
posix.sockaddr-in > sockaddr
posix.af-inet.as-i16
^.^.htons port
inet-addr-as-int
code. > connected
posix
"connect"
* sd sockaddr sockaddr.size
code. > closed
posix
"close"
* sd
code. > strerror
posix
"strerror"
* (posix "errno" *).code
if. > @
sd.eq -1
error
sprintf
"Couldn't create a posix socket, reason: %s"
* strerror
try
if.
connected.eq -1
error
sprintf
"Couldn't connect to '%s:%d' on posix socket '%d', reason: %s"
* ^.address ^.port sd strerror
as-bytes.
dataized
scope
^.scoped-posix-socket sd
ex > [ex]
if.
closed.eq -1
error
sprintf
"Couldn't close a posix socket '%d' connected to '%s:%d'"
* sd ^.address ^.port
true

[descriptor] > scoped-posix-socket

[address port] > win-socket
2 changes: 2 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/number.eo
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
as-bytes > @
$ > as-number
$.times -1 > neg
$.as-i64.as-i32 > as-i32
$.as-i32.as-i16 > as-i16

# Convert this `number` to `i64` object.
[] > as-i64 /i64
Expand Down
12 changes: 12 additions & 0 deletions eo-runtime/src/main/eo/org/eolang/sys/posix.eo
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
2 > af-inet
1 > sock-stream
6 > ipproto-tcp
-1 > inaddr-none

[] > @ /return

Expand All @@ -46,3 +47,14 @@
# Here `tv-sec` is seconds since Jan 1, 1970, and `tv-usec` - microseconds.
[tv-sec tv-usec] > timeval
$ > self

# The posix `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 @@ -29,12 +29,14 @@

import EOorg.EOeolang.EOsys.Posix.CloseSyscall;
import EOorg.EOeolang.EOsys.Posix.ConnectSyscall;
import EOorg.EOeolang.EOsys.Posix.ErrnoSyscall;
import EOorg.EOeolang.EOsys.Posix.GetenvSyscall;
import EOorg.EOeolang.EOsys.Posix.GetpidSyscall;
import EOorg.EOeolang.EOsys.Posix.GettimeofdaySyscall;
import EOorg.EOeolang.EOsys.Posix.InetAddrSyscall;
import EOorg.EOeolang.EOsys.Posix.ReadSyscall;
import EOorg.EOeolang.EOsys.Posix.SocketSyscall;
import EOorg.EOeolang.EOsys.Posix.StrerrorSyscall;
import EOorg.EOeolang.EOsys.Posix.WriteSyscall;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -71,6 +73,8 @@ public final class EOposix$EOφ extends PhDefault implements Atom {
EOposix$EOφ.SYS_CALLS.put("close", CloseSyscall::new);
EOposix$EOφ.SYS_CALLS.put("connect", ConnectSyscall::new);
EOposix$EOφ.SYS_CALLS.put("inet_addr", InetAddrSyscall::new);
EOposix$EOφ.SYS_CALLS.put("errno", ErrnoSyscall::new);
EOposix$EOφ.SYS_CALLS.put("strerror", StrerrorSyscall::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public interface CStdLib extends Library {
*/
int inet_addr(String address);

// Function to convert errno to a human-readable string
String strerror(int errnum);
/**
* Converts {@code errno} to a human-readable string.
* @param errno The error number
* @return Error as string
*/
String strerror(int errno);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.Posix; // NOPMD

import com.sun.jna.Native;
import EOorg.EOeolang.EOsys.Syscall;
import org.eolang.Data;
import org.eolang.PhDefault;
import org.eolang.Phi;

/**
* The 'errno' syscall.
* @since 0.40
*/
public final class ErrnoSyscall implements Syscall {
/**
* Posix object.
*/
private final Phi posix;

/**
* Ctor.
* @param posix Posix object
*/
public ErrnoSyscall(final Phi posix) {
this.posix = posix;
}

@Override
public Phi make(final Phi... params) {
final Phi result = this.posix.take("return").copy();
result.put(0, new Data.ToPhi(Native.getLastError()));
result.put(1, new PhDefault());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.Posix; // NOPMD

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

/**
* The 'strerror' syscall.
* @since 0.40
*/
public final class StrerrorSyscall implements Syscall {
/**
* Posix object.
*/
private final Phi posix;

/**
* Ctor.
* @param posix Posix object
*/
public StrerrorSyscall(final Phi posix) {
this.posix = posix;
}

@Override
public Phi make(final Phi... params) {
final Phi result = this.posix.take("return").copy();
result.put(
0,
new Data.ToPhi(CStdLib.INSTANCE.strerror(new Dataized(params[0]).asNumber().intValue()))
);
result.put(1, new PhDefault());
return result;
}
}
Loading

0 comments on commit f339985

Please sign in to comment.