public void Disconnect(object socket, int timeout) { if (socket != null) { if (ToPeer.ContainsKey((EndPoint)socket)) { if (SocketList.Contains(ToPeer[(EndPoint)socket])) { SocketList.Remove(ToPeer[(EndPoint)socket]); } } if (SocketToKey.ContainsKey((EndPoint)socket)) { SocketToKey.Remove((EndPoint)socket); } if (ToPeer.ContainsKey((EndPoint)socket)) { ToPeer.Remove((EndPoint)socket); } lock (checklock) { if (clientcheck.ContainsKey((EndPoint)socket)) { clientcheck.Remove((EndPoint)socket); } } } }
public void Close() { if (listener != null) { try { listener.Shutdown(SocketShutdown.Both); } catch (Exception) { } finally { listener.Close(); listener = null; } } SocketList.Clear(); Packets.Clear(); ToPeer.Clear(); SocketToEndPoint.Clear(); SocketToKey.Clear(); GetMessage = null; RSAkey = new EncryptAndCompress.RSAKeyPair(); }
public void Close() { lock (SendLock) { if (listener != null) { try { listener.Shutdown(SocketShutdown.Both); } finally { listener.Close(); listener = null; } } StopThread(); SocketList.Clear(); Packets.Clear(); ToPeer.Clear(); SocketToKey.Clear(); clientcheck.Clear(); OnListenClient.Clear(); GetMessage = null; RSAkey = new EncryptAndCompress.RSAKeyPair(); } }
public override void ToStream(Stream output) { output.Write(TLUtils.SignatureToBytes(Signature)); Flags.ToStream(output); FromPeer.ToStream(output); Id.ToStream(output); RandomIds.ToStream(output); ToPeer.ToStream(output); }
public override byte[] ToBytes() { return(TLUtils.Combine( TLUtils.SignatureToBytes(Signature), Flags.ToBytes(), FromPeer.ToBytes(), Id.ToBytes(), RandomIds.ToBytes(), ToPeer.ToBytes())); }
public void ConnectSuccessful(Func <object, INetServer, PeerBase> AddPeerFunc, Packet packet) { SocketToKey.Add(packet.peer, (string)packet.state); PeerBase peer = AddPeerFunc(packet.peer, this); SocketList.Add(peer); SocketToEndPoint.Add(((Socket)packet.peer), ((Socket)packet.peer).RemoteEndPoint); ToPeer.Add(((Socket)packet.peer).RemoteEndPoint, peer); using (Packet newpacket = new Packet(packet.peer)) { packet.BeginWrite(PacketType.CONNECT_SUCCESSFUL); packet.WriteSendData(new SendData(0, "On Connect"), (string)packet.state, EncryptAndCompress.LockType.AES); Send(packet, packet.peer); } }
public void Disconnect(object socket, int timeout) { if (socket != null) { if (SocketToEndPoint.ContainsKey((Socket)socket)) { if (ToPeer.ContainsKey(SocketToEndPoint[(Socket)socket])) { if (SocketList.Contains(ToPeer[SocketToEndPoint[(Socket)socket]])) { SocketList.Remove(ToPeer[SocketToEndPoint[(Socket)socket]]); } } if (SocketToKey.ContainsKey(socket)) { SocketToKey.Remove(socket); } if (ToPeer.ContainsKey(SocketToEndPoint[(Socket)socket])) { ToPeer.Remove(SocketToEndPoint[(Socket)socket]); } } try { ((Socket)socket).Shutdown(SocketShutdown.Both); } catch (Exception e) { GetMessage(e.Message); } finally { if (timeout == -1) { ((Socket)socket).Close(); } else { ((Socket)socket).Close(timeout); } socket = null; } } }
private void Receive(System.IAsyncResult ar) { EndPoint ipe = new IPEndPoint(IPAddress.IPv6Any, 0); byte[] data = null; PacketType msgid = (PacketType)(-1); Packet packet = null; try { int num = listener.EndReceiveFrom(ar, ref ipe); if (num >= 4) { data = new byte[num]; Buffer.BlockCopy(m_Buffer, 0, data, 0, num); if (BitConverter.ToInt32(data, 0) == data.Length - 4) { packet = new Packet(ipe, data, null); msgid = packet.BeginRead(); packet.ResetPosition(); } } EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0); listener.BeginReceiveFrom(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, ref remoteEP, Receive, listener); if (packet == null) { return; } } catch (SocketException e) { DebugMessage("Server Close On Receive:" + e.ToString()); Close(); return; } catch (Exception e) { DebugMessage("Receive:" + e.ToString()); EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0); listener.BeginReceiveFrom(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, ref remoteEP, Receive, listener); return; } lock (OnListenClient) { if (OnListenClient.ContainsKey((EndPoint)packet.peer)) { OnListenClient[(EndPoint)packet.peer].Add(packet); return; } } if (msgid > PacketType.SendAllowTypeTop && msgid < PacketType.SendAllowTypeEnd && ToPeer.ContainsKey((EndPoint)packet.peer)) { PushPacket(packet); lock (checklock) { clientcheck[(EndPoint)packet.peer] = true; } } else { switch (msgid) { case PacketType.ON_CONNECT: { ListenClient(packet); break; } case PacketType.CONNECTION_LOST: { if (ToPeer.ContainsKey((EndPoint)packet.peer)) { packet.BeginRead(); SendData sendData = packet.ReadSendData(""); PushPacket(PacketType.CONNECTION_LOST, sendData.DebugMessage, packet.peer); } packet.CloseStream(); break; } case PacketType.P2P_SERVER_CALL: { if (ToPeer.ContainsKey((EndPoint)packet.peer)) { packet.BeginRead(); SendData sendData = packet.ReadSendData(SocketToKey[packet.peer]); string remoteendpoint = (string)((object[])sendData.Parameters)[0]; string localendpoint = (string)((object[])sendData.Parameters)[1]; object thedata = ((object[])sendData.Parameters)[2]; if (remoteendpoint != packet.peer.ToString() && ToPeer.TryGetValue(TraceRoute.IPEndPointParse(remoteendpoint, AddressFamily.InterNetworkV6), out PeerBase peer)) { ((object[])sendData.Parameters)[0] = packet.peer.ToString(); ((object[])sendData.Parameters)[1] = remoteendpoint; using (Packet sendpacket = new Packet(peer.socket)) { sendpacket.BeginWrite(PacketType.P2P_SERVER_CALL); sendpacket.WriteSendData(sendData, SocketToKey[TraceRoute.IPEndPointParse(remoteendpoint, AddressFamily.InterNetworkV6)], EncryptAndCompress.LockType.AES); Send(sendpacket, peer.socket); } //DebugMessage(((Client.P2PCode)sendData.Code).ToString() + ":" + packet.peer.ToString() + " to " + remoteendpoint.ToString()); } else { ((object[])sendData.Parameters)[1] = packet.peer.ToString(); using (Packet sendpacket = new Packet(packet.peer)) { sendpacket.BeginWrite(PacketType.P2P_SERVER_FAILED); sendpacket.WriteSendData(sendData, SocketToKey[packet.peer], EncryptAndCompress.LockType.AES); Send(sendpacket, packet.peer); } } } packet.CloseStream(); break; } case PacketType.P2P_GET_PUBLIC_ENDPOINT: { using (Packet sendpacket = new Packet(packet.peer)) { sendpacket.BeginWrite(PacketType.P2P_GET_PUBLIC_ENDPOINT); sendpacket.WriteSendData(new SendData(0, packet.peer.ToString()), "", EncryptAndCompress.LockType.None); Send(sendpacket, packet.peer); } packet.CloseStream(); break; } case PacketType.CHECK: { if (ToPeer.ContainsKey((EndPoint)packet.peer)) { lock (checklock) { clientcheck[(EndPoint)packet.peer] = true; } } packet.CloseStream(); break; } case PacketType.ClientDebugMessage: { if (ToPeer.ContainsKey((EndPoint)packet.peer)) { packet.BeginRead(); SendData sendData = packet.ReadSendData(SocketToKey[packet.peer]); DebugMessage("ClientDebugMessage:" + packet.peer.ToString() + " " + sendData.DebugMessage); } packet.CloseStream(); break; } default: { DebugMessage(msgid.ToString()); //PushPacket(PacketType.CONNECTION_LOST, "不正確的標頭資訊 Receive", packet.peer); packet.CloseStream(); break; } } } }