Example #1
0
        //(u32 domain, u32 type, u32 protocol) -> (i32 ret, u32 bsd_errno)
        public long Socket(ServiceCtx Context)
        {
            SocketBsd NewBSDSocket = new SocketBsd
            {
                Family   = Context.RequestData.ReadInt32(),
                Type     = Context.RequestData.ReadInt32(),
                Protocol = Context.RequestData.ReadInt32()
            };

            Sockets.Add(NewBSDSocket);

            Sockets[Sockets.Count - 1].Handle = new Socket((AddressFamily)Sockets[Sockets.Count - 1].Family,
                                                           (SocketType)Sockets[Sockets.Count - 1].Type,
                                                           (ProtocolType)Sockets[Sockets.Count - 1].Protocol);

            Context.ResponseData.Write(Sockets.Count - 1);
            Context.ResponseData.Write(0);

            return(0);
        }
Example #2
0
        //(u32 socket) -> (i32 ret, u32 bsd_errno, u32 addrlen, buffer<sockaddr, 0x22, 0> addr)
        public long Accept(ServiceCtx Context)
        {
            int  SocketId      = Context.RequestData.ReadInt32();
            long AddrBufferPtr = Context.Request.ReceiveBuff[0].Position;

            Socket HandleAccept = null;

            var TimeOut = Task.Factory.StartNew(() =>
            {
                try
                {
                    HandleAccept = Sockets[SocketId].Handle.Accept();
                }
                catch (SocketException Ex)
                {
                    Context.ResponseData.Write(-1);
                    Context.ResponseData.Write(Ex.ErrorCode - 10000);
                }
            });

            TimeOut.Wait(10000);

            if (HandleAccept != null)
            {
                SocketBsd NewBSDSocket = new SocketBsd
                {
                    IpAddress = ((IPEndPoint)Sockets[SocketId].Handle.LocalEndPoint).Address,
                    RemoteEP  = ((IPEndPoint)Sockets[SocketId].Handle.LocalEndPoint),
                    Handle    = HandleAccept
                };

                Sockets.Add(NewBSDSocket);

                using (MemoryStream MS = new MemoryStream())
                {
                    BinaryWriter Writer = new BinaryWriter(MS);

                    Writer.Write((byte)0);
                    Writer.Write((byte)Sockets[Sockets.Count - 1].Handle.AddressFamily);
                    Writer.Write((Int16)((IPEndPoint)Sockets[Sockets.Count - 1].Handle.LocalEndPoint).Port);

                    string[] IpAdress = Sockets[Sockets.Count - 1].IpAddress.ToString().Split('.');
                    Writer.Write(byte.Parse(IpAdress[0]));
                    Writer.Write(byte.Parse(IpAdress[1]));
                    Writer.Write(byte.Parse(IpAdress[2]));
                    Writer.Write(byte.Parse(IpAdress[3]));

                    AMemoryHelper.WriteBytes(Context.Memory, AddrBufferPtr, MS.ToArray());

                    Context.ResponseData.Write(Sockets.Count - 1);
                    Context.ResponseData.Write(0);
                    Context.ResponseData.Write(MS.Length);
                }
            }
            else
            {
                Context.ResponseData.Write(-1);
                Context.ResponseData.Write((int)BsdError.ETIMEDOUT);
            }

            return(0);
        }