예제 #1
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!");
         }
     }
 }
예제 #2
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;
            }
        }