-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (118 loc) · 3.17 KB
/
main.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
142
143
144
145
146
147
148
149
150
package main
import (
"container/heap"
"encoding/binary"
"fmt"
"io"
"log"
"math/rand"
"net"
)
const (
PROXY_SERVER_HOST = "localhost"
PROXY_SERVER_PORT = "9988"
PROXY_SERVER_TYPE = "tcp"
SERVER_HOST = "localhost"
SERVER_PORT = "9989"
SERVER_TYPE = "tcp"
BUFFER_SIZE = 8096
)
func clientHandler(client net.Conn) {
defer func() {
client.Close()
log.Printf("Connection closed with %v\n", client.RemoteAddr())
}()
client_buf := make([]byte, BUFFER_SIZE)
// Connect to the server
server, err := net.Dial(SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
if err != nil {
log.Println(err)
return
}
defer server.Close()
server_buf := make([]byte, BUFFER_SIZE)
// Read image dimensions and forward them to the server
n, err := client.Read(client_buf[:4])
if err != nil {
log.Println(err)
return
}
rows := int(binary.LittleEndian.Uint32(client_buf[:n]))
server.Write(client_buf[:n])
n, err = client.Read(client_buf[:4])
if err != nil {
log.Println(err)
return
}
cols := int(binary.LittleEndian.Uint32(client_buf[:n]))
server.Write(client_buf[:n])
log.Printf("Client %v is sending an image of size: %d x %d\n", client.RemoteAddr(), cols, rows)
// Create a min heap to shuffle packets
h := &PacketHeap{}
heap.Init(h)
for packet := 0; packet < rows; packet++ {
n, err := client.Read(client_buf)
// Send ack msg to client
client.Write([]byte{1})
// First 4 bytes of the packet is the intended packet id and this should be sent to the server.
packet_id := int(binary.LittleEndian.Uint32(client_buf[:4]))
// Create a deep copy to insert in the heap
data := make([]byte, n)
copy(data, client_buf[:n])
switch err {
case io.EOF:
log.Println("EOF")
return
case nil:
log.Printf("Received packet %d\n from %s\n", packet_id, client.RemoteAddr())
// Push packet to the heap with a random priority
heap.Push(h, Packet{Priority: rand.Int(), Content: data})
// Send packet with the lowest priority to the server after every four packets received
if h.Len() > 0 && (packet%4 == 0) {
packet := heap.Pop(h).(Packet)
n, err = server.Write(packet.Content)
if err != nil {
log.Println(err)
return
}
log.Printf("Forwarded %d bytes to server from %s\n", len(packet.Content), client.RemoteAddr())
}
default:
log.Printf("Failed to receive the packet:%s", err)
return
}
}
//Empty the remaining packets
for h.Len() > 0 {
packet := heap.Pop(h).(Packet)
server.Write(packet.Content)
log.Printf("Forwarded %d bytes to server from %s\n", len(packet.Content), client.RemoteAddr())
}
//Forward server response back to the client
n, err = server.Read(server_buf)
if err != nil {
log.Println(err)
return
}
n, err = client.Write(server_buf)
if err != nil {
log.Println(err)
return
}
}
func main() {
fmt.Printf("Listening on port %s...\n", PROXY_SERVER_PORT)
proxy_server, err := net.Listen(PROXY_SERVER_TYPE, PROXY_SERVER_HOST+":"+PROXY_SERVER_PORT)
if err != nil {
log.Fatalf("Error listening on port: %s\n%s", PROXY_SERVER_PORT, err.Error())
}
defer proxy_server.Close()
for {
conn, err := proxy_server.Accept()
if err != nil {
log.Println(err)
continue
}
go clientHandler(conn)
}
}