aboutsummaryrefslogtreecommitdiffstats
path: root/AndroidAsync/src/com/koushikdutta/async/AsyncDatagramSocket.java
blob: 8e6fd54c2fe9be7a8c1931a282f13f215d1e19ed (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
package com.koushikdutta.async;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

public class AsyncDatagramSocket extends AsyncNetworkSocket {
    public void disconnect() throws IOException {
        socketAddress = null;
        ((DatagramChannelWrapper)getChannel()).disconnect();
    }

    @Override
    public InetSocketAddress getRemoteAddress() {
        if (isOpen())
            return super.getRemoteAddress();
        return ((DatagramChannelWrapper)getChannel()).getRemoteAddress();
    }

    public void connect(InetSocketAddress address) throws IOException {
        socketAddress = address;
        ((DatagramChannelWrapper)getChannel()).mChannel.connect(address);
    }

    public void send(final String host, final int port, final ByteBuffer buffer) {
        if (getServer().getAffinity() != Thread.currentThread()) {
            getServer().run(new Runnable() {
                @Override
                public void run() {
                    send(host, port, buffer);
                }
            });
            return;
        }

        try {
            ((DatagramChannelWrapper)getChannel()).mChannel.send(buffer, new InetSocketAddress(host, port));
        }
        catch (IOException e) {
//            close();
//            reportEndPending(e);
//            reportClose(e);
        }

    }
    public void send(final InetSocketAddress address, final ByteBuffer buffer) {
        if (getServer().getAffinity() != Thread.currentThread()) {
            getServer().run(new Runnable() {
                @Override
                public void run() {
                    send(address, buffer);
                }
            });
            return;
        }

        try {
            int sent = ((DatagramChannelWrapper)getChannel()).mChannel.send(buffer, new InetSocketAddress(address.getHostName(), address.getPort()));
        }
        catch (IOException e) {
//            Log.e("SEND", "send error", e);
//            close();
//            reportEndPending(e);
//            reportClose(e);
        }
    }
}