Author Archives: Robins

C++: Implementation of multi-channel IO transfer with select

Basic idea of select

Set LFD as port reuse

The file descriptors that need to be monitored are sent to the kernel for listening through select. When an event occurs, the number of events is returned through select. Then scan the set of file descriptors one by one to process the event.

Include file:

< sys/select.h>

The function structure of select is as follows:

int select(int nfds, fd_ set * readfds, fd_ set *writefds, fd_ set *exceptfds, struct timeval *timeout);

Parameter description

fd_ Set: the structure of file descriptor set. It is a 1024 bitmap. 0 means that there is no event and 1 means that there is an event. The converted format is:
typedef struct
{
long int__ fds_ bits[1024/(8*8))];
}
NFDS: the maximum number of file descriptors being monitored is + 1
readfds: read set is an incoming and outgoing parameter. The incoming parameter is the set of file descriptors that need to be read and monitored, and the outgoing parameter is the changed file descriptor<
writefds: write file descriptor (same as above, incoming and outgoing parameters)
execptfds: exception file descriptor (same as above, incoming and outgoing parameters)
timeout:
0: no blocking, return immediately after scanning
greater than 0: blocking waiting time is long, return immediately when no event occurs before arrival time
null: permanent blocking wait event
Return:
– 1: listening failure
greater than 0: number of events

Correlation bit operation

1. Remove FD from set
void FD_ CLR(int fd,set *set);
2. Judge whether the descriptor is in the set
2_ ISSET(int fd,fd_ set *set);
3. Put descriptors into the set
void FD_ SET(int fd,fd_ Set * set)
4. Empty set
void FD_ ZERO(fd_ set *set)

Advantages and disadvantages of select

Advantages: cross platform, supported on both windows and Linux
disadvantages: it involves copying back and forth between user area and kernel area. When there are many links but few active users, the efficiency is low, and the maximum number of listeners can not exceed 1024.

Select code

Insert code snippet here
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>	
#include<netinet/in.h>
#include<ctype.h>
#include<sys/socket.h>
#include<sys/select.h>

#include<iostream>
#include<string>

using namespace  std;
int main()
{
    /*1.create socket*/
    int lfd=socket(AF_INET,SOCK_STREAM,0);
    if(lfd<0)
    {
        perror("socket error!");
        return -1;
    }
    /*Setting up port multiplexing*/
    int opt=1;
    setsockopt(lfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(int));
    /*Initialize server-side address structure*/
    struct sockaddr_in  sevr;
    bzero(&sevr,sizeof(sevr));
    sevr.sin_family=AF_INET;
    sevr.sin_port=htons(9090);
    sevr.sin_addr.s_addr=htonl(INADDR_ANY);

    /*Binding socket address structure*/
    int ret=bind(lfd,(sockaddr *)&sevr,sizeof(sockaddr));
    if(ret<0)
    {
        perror("bind error!");
        return -1;
    }
    /*Listening to events*/
    int re= listen(lfd,128);

    /*Create a collection of listened read events*/
    fd_set readfds;
    fd_set tmpfds;

    /*Initialization*/
    FD_ZERO(&readfds);
    FD_ZERO(&tmpfds);
    /*Adding file descriptors to the read event set*/
    FD_SET(lfd,&readfds);
    int maxfd=lfd+1;
    int cfd;
    while(1)
    {
        tmpfds=readfds;
        /*Give the event descriptors in the collection to the kernel to listen */
        /* Listening range, max file descriptors + 1*/
        int nready=select(maxfd+1,&tmpfds,NULL,NULL,NULL);
        cout<<nready<<endl;
        if(nready<0)
        {
            /*Event Terminal*/
            if(errno==EINTR)
                continue;
	    cout<<"select error"<<endl;
            break;
        }

        /*There is a client connection request*/
        if(FD_ISSET(lfd,&tmpfds))
        {
            cfd=accept(lfd,NULL,NULL);
            if(cfd<=0)
            {
                cout<<"accept error"<<endl;
                return -1;
            }
            /*Add cfd to the listener set */
            FD_SET(cfd,&readfds);

            /*Modify the value of maxfds*/
            if(maxfd<=cfd)
            {
                maxfd=cfd;
            }
             /* Only listen events, no read content events*/
            if(--nready==0)
            {
                cout<<"continue"<<endl;
                continue;
            }
        }

        /*Polling to find out the descriptors of read events*/
        for(int i=0;i<=maxfd;i++)
        {
            cout<<i <<endl;
            if(FD_ISSET(i,&tmpfds))
            {
                cout<<"is in cfds"<<endl;
                char buf[1024]={0};
                int n=read(i,buf,sizeof(buf));
                if(n<=0)
                {
                    /*close*/
                    cout<<"read error"<<endl;
                    close(i);
                    /*dele*/
                    FD_CLR(i,&readfds);
                }
                else
                {
                    cout<<buf<<endl;
                    for(int k=0;k<n;k++)
                    {
                        buf[k]=toupper(buf[k]);
                    }
                    write(i,buf,n);
                }

            }
        }
    }

    close(lfd);
    close(cfd);
    return 0;
}

[Nginx] solution: it can’t be accessed on the background API interface after HTTPS (access the specified port through the domain name)

Demand

Original address: http://ip :54774/api_ Name
now requires you to access the specified port through the domain name: https://api.example.com/api_ name
realization

The most important thing is to configure the reverse proxy address of location

When we enter the domain name/API_ Name
will be mapped by nginx to IP or domain name: 54774/API_ Name path go to the nginx directory, open nginx. Conf , add reverse proxy :

server
{
    listen 80;
	listen 443 ssl http2;
    server_name https://api.example.com;
    
    # Reverse Proxy
    location ~ ^/api_name {
        proxy_pass http://ip:54777;
    }
    
    #SSL-START SSL-related configuration, please do not delete or modify the next line with the comment 404 rules
    #error_page 404/404.html;
    ssl_certificate    /www/server/panel/vhost/cert/api.example.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/api.example.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;


    #SSL-END
    
    #ERROR-PAGE-START  Error page configuration, which can be commented, deleted or modified
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP reference configuration, can be commented or modified
    #include enable-php-72.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL rewrite rule reference, the modification will cause the panel to set the pseudo-static rules are invalid
    #include /www/server/panel/vhost/rewrite/api.example.com.conf;
    #REWRITE-END
    
    #Files or directories to which access is prohibited
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #One Click Application for SSL Certificate Verification Directory Related Settings
    location ~ \.well-known{
        allow all;
    }
}
    1. overload configuration file:
./nginx -s reload

Python Grpc Error: A file with this name is already in the pool

1.Error:
rank.proto: A file with this name is already in the pool.

[E 210422 14:48:32 flask_server:87] grpc request execution failed, details: Couldn't build proto file into descriptor pool!
    Invalid proto descriptor for file "rank.proto":
      rank.proto: A file with this name is already in the pool.

2. Solutions

(1) Check the current protobuf version, assuming it is 3.12.2
pip3 show protobuf

(2)Uninstall this version
pip uninstall protobuf

(3)Use the following to reinstall the corresponding version
pip install --no-binary protobuf protobuf==3.12.2

Due to multi process — pychar debug breakpoint debugging encounter pychar dataloader will be stuck

num_ Works parameters   This is the usage of the process.

 
num_ When works is nonzero, the for loop will be stuck here.

 
Changed to 0, ready to run.

Add the num parameter in the dataloader_ Workers can be set to 0

Resources
debugger freezes stepping forward when using torch with workers (multiprocessing)
Zhihu – torch dataloader uses batch and num_ What is the principle of works parameter?Principle analysis
CSDN – pytorch training encountered in the stuck stop and other problems (recommended)
using the python dataloader stuck error solution
dataloader, when num_ worker > 0, there is a bug
to solve the pytorch dataloader num_ Problems of workers  

[error record] the tinker hot fix example runs with an error (patch receive fail: / storage / simulated / 0 / patch)_ signed_ 7zip.apk, code: -2)

Contents of articles

1、 2. Solutions

Refer to [Android hotfix] to run Tinker’s official example blog;

1、 Error information


In Tinker hotfix, the generated patch package app debug patch_ signed_ 7zip.apk to the root directory,

The following error occurred when trying to hot repair;

2021-04-23 22:52:50.533 22855-22855/tinker.sample.android V/Tinker.SamplePatchListener: receive a patch file: /storage/emulated/0/patch_signed_7zip.apk, file size:0
2021-04-23 22:52:50.536 22855-22855/tinker.sample.android I/Tinker.DefaultLoadReporter: patch loadReporter onLoadPatchListenerReceiveFail: patch receive fail: /storage/emulated/0/patch_signed_7zip.apk, code: -2

2、 Solutions


Pay attention to the patch package loading path, and tinker will automatically load the patch in the root directory_ signed_ 7zip.apk file;

receive a patch file: /storage/emulated/0/patch_signed_7zip.apk, file size:0

Run the tinkerpatchdebug gradle task, and the generated patch package name is app debug patch_ signed_ 7zip.apk ,

Blind lead, wasted dozens of minutes, looking for errors;

Add the app debug patch_ signed_ 7zip.apk renamed patch_ signed_ 7zip.apk, and then copy it to the root directory of mobile phone SD card;

Net Q & A: how to avoid the exception thrown by max() on emptyenumerable?

Consultation area

Naor:

I have the following query:


int maxShoeSize = Workers.Where(x => x.CompanyId == 8)
                          .Max(x => x.ShoeSize);

If workers. Where (x = & gt; x. Companyid = = 8) if no workers are found, the above code will throw an exception.

Now the idea is: query can return 0 if it can't be found, but don't throw an exception. How can I modify the query above?

Answer area

Ron K.:

You can use the extension method of IEnumerable defaultifempty() to avoid this embarrassment. Refer to the following code.


    class Program
    {
        static void Main(string[] args)
        {
            List<Worker> Workers = new List<Worker>()
            {
                new Worker(){ CompanyId=1, CompanyName="tweet", ShoeSize=10 },
                new Worker(){ CompanyId=2, CompanyName="google", ShoeSize=20 },
            };

            int maxShoeSize = Workers.Where(x => x.CompanyId == 8)
                          .Select(x => x.ShoeSize)
                          .DefaultIfEmpty(0)
                          .Max();

            Debug.WriteLine($"maxShoeSize={maxShoeSize}");
        }

    }

    class Worker
    {
        public int CompanyId { get; set; }

        public string CompanyName { get; set; }

        public int ShoeSize { get; set; }
    }

Output results:


maxShoeSize=0

Of course, the above 0 is not necessary. You can change it to any other number.

CptRobby:

Although the plan provided by the man upstairs can work normally, it doesn't look very eye-catching. It can be transformed into the following one.


    int maxShoeSize = Workers.Where(x => x.CompanyId == 8)
                             .Select(x => (int?)x.ShoeSize)
                              .Max() ?? 0;

Does the code look a little lengthy?The best way is to customize a extension method , as shown in the following code:


public static int MaxOrDefault<T>(this IQueryable<T> source, Expression<Func<T, int?>> selector, int nullValue = 0)
{
    return source.Max(selector) ?? nullValue;
}

For simplicity, this extension only deals with the int type. You can change it to any type, such as: (long, double,...), and then you can continue to reform the caller.


int maxShoeSize = Workers.Where(x => x.CompanyId == 8).MaxOrDefault(x => x.ShoeSize);

I hope my answer can help more people.

Comment area

Xiaobian never dares to do Max on the empty collection , after all, it's not once or twice, so every time we judge whether there is a value in the collection in advance, and then execute Max , we didn't expect that there are magic extension methods defaultifempty and empty type that can help us to do it, and we all forget about sensory science????????????.

invalid connection string format, a valid format is host:ip:port

After Oracle 11.2.0.4 database is upgraded to 19C, the original interface program reports an error: invalid connection string format, a valid format is host:ip :port

The reason is: Java variables refer to classes12. Jar and Ojdbc14. Jar, which leads to conflicts

Solution: after removing classes12. Jar, it will be normal.

In addition, the program should use JDK1.8 and ojdbc8.jar or above synchronously, which is a big change.

Relevant information:

The driver package classes12.jar is used for JDK 1.2 and JDK 1.3, while ojdbc14.jar is used for JDK 1.4

https://sqlora.blog.csdn.net/article/details/112985085

Error in go running: cannot find package “

When the file is imported, the case of the path is not correct. It was compiled under Mac, but not under docker and CentOS.

===

An error is reported in the operation as follows:

[root@localhost ginlaravel]# go run server.go
routes/route.go:19:2: cannot find package "." in:
	/home/wwwroot/go/src/ginlaravel/app/http/Controller
routes/route.go:20:2: cannot find package "." in:
	/home/wwwroot/go/src/ginlaravel/app/http/Controller/Gen1Controller

Or an error will be reported when running as follows:

[root@localhost ginlaravel]# go build -mod=mod
routes/route.go:19:2: package ginlaravel/app/http/Controller is not in GOROOT (/usr/local/go/src/ginlaravel/app/http/Controller)
routes/route.go:20:2: package ginlaravel/app/http/Controller/Gen1Controller is not in GOROOT (/usr/local/go/src/ginlaravel/app/http/Controller/Gen1Controller)

In fact, the above two are written in lowercase for the uppercase HTTP of the custom space naming path.

Appium step pit summary — solution

1. There are appium command version and appium desktop version in the computer, After opening appium desktop version, the script will run with an error:
selenium.common.exceptions.webdriverexception: Message: an unknown server-side error occurred while processing the command. Original error: cannot start the ‘com. XXXX’ application.
main meaning: an unknown server-side error occurred while processing the command

Check:
I found that the command line version (1.20.2) is inconsistent with the desktop version (1.14.1), appium – V, view version

Solution:
close the desktop version, enter appium in the CMD to execute the command line version, execute the script, and it can run successfully
solution 2: upgrade the desktop appium version or reduce the appium command line version

Can’t connect to MySQL server on ‘192.168.64.132‘

Sqlog failed to connect to MySQL database in Linux system
error message: can’t connect to MySQL server on ‘192.168.64.132’

It can be connected before, but it is not enough to install the virtual machine image. According to the data, you can use SSH connection, and the configuration is as follows:

If you don’t use SSH connection, you need to use win’s telnet to test whether telnet 192.168.64.132 3306 and telnet 192.168.64.132 22 can connect 1, the virtual machine does not set IPv4 forwarding,
2, the virtual machine opens the firewall and does not expose port 3306( 22 can be connected, 3306 can not be connected).

Reference blog

Error in mounted hook: “TypeError: Cannot read property ‘init‘ of undefined“

Error in mounted hook: “TypeError: Cannot read property ‘init’ of undefined”

let myChart = this.$echarts.init(document.getElementById('radar1'))

Error reason: ecarts version is too high, after unloading. Install lower version:

cnpm install [email protected] -S

  However, it is found that there are still errors:

Error: ENOENT: no such file or directory, open

terms of settlement:

Restart the service. Recompile!