Exemple #1
0
        // requires an IP and socket constructor
        public Client(string ipAddress, int socket, Guid guid)
        {
            this.socket    = socket;
            this.ipAddress = ipAddress;
            this.guid      = guid.ToString();

            master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipAddress), socket);

            try {
                // tries to connect to the socket located at the IP and socket given
                master.Connect(ip);
                ShardCore.getCore().Write("Connected to Heart at IP " + ipAddress + " Socket: " + socket);
            } catch {
                ShardCore.getCore().Write("Could not connect to " + ipAddress + " on Socket: " + socket);
                Thread.Sleep(1000);

                // we dont want the client socket continuing if we were not able to connect
                return;
            }

            // creates the new listening thread
            listeningThread = new Thread(Data_IN);
            running         = true;
            listeningThread.Start();              // Data_IN is now constantly being run
        }
Exemple #2
0
 public void Close()
 {
     Data_OUT(new Packet(Packet.PacketType.CloseConnection, guid));
     ShardCore.getCore().Write("Closing connection with Heart.");
     running = false;
     master.Close();
     master.Dispose();
     listeningThread.Abort();
 }
Exemple #3
0
 // for sending data through packets
 public void Data_OUT(Packet p)
 {
     if (!connected && p.packetType != Packet.PacketType.Registration)
     {
         ShardCore.getCore().Write("We are not connected yet. Please try restarting...");
         return;
     }
     master.Send(p.ToBytes());
 }
Exemple #4
0
        // for handling received data
        private void DataManager(Packet p)
        {
            switch (p.packetType)
            {
            case Packet.PacketType.Registration:
                // if the server is sending the registration packet (start of connection)
                ShardCore.getCore().Write("Received registration packet from Heart.");
                serverGuid = p.senderID;

                // now save that to a file if this is the first connection, otherwise compare it to the file

                // set the commandKey from the server registration packet
                ShardCore.commandKey = p.packetString;

                // send client registration packet
                Data_OUT(new Packet(Packet.PacketType.Registration, guid));
                ShardCore.getCore().Write("Sent registration packet to Heart.");
                break;

            case Packet.PacketType.Handshake:
                ShardCore.getCore().Write("Handshake received. Connection Established.");
                connected = true;
                break;

            case Packet.PacketType.CloseConnection:
                string reason = p.packetString;
                if (reason == null)
                {
                    reason = "No Reason Given.";
                }
                ShardCore.getCore().Write("Server is closing the connection. Reason: " + reason);
                ShardCore.GetWindow().ServerResponse("Server is closing the connection. Reason: " + reason);
                Close();
                break;

            case Packet.PacketType.Command:
                ShardCore.getCore().Write("Server sent the response: " + p.packetString);
                ShardCore.GetWindow().ServerResponse(p.packetString);
                break;

            default:
                break;
            }
        }