|
29 | 29 | using System.Collections; |
30 | 30 |
|
31 | 31 | using System; |
| 32 | +#if UNITY_WSA && !UNITY_EDITOR |
| 33 | +using Windows.Networking; |
| 34 | +using Windows.Networking.Sockets; |
| 35 | +#else |
32 | 36 | using System.Net; |
33 | 37 | using System.Net.Sockets; |
| 38 | +#endif |
34 | 39 | using System.Text; |
35 | 40 | using System.Threading; |
36 | 41 | using System.IO; |
@@ -430,9 +435,7 @@ public OmicronConnectorClient(IOmicronConnectorClientListener clistener) |
430 | 435 |
|
431 | 436 | public bool Connect(string serverIP, int msgPort, int dataPort, int flags = -1) |
432 | 437 | { |
433 | | -#if UNITY_WSA |
434 | | - Debug.LogWarning("Universal Windows Platform not supported. Will only work in Unity editor!"); |
435 | | -#endif |
| 438 | + |
436 | 439 | if (EnableInputService) |
437 | 440 | { |
438 | 441 | //dgrams = new ArrayList(); |
@@ -546,126 +549,237 @@ private void ListenTCP() |
546 | 549 | } |
547 | 550 |
|
548 | 551 | }// Listen() |
| 552 | +#else |
| 553 | + // TCP Connection |
| 554 | + StreamSocket client; |
| 555 | + Stream streamToServer; |
| 556 | + |
| 557 | + // UDP Connection |
| 558 | + public DatagramSocket udpClient; |
| 559 | + |
| 560 | + public String InputServer = "localhost"; |
| 561 | + public Int32 dataPort = 7000; |
| 562 | + public Int32 msgPort = 27000; |
| 563 | + |
| 564 | + public bool EnableInputService = true; |
| 565 | + bool connected = false; |
| 566 | + |
| 567 | + static IOmicronConnectorClientListener listener; |
| 568 | + |
| 569 | + public OmicronConnectorClient() |
| 570 | + { |
| 571 | + } |
| 572 | + |
| 573 | + public OmicronConnectorClient(IOmicronConnectorClientListener clistener) |
| 574 | + { |
| 575 | + listener = clistener; |
| 576 | + //Debug.LogWarning("Platform not supported"); |
| 577 | + } |
| 578 | + |
| 579 | + public bool Connect(string serverIP, int msgPort, int dataPort, int flags = -1) |
| 580 | + { |
| 581 | + if (EnableInputService) |
| 582 | + { |
| 583 | + Debug.Log("OmicronConnector: Initializing... "); |
| 584 | + ConnectASync(serverIP, msgPort, dataPort, flags); |
| 585 | + } |
| 586 | + connected = false; |
| 587 | + //Debug.LogWarning("Platform not supported"); |
| 588 | + return false; |
| 589 | + }// CTOR |
| 590 | + |
| 591 | + private async void ConnectASync(string serverIP, int msgPort, int dataPort, int flags = -1) |
| 592 | + { |
| 593 | + try |
| 594 | + { |
| 595 | + // Create a TcpClient. |
| 596 | + Debug.Log("InputService: Connecting to to " + serverIP); |
| 597 | + client = new StreamSocket(); |
| 598 | + await client.ConnectAsync(new HostName(serverIP), msgPort.ToString()); |
| 599 | + streamToServer = client.OutputStream.AsStreamForWrite(); |
| 600 | + |
| 601 | + // Translate the passed message into ASCII and store it as a Byte array. |
| 602 | + String message = "omicronV3_data_on," + dataPort + "," + flags; |
| 603 | + Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); |
| 604 | + |
| 605 | + // Send the handshake message to the server. |
| 606 | + streamToServer.Write(data, 0, data.Length); |
| 607 | + streamToServer.Flush(); |
| 608 | + |
| 609 | + // Create the UDP socket data will be received on |
| 610 | + udpClient = new DatagramSocket(); |
| 611 | + udpClient.MessageReceived += MessageReceived; |
| 612 | + udpClient.Control.InboundBufferSizeInBytes = 15360000; |
| 613 | + await udpClient.BindServiceNameAsync(dataPort.ToString()); |
| 614 | + |
| 615 | + Debug.Log("InputService: Connected to " + serverIP); |
| 616 | + connected = true; |
| 617 | + return; |
| 618 | + } |
| 619 | + catch (Exception e) |
| 620 | + { |
| 621 | + Debug.Log("Exception: " + e); |
| 622 | + } |
| 623 | + connected = false; |
| 624 | + return; |
| 625 | + } |
| 626 | + |
| 627 | + public void Dispose() |
| 628 | + { |
| 629 | + //Debug.LogWarning("Platform not supported"); |
| 630 | + if (connected) |
| 631 | + { |
| 632 | + String message = "data_off"; |
| 633 | + Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); |
| 634 | + streamToServer.Write(data, 0, data.Length); |
| 635 | + |
| 636 | + // Close the socket when finished receiving datagrams |
| 637 | + Debug.Log("OmicronConnectorClient: Finished receiving. Closing socket.\n"); |
| 638 | + udpClient.Dispose(); |
| 639 | + |
| 640 | + // Close TCP connection. |
| 641 | + streamToServer.Dispose(); |
| 642 | + client.Dispose(); |
| 643 | + |
| 644 | + Debug.Log("OmicronConnectorClient: Shutting down."); |
| 645 | + } |
| 646 | + } |
| 647 | + |
| 648 | + void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments) |
| 649 | + { |
| 650 | + try |
| 651 | + { |
| 652 | + Byte[] receiveBytes = new byte[eventArguments.GetDataReader().UnconsumedBufferLength]; |
| 653 | + eventArguments.GetDataReader().ReadBytes(receiveBytes); |
549 | 654 |
|
| 655 | + listener.onEvent(ByteArrayToEventData(receiveBytes)); |
| 656 | + } |
| 657 | + catch (Exception exception) |
| 658 | + { |
| 659 | + SocketErrorStatus socketError = SocketError.GetStatus(exception.HResult); |
| 660 | + if (socketError == SocketErrorStatus.ConnectionResetByPeer) |
| 661 | + { |
| 662 | + // This error would indicate that a previous send operation resulted in an |
| 663 | + // ICMP "Port Unreachable" message. |
| 664 | + Debug.Log( |
| 665 | + "Peer does not listen on the specific port. Please make sure that you run step 1 first " + |
| 666 | + "or you have a server properly working on a remote server."); |
| 667 | + } |
| 668 | + else if (socketError != SocketErrorStatus.Unknown) |
| 669 | + { |
| 670 | + Debug.Log( |
| 671 | + "Error happened when receiving a datagram: " + socketError.ToString() |
| 672 | + ); |
| 673 | + } |
| 674 | + else |
| 675 | + { |
| 676 | + throw; |
| 677 | + } |
| 678 | + } |
| 679 | + } |
| 680 | +#endif |
550 | 681 | public static EventData ByteArrayToEventData(byte[] receiveBytes) |
551 | | - { |
552 | | - MemoryStream ms = new MemoryStream(); |
553 | | - ms.Write(receiveBytes, 0, receiveBytes.Length); |
| 682 | + { |
| 683 | + MemoryStream ms = new MemoryStream(); |
| 684 | + ms.Write(receiveBytes, 0, receiveBytes.Length); |
554 | 685 |
|
555 | | - BinaryReader reader = new BinaryReader(ms); |
556 | | - omicronConnector.EventData ed = new omicronConnector.EventData(); |
557 | | - reader.BaseStream.Position = 0; |
558 | | - |
559 | | - ed.timestamp = reader.ReadUInt32(); |
560 | | - ed.sourceId = reader.ReadUInt32(); |
561 | | - ed.serviceId = reader.ReadInt32(); |
562 | | - ed.serviceType = (EventBase.ServiceType)reader.ReadUInt32(); |
563 | | - ed.type = reader.ReadUInt32(); |
564 | | - ed.flags = reader.ReadUInt32(); |
565 | | - |
566 | | - if (ed.type != 3) |
567 | | - { |
568 | | - Console.WriteLine(ed.type); |
569 | | - Console.WriteLine(ed.flags); |
570 | | - Console.WriteLine("---"); |
571 | | - } |
572 | | - |
573 | | - ed.posx = reader.ReadSingle(); |
574 | | - ed.posy = reader.ReadSingle(); |
575 | | - ed.posz = reader.ReadSingle(); |
576 | | - ed.orw = reader.ReadSingle(); |
577 | | - ed.orx = reader.ReadSingle(); |
578 | | - ed.ory = reader.ReadSingle(); |
579 | | - ed.orz = reader.ReadSingle(); |
580 | | - |
581 | | - ed.extraDataType = (omicron.EventBase.ExtraDataType)reader.ReadUInt32(); |
582 | | - ed.extraDataItems = reader.ReadUInt32(); |
583 | | - ed.extraDataMask = reader.ReadUInt32(); |
584 | | - |
585 | | - ed.extraData = reader.ReadBytes(EventData.ExtraDataSize); |
| 686 | + BinaryReader reader = new BinaryReader(ms); |
| 687 | + omicronConnector.EventData ed = new omicronConnector.EventData(); |
| 688 | + reader.BaseStream.Position = 0; |
| 689 | + |
| 690 | + ed.timestamp = reader.ReadUInt32(); |
| 691 | + ed.sourceId = reader.ReadUInt32(); |
| 692 | + ed.serviceId = reader.ReadInt32(); |
| 693 | + ed.serviceType = (EventBase.ServiceType)reader.ReadUInt32(); |
| 694 | + ed.type = reader.ReadUInt32(); |
| 695 | + ed.flags = reader.ReadUInt32(); |
| 696 | + |
| 697 | + if (ed.type != 3) |
| 698 | + { |
| 699 | + Console.WriteLine(ed.type); |
| 700 | + Console.WriteLine(ed.flags); |
| 701 | + Console.WriteLine("---"); |
| 702 | + } |
| 703 | + |
| 704 | + ed.posx = reader.ReadSingle(); |
| 705 | + ed.posy = reader.ReadSingle(); |
| 706 | + ed.posz = reader.ReadSingle(); |
| 707 | + ed.orw = reader.ReadSingle(); |
| 708 | + ed.orx = reader.ReadSingle(); |
| 709 | + ed.ory = reader.ReadSingle(); |
| 710 | + ed.orz = reader.ReadSingle(); |
| 711 | + |
| 712 | + ed.extraDataType = (omicron.EventBase.ExtraDataType)reader.ReadUInt32(); |
| 713 | + ed.extraDataItems = reader.ReadUInt32(); |
| 714 | + ed.extraDataMask = reader.ReadUInt32(); |
586 | 715 |
|
587 | | - return ed; |
588 | | - } |
| 716 | + ed.extraData = reader.ReadBytes(EventData.ExtraDataSize); |
589 | 717 |
|
590 | | - public static byte[] EventDataToByteArray(EventData ed) |
591 | | - { |
| 718 | + return ed; |
| 719 | + } |
| 720 | + |
| 721 | + public static byte[] EventDataToByteArray(EventData ed) |
| 722 | + { |
592 | 723 | MemoryStream ms = new MemoryStream(); |
593 | | - BinaryWriter bw = new BinaryWriter(ms); |
| 724 | + BinaryWriter bw = new BinaryWriter(ms); |
594 | 725 |
|
595 | | - bw.Write(ed.timestamp); |
| 726 | + bw.Write(ed.timestamp); |
596 | 727 | bw.Write(ed.sourceId); |
597 | | - bw.Write(ed.serviceId); |
| 728 | + bw.Write(ed.serviceId); |
598 | 729 | bw.Write((uint)ed.serviceType); |
599 | | - bw.Write(ed.type); |
600 | | - bw.Write(ed.flags); |
| 730 | + bw.Write(ed.type); |
| 731 | + bw.Write(ed.flags); |
601 | 732 |
|
602 | | - bw.Write(ed.posx); |
603 | | - bw.Write(ed.posy); |
604 | | - bw.Write(ed.posz); |
605 | | - bw.Write(ed.orw); |
606 | | - bw.Write(ed.orx); |
607 | | - bw.Write(ed.ory); |
608 | | - bw.Write(ed.orz); |
| 733 | + bw.Write(ed.posx); |
| 734 | + bw.Write(ed.posy); |
| 735 | + bw.Write(ed.posz); |
| 736 | + bw.Write(ed.orw); |
| 737 | + bw.Write(ed.orx); |
| 738 | + bw.Write(ed.ory); |
| 739 | + bw.Write(ed.orz); |
609 | 740 |
|
610 | | - bw.Write((uint)ed.extraDataType); |
611 | | - bw.Write(ed.extraDataItems); |
612 | | - bw.Write(ed.extraDataMask); |
| 741 | + bw.Write((uint)ed.extraDataType); |
| 742 | + bw.Write(ed.extraDataItems); |
| 743 | + bw.Write(ed.extraDataMask); |
613 | 744 |
|
614 | | - bw.Write(ed.extraData); |
| 745 | + bw.Write(ed.extraData); |
615 | 746 |
|
616 | 747 | bw.Close(); |
617 | 748 | byte[] dataArray = ms.GetBuffer(); |
618 | 749 | ms.Close(); |
619 | 750 |
|
620 | 751 |
|
621 | | - return dataArray; |
622 | | - } |
623 | | - |
624 | | - public static string EventDataToString( EventData e ) |
625 | | - { |
626 | | - byte[] b = EventDataToByteArray(e); |
627 | | - return GetStringFromBytes(b); |
628 | | - } |
629 | | - |
630 | | - public static EventData StringToEventData( string s ) |
631 | | - { |
632 | | - byte[] b = GetBytesFromString(s); |
633 | | - return ByteArrayToEventData(b); |
634 | | - } |
635 | | - |
636 | | - static byte[] GetBytesFromString(string str) |
637 | | - { |
638 | | - return System.Convert.FromBase64String(str); |
639 | | - //return System.Text.Encoding.UTF8.GetBytes(str); |
640 | | - //byte[] bytes = new byte[str.Length * sizeof(char)]; |
641 | | - //System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); |
642 | | - //return bytes; |
643 | | - } |
644 | | - |
645 | | - static string GetStringFromBytes(byte[] bytes) |
646 | | - { |
647 | | - return System.Convert.ToBase64String(bytes); |
648 | | - //return System.Text.Encoding.UTF8.GetString(bytes); |
649 | | - //char[] chars = new char[bytes.Length / sizeof(char)]; |
650 | | - //System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); |
651 | | - //return new string(chars); |
652 | | - } |
653 | | -#else |
654 | | - public OmicronConnectorClient(IOmicronConnectorClientListener clistener) |
| 752 | + return dataArray; |
| 753 | + } |
| 754 | + |
| 755 | + public static string EventDataToString(EventData e) |
655 | 756 | { |
656 | | - Debug.LogWarning("Platform not supported"); |
| 757 | + byte[] b = EventDataToByteArray(e); |
| 758 | + return GetStringFromBytes(b); |
657 | 759 | } |
658 | 760 |
|
659 | | - public bool Connect(string serverIP, int msgPort, int dataPort) |
| 761 | + public static EventData StringToEventData(string s) |
660 | 762 | { |
661 | | - Debug.LogWarning("Platform not supported"); |
662 | | - return false; |
663 | | - }// CTOR |
| 763 | + byte[] b = GetBytesFromString(s); |
| 764 | + return ByteArrayToEventData(b); |
| 765 | + } |
664 | 766 |
|
665 | | - public void Dispose() |
| 767 | + static byte[] GetBytesFromString(string str) |
666 | 768 | { |
667 | | - Debug.LogWarning("Platform not supported"); |
| 769 | + return System.Convert.FromBase64String(str); |
| 770 | + //return System.Text.Encoding.UTF8.GetBytes(str); |
| 771 | + //byte[] bytes = new byte[str.Length * sizeof(char)]; |
| 772 | + //System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); |
| 773 | + //return bytes; |
| 774 | + } |
| 775 | + |
| 776 | + static string GetStringFromBytes(byte[] bytes) |
| 777 | + { |
| 778 | + return System.Convert.ToBase64String(bytes); |
| 779 | + //return System.Text.Encoding.UTF8.GetString(bytes); |
| 780 | + //char[] chars = new char[bytes.Length / sizeof(char)]; |
| 781 | + //System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); |
| 782 | + //return new string(chars); |
668 | 783 | } |
669 | | -#endif |
670 | 784 | } |
671 | 785 | } |
0 commit comments