Esempio n. 1
0
    public static void startCon()
    {
        sc = new vesal_network.vesal_socket();

        if (sc.connect("127.0.0.1", port))
        {
            // DebugLog.DebugLogInfo("I'm Unity,Save me!");
            // int tmp = sc._sock.Send(Encoding.UTF8.GetBytes("I'm Unity,Save me!"), SocketFlags.None);
        }
        else
        {
            //连接失败重连
            startCon();
        }
        pc = new vesal_network.packet();

        byte[] bytes = new byte[1024];
        while (true)
        {
            if (sc != null && sc._sock != null && !sc._sock.Poll(1000, SelectMode.SelectRead))
            {
                int    receiveNumber = sc._sock.Receive(bytes);
                string message       = Encoding.UTF8.GetString(bytes, 0, receiveNumber);
                switch (message)
                {
                case "quit":
                    stopThread();
                    break;

                case "":
                    UnityEngine.Debug.Log("linsten");
                    break;

                default:
                    UnityEngine.Debug.Log("得到参数:" + message);
                    // if(PublicClass.appOpera!=null)
                    //     PublicClass.appOpera.Get_app(message);
                    rcvMessage = message;
                    break;
                }

                // UnityEngine.Debug.Log(string.Format(" 接收客户端{0}消息{1}", sc._sock.RemoteEndPoint.ToString(), message));
                //         try
                //         {
                //             rcvMessage = "断开连接";
                //             thread.Abort();
                //             sc.close();
                //             break;
                //         }
                //         catch
                //         {
                //         }
                // int tmp = sc._sock.Send(Encoding.UTF8.GetBytes(message));
                // UnityEngine.Debug.Log(tmp + "--" + message.Equals("disconnect") + "--" + (message == "disconnect"));
            }
            //Thread.SpinWait(1);
        }
    }
Esempio n. 2
0
        public bool recv_packet(ref packet pk)
        {
            int ret = 0;

            if (!_get_header)
            {
                ret = _sock.Receive(_head_buff, _offset, _expect_len - _offset, SocketFlags.None);
            }
            else
            {
                ret = _sock.Receive(_data_buff, _offset, _expect_len - _offset, SocketFlags.None);
            }

            if (ret <= 0)
            {
                // link broken.
                throw new ArgumentNullException("value");;
            }

            _offset += (ushort)ret;
            if (_offset == _expect_len)
            {
                _offset = 0;
                if (!_get_header)
                {
                    // 包头接收完毕。
                    _get_header = true;
                    _expect_len = get_pack_len(_head_buff);
                    return(false);
                }
                else
                {
                    // 一个包收完了。
                    _get_header = false;

                    //输出包
                    pk._cmd_code = _head_buff[0];
                    pk._data_len = _expect_len;
                    pk._data     = new byte[_expect_len];
                    for (int j = 0; j < _expect_len; j++)
                    {
                        pk._data[j] = _data_buff[j];
                    }
                    _expect_len = 3;
                    return(true);
                }
            }

            return(false);
        }