From 311b705fa95782b6638a376bdd3e34afec63c464 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Sat, 25 Feb 2023 09:31:23 +0100 Subject: [PATCH] Disable TCP client lib --- Capy64/Runtime/Libraries/TCP.cs | 46 ++++++++++++++++++++----- Capy64/Runtime/Objects/Socket.cs | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 Capy64/Runtime/Objects/Socket.cs diff --git a/Capy64/Runtime/Libraries/TCP.cs b/Capy64/Runtime/Libraries/TCP.cs index f41dedf..5174af0 100644 --- a/Capy64/Runtime/Libraries/TCP.cs +++ b/Capy64/Runtime/Libraries/TCP.cs @@ -14,18 +14,21 @@ // limitations under the License. using Capy64.API; +using Capy64.Runtime.Objects; using KeraLua; +using System.IO; using System.Net.Sockets; -using System.Threading.Tasks; namespace Capy64.Runtime.Libraries; public class TCP : IComponent { + private static int Counter = 0; private static IGame _game; public TCP(IGame game) { _game = game; + Counter = 0; } private static LuaRegister[] TCPLib = new LuaRegister[] @@ -40,14 +43,14 @@ public class TCP : IComponent public void LuaInit(Lua L) { - L.RequireF("tcp", OpenLib, false); + //L.RequireF("tcp", OpenLib, false); } public int OpenLib(nint state) { var L = Lua.FromIntPtr(state); L.NewLib(TCPLib); - return 0; + return 1; } private static int L_Connect(nint state) @@ -58,17 +61,42 @@ public class TCP : IComponent var port = (int)L.CheckInteger(2); L.ArgumentCheck(port >= 0 && port <= 0xffff, 2, "port must be in range 0-65535"); - var client = new TcpClient(host, port); - + var client = new TcpClient(); + var id = Counter++; var task = client.ConnectAsync(host, port); - task.ContinueWith(t => { - if(client.Connected) + + task.ContinueWith(t => + { + if (client.Connected) { - // todo: make logic do stuff + _game.LuaRuntime.QueueEvent("tcp_connect", LK => + { + var handle = new FileHandle + { + Stream = client.GetStream(), + DefaultSize = client.ReceiveBufferSize, + }; + + LK.PushInteger(id); + ObjectManager.PushObject(L, handle); + L.SetMetaTable("file"); + return 2; + }); + } + else + { + _game.LuaRuntime.QueueEvent("tcp_failure", LK => + { + LK.PushInteger(id); + LK.PushString(t.Exception.Message); + return 2; + }); } }); - return 0; + L.PushInteger(id); + + return 1; } } diff --git a/Capy64/Runtime/Objects/Socket.cs b/Capy64/Runtime/Objects/Socket.cs new file mode 100644 index 0000000..817f425 --- /dev/null +++ b/Capy64/Runtime/Objects/Socket.cs @@ -0,0 +1,59 @@ +// This file is part of Capy64 - https://github.com/Ale32bit/Capy64 +// Copyright 2023 Alessandro "AlexDevs" Proto +// +// Licensed 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 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Capy64.API; +using KeraLua; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Capy64.Runtime.Objects; + +public class Socket : IDisposable +{ + public void Dispose() + { + GC.SuppressFinalize(this); + } +} + +public class SocketLib : IComponent +{ + private static IGame _game; + public SocketLib(IGame game) + { + + } + + private static LuaRegister[] Methods = new LuaRegister[] { + + new(), + }; + + private static LuaRegister[] MetaMethods = new LuaRegister[] { + new() + { + name = "__index", + }, + new(), + }; + + public void LuaInit(Lua L) + { + + } +} \ No newline at end of file