private async void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { // 多重イベントを抑制 //m_udp.MessageReceived -= Socket_MessageReceived; Stream streamIn = args.GetDataStream().AsStreamForRead(); StreamReader reader = new StreamReader(streamIn); int port; Int32.TryParse(args.RemotePort, out port); ReceiveDataType r = new ReceiveDataType(); try { r.data = await reader.ReadLineAsync(); r.remoteIP = new IPEndPoint(IPAddress.Parse(args.RemoteAddress.ToString()), port); m_receivedData.Enqueue(r); } catch (Exception e) { } // イベント発生を許可 //m_udp.MessageReceived += Socket_MessageReceived; }
void ReceiveData(byte[] data) { ByteArray byteArray = new ByteArray(data, (short)data.Length); ReceiveDataType type = (ReceiveDataType)byteArray.readChar(); ReceiveData receiveData = new ReceiveData(); switch (type) { case ReceiveDataType.Response: checkConfirm(byteArray.readInt(), byteArray.readChar()); break; case ReceiveDataType.Transform: receiveData.cmd = type; receiveData.transform = ReceiveTransformData.readFromBytes(byteArray.readBytes()); callback(receiveData); break; case ReceiveDataType.Action: receiveData.cmd = type; SendResponse(byteArray.readInt()); receiveData.action = ReceiveActionData.readFromBytes(byteArray.readBytes()); callback(receiveData); break; case ReceiveDataType.State: receiveData.cmd = type; SendResponse(byteArray.readInt()); receiveData.state = ReceiveStateData.readFromBytes(byteArray.readBytes()); callback(receiveData); break; } }
//接收业务协同返回的消息(报警信息) private void ReveiveData3() { if (!tcp1.Connected) { return; } stream1 = tcp1.GetStream(); byte[] data; string receiveData; while (true) { try { data = new byte[1024 * 100]; int bytes = stream1.Read(data, 0, data.Length); receiveData = Encoding.UTF8.GetString(data, 0, bytes).Replace("\r\n", ""); //转换成ReceiveDataType,判断数据源的服务类型 ReceiveDataType d = JsonConvert.DeserializeObject <ReceiveDataType>(receiveData); switch (d.SType) { //实时信息 case ServerType.DeviceRealTimeData: break; //报警信息 case ServerType.Alarm: string sendStr = "10/" + JsonConvert.SerializeObject(d.DataPackage.alarmmessage); byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr); IPAddress ipAddress = IPAddress.Parse(ConfigurationManager.AppSettings["BCSimulateSoftware_IP"]); int BCSimulateSoftware_Port = int.Parse(ConfigurationManager.AppSettings["BCSimulateSoftware_Port"]); IPEndPoint ipe3 = new IPEndPoint(ipAddress, BCSimulateSoftware_Port); Socket clientSocket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clientSocket1.Connect(ipe3); clientSocket1.Send(sendBytes); break; case ServerType.Intercom: break; } Console.WriteLine(receiveData); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// 接收业务协同服务返回信息 /// </summary> private static void ReveiveData1() { if (!tcp1.Connected) { return; } stream1 = tcp1.GetStream(); byte[] data; string receiveData; while (true) { try { data = new Byte[1024 * 100]; Int32 bytes = stream1.Read(data, 0, data.Length); receiveData = Encoding.UTF8.GetString(data, 0, bytes).Replace("\r\n", ""); //转换成ReceiveDataType,判断数据源的服务类型 ReceiveDataType d = JsonConvert.DeserializeObject <ReceiveDataType>(receiveData); switch (d.SType) { case ServerType.DeviceRealTimeData: break; case ServerType.Alarm: AlarmEvent(d.DataPackage); break; case ServerType.Intercom: break; } Console.WriteLine(receiveData); } catch (Exception ex) { Console.WriteLine(ex.ToString()); //服务器重连 Init(); } } }
// Use this for initialization #if !UNITY_EDITOR async void Start() { #else void Start() { #endif m_receivedData = new SafeQueue <ReceiveDataType>(); #if !UNITY_EDITOR m_udp = new DatagramSocket(); m_udp.MessageReceived += Socket_MessageReceived; try { await m_udp.BindEndpointAsync(null, m_localPort.ToString()); } catch (IOException e) { return; } //m_udp.Control.DontFragment = true; //m_udp.Control.InboundBufferSizeInBytes = 1024 * 1024; //m_udp.Control.QualityOfService = SocketQualityOfService.LowLatency; #else m_udp = new UdpClient(m_localPort); m_udp.Client.ReceiveTimeout = 1000; m_listenThread = new Thread(new ParameterizedThreadStart(ListenThread)); m_listenThread.Start(this); #endif } // Update is called once per frame void Update() { while (m_receivedData.Count > 0) { ReceiveDataType rcv = m_receivedData.Dequeue(); //Debug.Log(rcv.data + " from " + rcv.remoteIP.ToString()); if (null != m_onReceive) { m_onReceive.Invoke(rcv.data, rcv.remoteIP.Address.ToString(), rcv.remoteIP.Port); } } }
private static void ListenThread(object callee) { UDP_Receiver thiz = (UDP_Receiver)callee; while (true) { IPEndPoint remoteIP = null; byte[] data; if (thiz.m_udp.Available > 0) { data = thiz.m_udp.Receive(ref remoteIP); ReceiveDataType r = new ReceiveDataType(); r.data = Encoding.ASCII.GetString(data); r.remoteIP = remoteIP; thiz.m_receivedData.Enqueue(r); } // Sleepしないとフリーズする!? Thread.Sleep(1); } }
private void GetDeviceModel() { rdType = ReceiveDataType.GetDeviceModel; SendData(DataDeclaration.DeviceModel, 0, DataDeclaration.DeviceModel.Length); }