This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
/
server.go
141 lines (120 loc) · 3.36 KB
/
server.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"os"
"log"
"strconv"
"path/filepath"
"syscall"
"net"
"flag"
)
// global parameter :D
var counter int = 0
var pid int
// this func will check for open file discriptor of the pid
func check_open_fd() uint64 {
pid_string := strconv.Itoa(pid)
// fmt.Println(pid_string)
path := "/proc/" + pid_string + "/fd"
// log.Println("the path is : ", path)
err := os.Chdir(path)
if err != nil {
log.Fatal( "L:20 | os.Chdir | ", err )
}
contents, err := filepath.Glob("*")
if err != nil {
log.Fatal( "L:41 | filepath.Glob | ", err )
}
return uint64(len(contents))
}
//this func will handle the new connection and the stream they have
func handleConn(c net.Conn) {
var data []byte
counter++
log.Println(c.RemoteAddr())
log.Println(c.LocalAddr())
log.Println("-----", counter, "-----")
log.Println()
c.Read(data)
analyze_iso_msg(data)
log.Printf("\n\n")
defer c.Close()
}
// this function will start listener
func start_listener(user_port string) net.Listener {
var port string = ":" + user_port
l, err := net.Listen("tcp", port)
if err != nil {
log.Fatal("net.Listen | ", err)
}
return l
}
func server_func(port *string) {
// check for max_allowed ?
var rlimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit)
if err != nil {
log.Fatal( "syscall.Getrlimit | ", err )
}
// if server run the server
l := start_listener(*port)
defer l.Close()
log.Println("the listener started on port " + *port )
// now we need to accept the comming req
// but B4 that we need to check for openfd's
for {
// refresh the number of openfd's
nofile := check_open_fd()
// log.Printf("the new number of openfd's %d\n", nofile)
// check them ?
if rlimit.Cur > nofile {
// if no problem accept the new connection
c, err := l.Accept()
if err != nil {
log.Fatal("l.Accept " , err)
}
// defer c.Close()
go handleConn(c)
}
}
}
// this is the main function
func main() {
// get pid of this app
pid = os.Getpid()
// add the flags needed ?
server_pbool := flag.Bool("server", false,
"for running as sever only one of client or server must be present.")
client_pbool := flag.Bool("client", false,
"for testing the server. only one of client or server must be present.")
port_pstring := flag.String("port", "9999",
"the port we should listen on")
address_pstring := flag.String("addr", "localhost",
"the address client should cnnect to.")
// parse the flags
// but remember to add the flags before this
flag.Parse()
// we need to know if we should run as server or client
if flag.NFlag() < 1 {
flag.Usage()
os.Exit(1)
}
// lets c if we have the flags :D
if *client_pbool {
if *server_pbool {
flag.Usage()
os.Exit(1)
}
// if client , run test the client
client(address_pstring, port_pstring)
} else if *server_pbool {
if *client_pbool {
flag.Usage()
os.Exit(1)
}
server_func(port_pstring)
} else {
flag.Usage()
os.Exit(1)
}
}