Ejemplo n.º 1
0
        } // class SocketStream

        // Create a new socket.
        public static int __syscall_socket(int domain, int type)
        {
            AddressFamily family;
            SocketType    socketType;
            ProtocolType  protocol;

            // Convert the parametrs into their C# equivalents.
            if (domain == 2 /*AF_INET*/)
            {
                family = AddressFamily.InterNetwork;
            }
            else if (domain == 10 /*AF_INET6*/)
            {
                family = AddressFamily.InterNetworkV6;
            }
            else
            {
                return(-22);                                    /* EINVAL */
            }
            if (type == 1 /*SOCK_STREAM*/)
            {
                socketType = SocketType.Stream;
                protocol   = ProtocolType.Tcp;
            }
            else if (type == 2 /*SOCK_DGRAM*/)
            {
                socketType = SocketType.Dgram;
                protocol   = ProtocolType.Udp;
            }
            else
            {
                return(-22);                                    /* EINVAL */
            }

            // Create the socket.
            Socket socket;

            try
            {
                socket = new Socket(family, socketType, protocol);
            }
            catch (SocketException)
            {
                return(-22);                                    /* EINVAL */
            }

            // Wrap the socket within a stream.
            Stream stream = new SocketStream(socket);

            // Associate the stream with a file descriptor.
            int fd = FileTable.AllocFD();

            if (fd == -1)
            {
                stream.Close();
                return(fd);
            }
            FileTable.SetFileDescriptor(fd, stream);
            return(fd);
        }
Ejemplo n.º 2
0
        private void ReadStaleData(bool closeStream, bool evenEncrypted)
        {
            if (m_stream != null && m_stream.SocketDataAvailable > 0)
            {
                if (m_stream.IsConnected && (!m_stream.IsEncrypted || evenEncrypted))
                {
                    byte[] buf = new byte[m_stream.SocketDataAvailable];
                    m_stream.RawSocketRead(buf);
                }

                if (closeStream)
                {
                    m_stream.Close();
                }
            }
        }
Ejemplo n.º 3
0
        public void Dispose()
        {
            if (socket.Connected)
            {
                socket.Disconnect(false);
            }

            socket.Dispose();
            SocketStream.Close();
            CryptoProvider.Dispose();
            buffer = null;

            OnHandlerDisposed(this);
        }
Ejemplo n.º 4
0
        // Wrap a socket that was just accepted with a file descriptor.
        public static int __syscall_wrap_accept(Socket socket)
        {
            // Wrap the socket within a stream.
            SocketStream stream = new SocketStream(socket);

            stream.Connected = true;

            // Associate the stream with a file descriptor.
            int fd = FileTable.AllocFD();

            if (fd == -1)
            {
                stream.Close();
                return(fd);
            }
            FileTable.SetFileDescriptor(fd, stream);
            return(fd);
        }
Ejemplo n.º 5
0
        public void Disconnect()
        {
            if (_disconnectedCallback != null)
            {
                try
                {
                    _disconnectedCallback(this);
                }
                catch (Exception e)
                {
                    Log.InvokeExceptionLog(this, e, true);
                }
            }

            lock (_lockObject)
            {
                if (!Connected)
                {
                    return;
                }

                Connected = false;
            }
            Log.InvokeInfoLog(this, $"Socket {ContextName} is Disconnected with Ip:{TcpClient.Client.RemoteEndPoint}. Id=" + Id);

            try
            {
                _cancellationToken.Cancel(true);
            }
            catch (Exception e)
            {
                Log.InvokeExceptionLog(this, e, true);
            }

            try
            {
                SocketStatistic.WeHaveDisconnect();
            }
            catch (Exception e)
            {
                Log.InvokeExceptionLog(this, e, true);
            }

            try
            {
                SocketStream.Close();
            }
            catch (Exception e)
            {
                Log.InvokeExceptionLog(this, e, true);
            }

            try
            {
                TcpClient.Close();
            }
            catch (Exception e)
            {
                Log.InvokeExceptionLog(this, e, true);
            }

            try
            {
                _deliveryPublisherSubscriber.Stop();
            }
            catch (Exception e)
            {
                Log.InvokeExceptionLog(this, e, true);
            }


            Task.Run(ProcessOnDisconnectAsync);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Closes the stream.
 /// </summary>
 /// <param name="stream">The <see cref="NetworkStream" /></param>
 private void CloseStream(SocketStream stream)
 {
     this.Log(this.ToString(), "flush client stream", LogLevel.Debug);
     stream.Close();
     this.OnClientDisconnected?.Invoke(stream.ClientId);
 }