代码高亮测试

1
2
3
function helloWorld () {
  alert("Hello, World!")
}
1
2
3
4
5
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}
1
2
3
4
5
package hello

fun main(args: Array<String>) {
  println("Hello World!")
}
1
2
3
4
5
6
7
#include <stdio.h>

/* Hello */
int main(void){
  printf("Hello, World!");
  return 0;
}
1
2
3
4
5
6
7
8
// 'Hello World!' program 
 
#include <iostream>
 
int main(){
  std::cout << "Hello World!" << std::endl;
  return 0;
}
1
2
3
4
5
6
using System;
class HelloWorld{
  public static void Main(){ 
    System.Console.WriteLine("Hello, World!");
  }
}
1
2
3
4
5
<html>
<body>
  Hello, World!
</body>
</html>
1
2
3
4
5
6
7
package main
import fmt "fmt"

func main() 
{
   fmt.Printf("Hello, World!\n");
}
1
2
3
object HelloWorld with Application {
  Console.println("Hello, World!");
}
1
2
3
<?php
  echo 'Hello, World!';
?>
1
print("Hello, World!") 
1
2
3
4
(defn hello-world
  "A function print 'Hello world'."
  []
  (prn "Hello world"))
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.

// Author: Shuo Chen (chenshuo at chenshuo dot com)

#include "muduo/net/Acceptor.h"

#include "muduo/base/Logging.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/InetAddress.h"
#include "muduo/net/SocketsOps.h"

#include <errno.h>
#include <fcntl.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#include <unistd.h>

using namespace muduo;
using namespace muduo::net;

Acceptor::Acceptor(EventLoop *loop, const InetAddress &listenAddr, bool reuseport)
    : loop_(loop),
      acceptSocket_(sockets::createNonblockingOrDie(listenAddr.family())), //这里创建非阻塞TCP socket套接字描述符,然后构造这个Socket对象 socket()
      acceptChannel_(loop, acceptSocket_.fd()),                            //此Acceptor的Channel中使用主函数中定义的loop,上面创建的套接字描述符也传给该Channel
      listening_(false),                                                   //服务器没有调用start()之前,不启动监听
      idleFd_(::open("/dev/null", O_RDONLY | O_CLOEXEC))                   //处理描述符用完的情况,需要断开连接
{
    assert(idleFd_ >= 0);
    acceptSocket_.setReuseAddr(true);
    acceptSocket_.setReusePort(reuseport);
    acceptSocket_.bindAddress(listenAddr); //套接字和地址绑定sockets::bindOrDie
    acceptChannel_.setReadCallback(
        std::bind(&Acceptor::handleRead, this));
}

Acceptor::~Acceptor()
{
    acceptChannel_.disableAll();
    acceptChannel_.remove();
    ::close(idleFd_);
}

void Acceptor::listen() //在TcpServer的start()函数开始运行
{
    loop_->assertInLoopThread();
    listening_ = true;
    acceptSocket_.listen();
    acceptChannel_.enableReading();
}

void Acceptor::handleRead()
{
    loop_->assertInLoopThread();
    InetAddress peerAddr;
    //FIXME loop until no more
    int connfd = acceptSocket_.accept(&peerAddr);
    if (connfd >= 0)
    {
        // string hostport = peerAddr.toIpPort();
        // LOG_TRACE << "Accepts of " << hostport;
        if (newConnectionCallback_) //TcpServer构造时绑定的函数是:TcpServer::newConnection
        {
            newConnectionCallback_(connfd, peerAddr);
        }
        else
        {
            sockets::close(connfd);
        }
    }
    else
    {
        LOG_SYSERR << "in Acceptor::handleRead";
        // Read the section named "The special problem of
        // accept()ing when you can't" in libev's doc.
        // By Marc Lehmann, author of libev.
        if (errno == EMFILE)
        {
            ::close(idleFd_);
            idleFd_ = ::accept(acceptSocket_.fd(), NULL, NULL);
            ::close(idleFd_);
            idleFd_ = ::open("/dev/null", O_RDONLY | O_CLOEXEC);
        }
    }
}
WindSun 支付宝支付宝
WindSun 微信微信
0%