Example #1
0
 void sendWattUsageUpdate(short socketid, int watt)
 {
     scPacket pck = new scPacket(PACKET_TYPE.S_SOCKET_POWER_UPDATE);
     pck.writeShort(socketid);
     pck.writeLong(watt);
     Program.getRoot().sendToAll(pck);
 }
Example #2
0
 void sendSocketPowerInfo(short socketid)
 {
     List<relay> relays = Program.getRoot().getRelays();
     for (int i = 0; i < relays.Count; i++)
     {
         if (relays[i].getID() == socketid)
         {
             scPacket pck = new scPacket(PACKET_TYPE.S_SOCKET_POWER_INFO);
             pck.writeShort(socketid);
             byte state = (byte)(relays[i].getCurrentState() == relay.RELAY_STATE.ON ? 1 : 0);
             pck.writeByte(state);
             pck.writeLong(relays[i].getTotalUptime());
             pck.writeLong(relays[i].getDailyUptime());
             pck.writeLong(relays[i].getDailyDowntime());
             pck.writeLong(relays[i].getWatt());
             sendPacket(pck);
             break;
         }
     }
 }
Example #3
0
 void sendSockUpdateToAll(short sockid, byte state)
 {
     scPacket pck = new scPacket(PACKET_TYPE.S_SOCKET_UPDATE);
     pck.writeShort(sockid);
     pck.writeByte(state);
     Program.getRoot().sendToAll(pck);
 }
Example #4
0
 void sendOnlineSockets()
 {
     scPacket pck = new scPacket(PACKET_TYPE.S_SEND_ONLINE_CLIENTS);
     // now write all the data needed to the packet
     List<relay> relays = Program.getRoot().getRelays();
     pck.writeShort((short)relays.Count);
     for (int i = 0; i < relays.Count; i++)
     {
         pck.writeShort(relays[i].getID());                  //2
         pck.writeByte((byte)relays[i].getCurrentState());   //3
         pck.writeLong(relays[i].getTotalUptime());          //7
         pck.writeLong(relays[i].getDailyUptime());          //11
         pck.writeLong(relays[i].getDailyDowntime());        //15
         pck.writeLong(relays[i].getWatt());                 //19
     }
     sendPacket(pck);
 }
Example #5
0
 void sendPingPacket()
 {
     m_sendTime = unixtime.getCurrentTime();
     m_packetSend = DateTime.Now;
     // just use time to generate a random string
     m_message = DateTime.Now.Millisecond.ToString();
     scPacket pck = new scPacket(PACKET_TYPE.SC_PING);
     pck.writeString(m_message);
     sendPacket(pck);
     m_waitingForPing = true;
     Console.WriteLine("sending ping packet");
 }
Example #6
0
 void sendAcceptPacket()
 {
     scPacket pck = new scPacket(PACKET_TYPE.S_ACCEPT);
     m_state = CLIENT_STATE.ONLINE;
     sendPacket(pck);
 }
Example #7
0
 void parseBuffer()
 {
     scPacket pck = new scPacket();
     if (pck.parseRawData(m_buffer))
     {
         m_buffer.RemoveRange(0, pck.getSize());
         handlePacket(pck);
     }
     else
     {
         // check pck error
         if ((PACKET_TYPE)pck.getPacketType() == PACKET_TYPE.ERR_INVALID_PACKET)
         {
             kick("got an invalid packet!");
         }
     }
 }
Example #8
0
        void handlePacket(scPacket pck)
        {
            switch ((PACKET_TYPE)pck.getPacketType())
            {
                case PACKET_TYPE.C_HANDSHAKE:
                    if (m_state == CLIENT_STATE.HANDHAKE)
                    {
                        Console.WriteLine("got handshake");
                        sendAcceptPacket();
                        m_state = CLIENT_STATE.ONLINE;
                        m_latency = 0;
                        m_sendTime = unixtime.getCurrentTime();
                    }
                    else
                    {
                        // kick the client for sending wrong packets!
                        kick("Handshake recieved from client, but it is already online!");
                    }
                    break;
                case PACKET_TYPE.C_GET_ONLINE_CLIENTS:
                    if (m_state == CLIENT_STATE.ONLINE)
                    {
                        Console.WriteLine("got GET_ONLINE_CLIENTS packet!");
                        sendOnlineSockets();
                    }
                    else
                    {
                        kick("got packet GET_ONLINE_CLIENTS, but client is not online!!");
                    }
                    break;
                case PACKET_TYPE.C_UPDATE_SOCKET_STATE:
                    if (m_state == CLIENT_STATE.ONLINE)
                    {
                        Console.WriteLine("got UPDATE_SOCKET_STATE packet!");
                        short sockId = pck.readShort();
                        byte state = pck.readByte();
                        if (Program.getRoot().setRelayState(sockId, state))
                        {
                            Console.WriteLine("updated socket {0} to state {1}", sockId, state);
                            updateSocket(sockId, state);
                            sendSockUpdateToAll(sockId, state);
                        }
                        else
                        {
                            Console.WriteLine("failed updated socket {0} to state {1}", sockId, state);
                        }
                    }
                    else
                    {
                        kick("got packet UPDATE_SOCKET_STATE, but client is not online!!");
                    }
                    break;
                case PACKET_TYPE.C_SOCKET_POWER_UPDATE:
                    if (m_state != CLIENT_STATE.ONLINE)
                    {
                        kick("got packet C_SOCKET_POWER_UPDATE but client is not online!");
                        return;
                    }
                    short socketid = pck.readShort();
                    int watt = pck.readLong();
                    watt = watt < 0 ? 0 : watt;
                    Console.WriteLine("C_SOCKET_POWER_UPDATE watt{0}, sockid{1}", watt, socketid);
                    updateSocketPowerUsage(socketid, watt);
                    sendWattUsageUpdate(socketid, watt);
                    break;
                case PACKET_TYPE.C_REQUEST_SOCKET_INFO:
                    if (m_state != CLIENT_STATE.ONLINE)
                    {
                        kick("got packet C_REQUEST_POWER_UPDATE but client is not online");
                        return;
                    }
                    short sockid = pck.readShort();
                    sendSocketPowerInfo(sockid);
                    Console.WriteLine("got packet C_REQUEST_SOCKET_UPDATE sockid {0}", sockid);
                    break;
                case PACKET_TYPE.SC_PING:
                    if (m_state != CLIENT_STATE.ONLINE)
                    {
                        kick("got packet SC_PING, but client is not online!");
                        return;
                    }

                    string msg = pck.readString();
                    if (msg != m_message)
                    {
                        kick("got ping packet, but the strings are not equal!");
                        return;
                    }
                    m_latency = unixtime.getDifferenceMilisecond(m_packetSend, DateTime.Now);
                    m_sendTime = unixtime.getCurrentTime();
                    m_waitingForPing = false;
                    Console.WriteLine("got ping packet latency {0} ms", m_latency);
                    break;
                case PACKET_TYPE.SC_SEND_XML:
                    if (m_state != CLIENT_STATE.ONLINE)
                    {
                        kick("got packet SC_SEND_XML, but client is not online!");
                        return;
                    }
                    string xml = pck.readString();
                    Program.getRoot().saveXML(xml);
                    // send the xml file to all clients
                    // its the same packet, so just send it
                    Program.getRoot().sendToAll(pck);
                    Console.WriteLine("got SC_SEND_XML lenght {0}", xml.Length);
                    break;
                case PACKET_TYPE.C_GET_XML:
                    if (m_state != CLIENT_STATE.ONLINE)
                    {
                        kick("got packet C_GET_XML, but client is not online!");
                        return;
                    }
                    xml = Program.getRoot().getXML();
                    scPacket packet = new scPacket(PACKET_TYPE.SC_SEND_XML);
                    packet.writeString(xml);
                    sendPacket(packet);
                    Console.WriteLine("got C_GET_XML");
                    break;
                case PACKET_TYPE.SC_SEND_PICTURE:
                    if (m_state != CLIENT_STATE.ONLINE)
                    {
                        kick("got packet SC_SEND_PICTURE, but client is not online!");
                        return;
                    }
                    string name = pck.readString();
                    int dataLen = pck.readLong();
                    byte[] data = new byte[dataLen];
                    for (int i = 0; i < dataLen; i++)
                        data[i] = pck.readByte();
                    Program.getRoot().setPicture(name, data);
                    // send this packet to all clients
                    Program.getRoot().sendToAll(pck);
                    Console.WriteLine("got packet SC_SEND_PICTURE name[{0}] = byte[{1}]", name, dataLen);
                    break;
                case PACKET_TYPE.C_GET_PICTURE:
                    if (m_state != CLIENT_STATE.ONLINE)
                    {
                        kick("got packet SC_SEND_PICTURE, but client is not online!");
                        return;
                    }
                    name = pck.readString();
                    data = Program.getRoot().getPicture(name);
                    packet = new scPacket(PACKET_TYPE.SC_SEND_PICTURE);
                    packet.writeString(name);
                    if (data == null)
                    {
                        packet.writeLong(0);
                    }
                    else
                    {
                        packet.writeLong(data.Length);
                        Console.WriteLine("data lenght {0}", data.Length);
                        if (data.Length > 0)
                        {
                            for (int i = 0; i < data.Length; i++)
                                packet.writeByte(data[i]);
                        }
                    }
                    sendPacket(packet);
                    Console.WriteLine("got C_GET_PICTURE name = {0}", name);
                    break;
                default:
                    Console.WriteLine("unknown packet recieved");
                    break;
            }
        }
Example #9
0
 public void sendPacket(scPacket pck)
 {
     try
     {
         byte[] pckData = pck.getRawData().ToArray();
         Console.WriteLine("sending {0} bytes", pckData.Length);
         int sent = 0;
         int toBeSent = pckData.Length;
         while(sent < toBeSent)
             sent += m_socket.Send(pckData, sent, pckData.Length, SocketFlags.None);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Example #10
0
 public void sendToAll(scPacket pck)
 {
     foreach (Client c in m_clients)
     {
         c.sendPacket(pck);
     }
 }