blob: c1e9958269b1cc5e96c5ab7763feda80ebc57a73 (
plain)
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
|
package main
import (
"fmt"
"net"
"os"
"strconv"
"sync"
)
const (
LISTEN = "localhost:3334"
)
type becoming struct {
Fibmutex sync.Mutex
Last [2]int
}
func (b *becoming) Become() {
b.Last = [2]int{0, 0}
l, err := net.Listen("tcp", LISTEN)
if err != nil {
fmt.Println("Error listening: ", err.Error())
os.Exit(1)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
} else {
go handleRequest(conn, b)
}
}
}
func handleRequest(conn net.Conn, b *becoming) {
defer conn.Close()
b.Fibmutex.Lock()
defer b.Fibmutex.Unlock()
if b.Last == [2]int{0, 0} {
conn.Write([]byte(strconv.Itoa(0)))
b.Last[1] = 1
} else {
conn.Write([]byte(strconv.Itoa(b.Last[1])))
sum := b.Last[0] + b.Last[1]
b.Last[0] = b.Last[1]
b.Last[1] = sum
}
}
var Become becoming
|