Пример #1
0
 public override void on_link_broken(vesal_socket vsock)
 {
     base.on_link_broken(vsock);
     UnityEngine.Debug.Log(role_msg + " " + vsock.fd() + " broken");
     client_soket = null;
     OnpeerBroken();
 }
Пример #2
0
        public bool Main_start(string[] args, string server_ip)
        {
            if (args.Length == 0)
            {
                return(false);
            }
            StopWork();
            role_msg = args[0];
            switch (args[0])
            {
            case "server":
                vesal_socket lssock = new vesal_socket();
                lssock.listen(17000);
                base.add_sock(lssock, true);
                ScreenData.instance.ShowText("server start ip: " + server_ip);
                break;

            case "client":
                vesal_socket clisock = new vesal_socket();
                bool         ret     = clisock.connect(server_ip, 17000);
                if (!ret)
                {
                    DebugLog.DebugLogInfo("00ff00", "客户端未连接");
                    return(false);
                }
                base.add_sock(clisock, false);
                client_soket = clisock;
                break;

            default:
                break;
            }
            return(true);
        }
Пример #3
0
 public void brodcast_data(byte cmd, byte[] data)
 {
     foreach (var item in _comm_socks)
     {
         vesal_socket vsk = _comm_socks[item.Key];
         vsk.send_packet(cmd, data);
     }
 }
Пример #4
0
        public vesal_socket accept()
        {
            Socket sock = _sock.Accept();

            sock.Blocking = false;
            vesal_socket clisock = new vesal_socket();

            clisock._sock = sock;
            return(clisock);
        }
Пример #5
0
 void GetMessage(vesal_socket vsock, byte cmd, byte[] str)
 {
     if (role_message == "server")
     {
         serverMsgManager.RecieveMessageFromServer(vsock, cmd, str);
     }
     else
     {
         fm_manager.ClientResloveMessage(cmd, str);
     }
 }
Пример #6
0
 public void add_sock(vesal_socket vsock, bool listen)
 {
     if (listen)
     {
         _listen_socks[(int)vsock._sock.Handle] = vsock;
     }
     else
     {
         _comm_socks[(int)vsock._sock.Handle] = vsock;
     }
 }
Пример #7
0
        public void SendMessageToServer(byte cmd, string msg)
        {
            vesal_socket vs = program.GetClientSocket();

            if (vs == null)
            {
                serverMsgManager.CloseCheck();
            }
            else
            {
                program.send_cmd_fd(vs.fd(), cmd, msg);
            }
        }
Пример #8
0
        public override void on_packet_recved(vesal_socket vsock, packet pk)
        {
            base.on_packet_recved(vsock, pk);

            byte[] buff = new byte[pk._data_len];

            for (int j = 0; j < pk._data_len; j++)
            {
                buff[j] = pk._data[j];
            }
            //DebugLog.DebugLogInfo("ff0000", buff.Length + "");
            //接收
            OnpeerRecive(vsock, pk._cmd_code, buff);
            // string str = System.Text.Encoding.UTF8.GetString(buff);
            // if (pk._cmd_code == (byte)VESAL_CMD_CODE.MSG_CMD)
            // {
            //     UnityEngine.Debug.Log("realMessage:" + str);
            //     OnpeerRecive(str);
            // }
        }
Пример #9
0
 public override void on_peer_connected(vesal_socket vsock)
 {
     base.on_peer_connected(vsock);
     base.add_sock(vsock, false);
     ScreenData.instance.ShowText(vsock.fd() + " connet");
 }
Пример #10
0
 public void SendMessageToClientFd(vesal_socket vsock, byte cmd, string msg)
 {
     program.send_cmd_fd(vsock.fd(), cmd, msg);
 }
Пример #11
0
 virtual public void on_link_broken(vesal_socket vsock)
 {
     //Console.WriteLine("{0} link broken.", vsock.fd());
 }
Пример #12
0
 virtual public void on_packet_recved(vesal_socket vsock, packet pk)
 {
     //Console.WriteLine("{0} recv packet.", vsock.fd());
 }
Пример #13
0
 // 以下接口,由子类实现
 virtual public void on_peer_connected(vesal_socket vsock)
 {
     //Console.WriteLine("{0} link connected.", vsock.fd());
 }
Пример #14
0
        public void send_packet(int fd, byte cmd, byte[] data)
        {
            vesal_socket vsk = _comm_socks[fd];

            vsk.send_packet(cmd, data);
        }
Пример #15
0
        //写数据时,socket broken
        public void work()
        {
            if (_listen_socks.Count > 0)
            {
                ArrayList sockList = new ArrayList();
                foreach (var item in _listen_socks)
                {
                    sockList.Add(item.Value._sock);
                }

                Socket.Select(sockList, null, null, 0);
                for (int i = 0; i < sockList.Count; i++)
                {
                    vesal_socket vsock   = _listen_socks[(int)((Socket)sockList[i]).Handle];
                    vesal_socket newsock = vsock.accept();
                    _comm_socks[(int)newsock._sock.Handle] = newsock;
                    on_peer_connected(newsock);
                }
            }

            if (_comm_socks.Count > 0)
            {
                List <int> bad_socks = new List <int>();
                ArrayList  sockList2 = new ArrayList();
                foreach (var item in _comm_socks)
                {
                    sockList2.Add(item.Value._sock);
                }

                Socket.Select(sockList2, null, null, 0);

                for (int i = 0; i < sockList2.Count; i++)
                {
                    int          fd    = (int)((Socket)sockList2[i]).Handle;
                    vesal_socket vsock = _comm_socks[fd];
                    try
                    {
                        packet pk      = new packet();
                        bool   getpack = vsock.recv_packet(ref pk);
                        if (getpack)
                        {
                            on_packet_recved(vsock, pk);
                        }
                    }
                    catch (Exception e)
                    {
                        //Console.WriteLine("link read bad");
                        bad_socks.Add(fd);
                    }
                }

                clean_bad_fd(bad_socks);

                foreach (var item in _comm_socks)
                {
                    if (item.Value.has_data())
                    {
                        bool ret = item.Value.send_data();
                        if (!ret)
                        {
                            //Console.WriteLine("link send bad");
                            bad_socks.Add(item.Key);
                        }
                    }
                }

                clean_bad_fd(bad_socks);
            }
        }