|
1 |
| -using System.Net.Sockets; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Sockets; |
| 6 | +using System.Text; |
2 | 7 |
|
3 |
| -namespace MCQuery_Lib |
| 8 | +namespace MCQuery |
4 | 9 | {
|
5 | 10 | public class Connection
|
6 | 11 | {
|
7 |
| - public Connection(string address, string port) |
8 |
| - { |
9 |
| - TcpClient client = new TcpClient(address, int.Parse(port)); |
| 12 | + private byte[] Magic = { 0xFE, 0xFD }; |
| 13 | + private byte Handshake = 0x09; |
| 14 | + private byte Stat = 0x00; |
| 15 | + private byte[] SessionID = { 0x01, 0x01, 0x01, 0x01 }; |
10 | 16 |
|
| 17 | + public Connection(string address, int port) |
| 18 | + { |
11 | 19 | try
|
12 | 20 | {
|
13 |
| - // client.Connect(); |
| 21 | + string udpResponse = sendByUdp(address, port); |
| 22 | + |
| 23 | + if (udpResponse.Equals(String.Empty)) |
| 24 | + { |
| 25 | + string tcpResponse = sendByTcp(address, port); |
| 26 | + |
| 27 | + if (tcpResponse.Equals(String.Empty)) throw new NotImplementedException(); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + // Uses the IPEndPoint object to determine which of these two hosts responded. |
| 32 | + Console.WriteLine("This is the message you received " + |
| 33 | + udpResponse.ToString()); |
| 34 | + Console.WriteLine("This message was sent from " + |
| 35 | + address + |
| 36 | + " on their port number " + |
| 37 | + port.ToString()); |
| 38 | + } |
14 | 39 | }
|
15 | 40 | catch (SocketException socketException)
|
16 | 41 | {
|
17 |
| - //TODO:Do something with exception... |
| 42 | + throw new SocketException(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public Server getServer() |
| 47 | + { |
| 48 | + //TODO: Return server object with fetched data from the request. |
| 49 | + return new Server("dummy", true); |
| 50 | + } |
| 51 | + |
| 52 | + public string sendByUdp(string address, int port) |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + //Check if address is a domain name. |
| 57 | + if (isDomainAddress(address)) |
| 58 | + { |
| 59 | + address = getIpFromDomain(address); |
| 60 | + } |
| 61 | + |
| 62 | + UdpClient udpClient = new UdpClient(); |
| 63 | + |
| 64 | + udpClient.Connect(address, port); |
| 65 | + |
| 66 | + List<byte> message = new List<byte>(); |
| 67 | + message.AddRange(Magic); |
| 68 | + message.Add(Handshake); |
| 69 | + message.Add(Stat); |
| 70 | + message.AddRange(SessionID); |
| 71 | + |
| 72 | + Byte[] handshakeMessage = message.ToArray().ToArray(); |
| 73 | + |
| 74 | + udpClient.Send(handshakeMessage, handshakeMessage.Length); |
| 75 | + |
| 76 | + //IPEndPoint object will allow us to read datagrams sent from any source. |
| 77 | + IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse(address), port); |
| 78 | + |
| 79 | + // Blocks until a message returns on this socket from a remote host. |
| 80 | + Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); |
| 81 | + string returnData = Encoding.ASCII.GetString(receiveBytes); |
| 82 | + |
| 83 | + udpClient.Close(); |
| 84 | + |
| 85 | + if (returnData.Equals(String.Empty)) |
| 86 | + { |
| 87 | + return String.Empty; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + return returnData; |
| 92 | + } |
| 93 | + } |
| 94 | + catch (SocketException exception) |
| 95 | + { |
| 96 | + throw new SocketException(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private string getIpFromDomain(string address) |
| 101 | + { |
| 102 | + IPAddress[] addresses = Dns.GetHostAddresses(address); |
| 103 | + |
| 104 | + return addresses[0].AddressFamily.ToString(); |
| 105 | + } |
| 106 | + |
| 107 | + private bool isDomainAddress(string address) |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + IPHostEntry hostEntry = Dns.GetHostEntry(address); |
| 112 | + |
| 113 | + if (hostEntry.HostName == address) |
| 114 | + { |
| 115 | + return true; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + catch (Exception exception) |
| 123 | + { |
| 124 | + Console.WriteLine(exception.Message); |
| 125 | + throw new SocketException(); |
18 | 126 | }
|
19 | 127 | }
|
| 128 | + |
| 129 | + public string sendByTcp(string address, int port) |
| 130 | + { |
| 131 | + //TODO: Implement sending packet by TCP. |
| 132 | + return String.Empty; |
| 133 | + } |
20 | 134 | }
|
21 | 135 | }
|
0 commit comments