summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/NetworkManager.java
blob: d143fc86f6b23fcc34faf31a3b7482260c63da21 (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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package net.minecraft.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NetworkManager {

    public static final Object a = new Object();
    public static int b;
    public static int c;
    private Object e = new Object();
    private Socket f;
    private final SocketAddress g;
    private DataInputStream h;
    private DataOutputStream i;
    private boolean j = true;
    private List k = Collections.synchronizedList(new ArrayList());
    private List l = Collections.synchronizedList(new ArrayList());
    private List m = Collections.synchronizedList(new ArrayList());
    private NetHandler n;
    private boolean o = false;
    private Thread p;
    private Thread q;
    private boolean r = false;
    private String s = "";
    private Object[] t;
    private int u = 0;
    private int v = 0;
    public int d = 0;
    private int w = 50;

    public NetworkManager(Socket socket, String s, NetHandler nethandler) {
        this.f = socket;
        this.g = socket.getRemoteSocketAddress();
        this.n = nethandler;

        // Craftbukkit start
        try {
            socket.setSoTimeout(30000);
            socket.setTrafficClass(24);
            this.h = new DataInputStream(socket.getInputStream());
            this.i = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(NetworkManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        // Craftbukkit end

        this.q = new NetworkReaderThread(this, s + " read thread");
        this.p = new NetworkWriterThread(this, s + " write thread");
        this.q.start();
        this.p.start();
    }

    public void a(NetHandler nethandler) {
        this.n = nethandler;
    }

    public void a(Packet packet) {
        if (!this.o) {
            Object object = this.e;

            synchronized (this.e) {
                this.v += packet.a() + 1;
                if (packet.k) {
                    this.m.add(packet);
                } else {
                    this.l.add(packet);
                }
            }
        }
    }

    private void e() {
        try {
            boolean flag = true;
            Object object;
            Packet packet;

            if (!this.l.isEmpty() && (this.d == 0 || System.currentTimeMillis() - ((Packet) this.l.get(0)).j >= (long) this.d)) {
                flag = false;
                object = this.e;
                synchronized (this.e) {
                    packet = (Packet) this.l.remove(0);
                    this.v -= packet.a() + 1;
                }

                Packet.a(packet, this.i);
            }

            if ((flag || this.w-- <= 0) && !this.m.isEmpty() && (this.d == 0 || System.currentTimeMillis() - ((Packet) this.m.get(0)).j >= (long) this.d)) {
                flag = false;
                object = this.e;
                synchronized (this.e) {
                    packet = (Packet) this.m.remove(0);
                    this.v -= packet.a() + 1;
                }

                Packet.a(packet, this.i);
                this.w = 50;
            }

            if (flag) {
                Thread.sleep(10L);
            }
        } catch (InterruptedException interruptedexception) {
            ;
        } catch (Exception exception) {
            if (!this.r) {
                this.a(exception);
            }
        }
    }

    private void f() {
        try {
            Packet packet = Packet.b(this.h);

            if (packet != null) {
                this.k.add(packet);
            } else {
                this.a("disconnect.endOfStream", new Object[0]);
            }
        } catch (Exception exception) {
            if (!this.r) {
                this.a(exception);
            }
        }
    }

    private void a(Exception exception) {
        exception.printStackTrace();
        this.a("disconnect.genericReason", new Object[] { "Internal exception: " + exception.toString()});
    }

    public void a(String s, Object... aobject) {
        if (this.j) {
            this.r = true;
            this.s = s;
            this.t = aobject;
            (new NetworkMasterThread(this)).start();
            this.j = false;

            try {
                this.h.close();
                this.h = null;
            } catch (Throwable throwable) {
                ;
            }

            try {
                this.i.close();
                this.i = null;
            } catch (Throwable throwable1) {
                ;
            }

            try {
                this.f.close();
                this.f = null;
            } catch (Throwable throwable2) {
                ;
            }
        }
    }

    public void a() {
        if (this.v > 1048576) {
            this.a("disconnect.overflow", new Object[0]);
        }

        if (this.k.isEmpty()) {
            if (this.u++ == 1200) {
                this.a("disconnect.timeout", new Object[0]);
            }
        } else {
            this.u = 0;
        }

        int i = 100;

        while (!this.k.isEmpty() && i-- >= 0) {
            Packet packet = (Packet) this.k.remove(0);

            packet.a(this.n);
        }

        if (this.r && this.k.isEmpty()) {
            this.n.a(this.s, this.t);
        }
    }

    public SocketAddress b() {
        return this.g;
    }

    public void c() {
        this.o = true;
        this.q.interrupt();
        (new ThreadMonitorConnection(this)).start();
    }

    public int d() {
        return this.m.size();
    }

    static boolean a(NetworkManager networkmanager) {
        return networkmanager.j;
    }

    static boolean b(NetworkManager networkmanager) {
        return networkmanager.o;
    }

    static void c(NetworkManager networkmanager) {
        networkmanager.f();
    }

    static void d(NetworkManager networkmanager) {
        networkmanager.e();
    }

    static Thread e(NetworkManager networkmanager) {
        return networkmanager.q;
    }

    static Thread f(NetworkManager networkmanager) {
        return networkmanager.p;
    }
}