Ejemplo n.º 1
0
    void MSKVersionReport(uint Version)
    {
        BytePacket bp = new BytePacket();

        bp.Add("MSKVersionReport");
        bp.Add(Version);
        netThread.SendData(bp);
    }
Ejemplo n.º 2
0
    public void ThreadProc()
    {
        BytePacket np = new BytePacket();
        Socket     sock;

        try
        {
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Connect("www.nebulastormsoft.com", 8888);
            //sock.Connect("127.0.0.1",8888);
        }
        catch (SocketException)
        {
            np.Add("OnError");
            np.Add("不能连接到服务器");
            CallProc(np);
            return;
        }
        byte [] LenB = new byte[4];
        byte [] dat;
        byte [] heart = new byte[4];
        heart[0] = 0;
        heart[1] = 0;
        heart[2] = 0;
        heart[3] = 0;
        uint LastSendTime = (uint)System.Environment.TickCount;

        while (true)
        {
            SendMutex.WaitOne();
            if (0 == ToSendData.GetLength(0))
            {
                SendMutex.ReleaseMutex();
            }
            else
            {
                dat        = ToSendData;
                ToSendData = new byte[0];
                SendMutex.ReleaseMutex();
                sock.Send(dat);
                LastSendTime = (uint)System.Environment.TickCount;
            }

            if ((uint)System.Environment.TickCount - LastSendTime > 30 * 1000)
            {            //发送心跳包
                sock.Send(heart);
                LastSendTime = (uint)System.Environment.TickCount;
            }

            int res = RecvBytes(sock, LenB, 0, 4, 100);
            if (0 == res)
            {
                continue;
            }
            if (-1 == res)
            {
                break;
            }
            LenB[3] = 0;
            uint len = System.BitConverter.ToUInt32(LenB, 0);
            if (0 == len)
            {
                continue;                  //心跳回应包直接丢弃
            }
            dat = new byte[len];
            res = RecvBytes(sock, dat, 0, len, -1);
            if (-1 == res)
            {
                break;
            }
            CallProc(dat);
        }

        np.Add("OnError");
        np.Add("与服务器的连接已断开");
        CallProc(np);
    }