Esempio n. 1
0
 void SocketSendData()
 {
     byte[] sendbytes = null;
     while (true)
     {
         System.Threading.Thread.Sleep(100);
         lock (SendDataCache)
         {
             if (SendDataCache.Count != 0)
             {
                 sendbytes = SendDataCache[0];
                 SendDataCache.RemoveAt(0);
             }
             else
             {
                 sendbytes = null;
             }
         }
         if (/*socket.Connected && */ sendbytes != null && socket != null)
         {
             try
             {
                 socket.Send(sendbytes);
                 IsCommcation = true;
             }
             catch// (Exception exc)
             {
                 IsCommcation = false;
                 SocketEvent?.Invoke(SocketEventType.发送数据失败);
                 SocketConnect();
             }
             SocketEvent?.Invoke(SocketEventType.发送数据成功);
         }
     }
 }
Esempio n. 2
0
        void ReceiveFromServer()
        {
            try
            {
                byte[] buf4 = new byte[4];
                while (true)
                {
                    clientSocket.Receive(buf4, 4, SocketFlags.None);
                    int    protocolLength = (buf4[0] - '0') * 1000 + (buf4[1] - '0') * 100 + (buf4[2] - '0') * 10 + (buf4[3] - '0') * 1;
                    byte[] buf            = new byte[protocolLength];
                    int    byteNumber     = clientSocket.Receive(buf, protocolLength, SocketFlags.None);

                    string   s  = Encoding.UTF8.GetString(buf, 0, byteNumber);
                    string[] ss = s.Split(' ');

                    if (legalProtocolMap.ContainsKey(ss[0]))
                    {
                        ClientProtocol protocol = legalProtocolMap[ss[0]].GetInstance();
                        protocol.LoadContentFromWString(s);
                        if (ProcessProtocolEvent != null)
                        {
                            ProcessProtocolEvent(protocol);
                        }
                    }
                    else
                    {
                        Debug.LogError("非法协议!" + ss[0]);
                    }
                }
            }
            catch (Exception)
            {
                if (DisconnectEvent != null)
                {
                    DisconnectEvent.Invoke(clientSocket);
                }
            }
            finally
            {
                clientSocket.Close();
            }
        }
Esempio n. 3
0
 public void StartConnect(string ip, int port)
 {
     clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     try
     {
         clientSocket.Connect(ip, port);
         if (ConnectSuccessEvent != null)
         {
             ConnectSuccessEvent.Invoke(clientSocket);
         }
         StartReceive();
     }
     catch (Exception)
     {
         if (ConnectFailEvent != null)
         {
             ConnectFailEvent.Invoke(clientSocket);
             clientSocket.Close();
         }
     }
 }
Esempio n. 4
0
        void SocketReceData()
        {
            byte[] b = new byte[1024];
            while (true)
            {
                System.Threading.Thread.Sleep(1);
                if (this.socket != null && this.socket.Available != 0)
                {
                    int count_rece = 0;
                    try
                    {
                        count_rece = this.socket.Receive(b);
                    }
                    catch
                    {
                        this.SocketEvent?.Invoke(RFID_And_IO_Connect.SocketEventType.接收数据失败);
                        continue;
                    }
                    this.SocketEvent?.Invoke(RFID_And_IO_Connect.SocketEventType.接收数据成功);
                    byte b2 = b[0];
                    switch (b2)
                    {
                    case 0x30:
                    case 0x32:
                    case 0x34:
                    case 0x35:
                    case 0x36:
                        DealReceRFIDData(b, 0, count_rece);
                        break;

                    case 1:
                        if (count_rece == 6)
                        {
                            //AppMessage.Add("接受到IO读取数据:"+b.ToStandardString(","), AppMessage.MsgType.RFID);
                            DealReceIOData(b, 0, count_rece);
                        }
                        else
                        {
                            SocketEvent?.Invoke(SocketEventType.接收到未知数据);
                        }
                        break;

                    default:
                        SocketEvent?.Invoke(SocketEventType.接收到未知数据);
                        break;
                    }
                }
            }
        }
Esempio n. 5
0
    private void OnMessageReceived(WebSocket webSocket, string message)
    {
        RKLog.Log("OnMessageReceived: " + message, "socketcontroller");
        SocketResponse data = JsonUtility.FromJson <SocketResponse>(message);

        GenericData gd = new GenericData();

        foreach (ResponseStringList item in data.data)
        {
            gd.Set(item.key, item.value);
        }

        RKLog.Log("invoke: " + gd.ToString(), "socketcontroller");

        MessageReceived.Invoke(data.type, gd);

        if (data.type == "update")
        {
            RKLog.Log("onmessagereceived, update: " + gd.ToString() + " dus poll", "socketcontroller");
        }
    }
Esempio n. 6
0
        /// <summary>
        /// 服务器接收客户端
        /// </summary>
        void AcceptClient()
        {
            try
            {
                clientSocket = serverSocket.Accept();

                if (AcceptNewSocketEvent != null)
                {
                    AcceptNewSocketEvent.Invoke(clientSocket);
                }

                ClientManager.GetSingleInstance().GetClientFromServer(clientSocket);
                ClientManager.GetSingleInstance().ProcessProtocolEvent += ProcessProtocolEvent;
            }
            catch (SocketException)
            {
                if (SocketDisconnectEvent != null)
                {
                    SocketDisconnectEvent.Invoke(serverSocket);
                    serverSocket.Close();
                }
            }
        }
Esempio n. 7
0
 protected virtual void OnMessage(string args)
 {
     SocketEvent?.Invoke(this, args);
 }