[NSPortExample] Update metadata + fix static analyzer issues

This commit is contained in:
olegoid 2015-10-30 16:01:04 -03:00
Родитель 61ac921128
Коммит c2222efc99
4 изменённых файлов: 23 добавлений и 35 удалений

Просмотреть файл

@ -1,6 +1,4 @@
using System;
using Foundation;
using Foundation;
using AppKit;
namespace MessageSender
@ -9,10 +7,6 @@ namespace MessageSender
{
MainWindowController mainWindowController;
public AppDelegate ()
{
}
public override void DidFinishLaunching (NSNotification notification)
{
mainWindowController = new MainWindowController ();

Просмотреть файл

@ -15,10 +15,5 @@ namespace MessageSender
public MainWindow (NSCoder coder) : base (coder)
{
}
public override void AwakeFromNib ()
{
base.AwakeFromNib ();
}
}
}

Просмотреть файл

@ -1,13 +1,21 @@
using System;
using Foundation;
using AppKit;
using System.Runtime.InteropServices;
using CoreFoundation;
using Foundation;
namespace MessageSender
{
public partial class MainWindowController : NSWindowController
{
CFMessagePort msgPort;
public new MainWindow Window {
get {
return (MainWindow)base.Window;
}
}
public MainWindowController (IntPtr handle) : base (handle)
{
}
@ -23,33 +31,23 @@ namespace MessageSender
public override void AwakeFromNib ()
{
msgPort = CFMessagePortCreateRemote (IntPtr.Zero, PortName.Handle);
if (msgPort == IntPtr.Zero) {
NSAlert.WithMessage ("Unable to connect to port? Did you launch server first?", "OK", "", "", "").RunModal ();
NSApplication.SharedApplication.Terminate (this);
msgPort = CFMessagePort.CreateRemotePort (CFAllocator.Default, "com.example.app.port.server");
if (msgPort == null) {
var alert = new NSAlert {
MessageText = "Unable to connect to port? Did you launch server first?",
};
alert.AddButton ("OK");
alert.RunSheetModal (Window);
}
TheButton.Activated += SendMessage;
}
IntPtr msgPort;
static NSString PortName = (NSString)"com.example.app.port.server";
[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
extern static IntPtr CFMessagePortCreateRemote(IntPtr allocator, IntPtr name);
[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
extern static int CFMessagePortSendRequest(IntPtr remote, int msgid, IntPtr data, double sendTimeout, double rcvTimeout, IntPtr replyMode, IntPtr returnData);
void SendMessage (object sender, EventArgs e)
{
using (NSData data = NSData.FromString (TextField.StringValue))
{
CFMessagePortSendRequest (msgPort, 0x111, data.Handle, 10.0, 10.0, IntPtr.Zero, IntPtr.Zero);
using (var data = NSData.FromString (TextField.StringValue)) {
NSData responseData;
msgPort.SendRequest (0x111, data, 10.0, 10.0, (NSString)string.Empty, out responseData);
}
}
public new MainWindow Window {
get { return (MainWindow)base.Window; }
}
}
}

Просмотреть файл

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<SampleMetadata>
<ID>68d0d259-a479-407c-a7e3-dafecfc00c7c</ID>
<IsFullApplication>true</IsFullApplication>
<IsFullApplication>false</IsFullApplication>
<Level>Advanced</Level>
<Tags>Advanced, Cross-Platform Communication</Tags>
<SupportedPlatforms>Mac</SupportedPlatforms>
<Gallery>true</Gallery>
<Brief>This is an example showing use of NSMessagePort and CFMessagePort to send information cross process, between applications.</Brief>
</SampleMetadata>