Created
June 6, 2018 10:56
-
-
Save pwxcoo/7f7bf51efd04a58e7178abaf175f3440 to your computer and use it in GitHub Desktop.
socket corresponding in Revit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ClientDemo | |
{ | |
class Client | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
int port = 2000; | |
string host = "127.0.0.1"; | |
IPAddress ip = IPAddress.Parse(host); | |
IPEndPoint ipe = new IPEndPoint(ip, port); | |
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
Console.WriteLine("Connecting..."); | |
c.Connect(ipe); | |
string sendStr = ""; | |
byte[] bs; | |
//向服务器发送一个文件路径 | |
sendStr = @"C:\Program Files\Autodesk\Revit 2018\Samples\Arch Link Model.rvt"; | |
bs = Encoding.ASCII.GetBytes(sendStr); | |
Console.WriteLine("Send message:" + sendStr); | |
c.Send(bs, bs.Length, 0); | |
Thread.Sleep(1000); | |
//向服务器发送另一个文件路径 | |
sendStr = @"C:\Program Files\Autodesk\Revit 2018\Samples\rac_advanced_sample_project.rvt"; | |
bs = Encoding.ASCII.GetBytes(sendStr); | |
Console.WriteLine("Send message:" + sendStr); | |
c.Send(bs, bs.Length, 0); | |
Console.ReadLine(); | |
c.Close(); | |
} | |
catch (ArgumentException e) | |
{ | |
Console.WriteLine("argumentNullException:{0}", e); | |
} | |
catch (SocketException e) | |
{ | |
Console.WriteLine("SocketException:{0}", e); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace SocketDemo | |
{ | |
internal class Communicator | |
{ | |
private int target_port = 2000; | |
private string target_host = "127.0.0.1"; | |
private Socket server_socket = null; | |
private int connection_max = 10; | |
internal Communicator() | |
{ | |
} | |
internal Communicator(string ip, int port) | |
{ | |
this.target_host = ip; | |
this.target_port = port; | |
} | |
internal int Port | |
{ | |
get { return target_port; } | |
set { target_port = value; } | |
} | |
internal string Host | |
{ | |
get { return target_host; } | |
set { target_host = value; } | |
} | |
internal int MaxConnection | |
{ | |
get { return connection_max; } | |
set { connection_max = value; } | |
} | |
//建立与客户端的连接 | |
internal bool StartListening() | |
{ | |
try | |
{ | |
IPAddress ip_address = IPAddress.Parse(target_host); | |
IPEndPoint ip_endpoint = new IPEndPoint(ip_address, target_port); | |
server_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
server_socket.Bind(ip_endpoint); | |
server_socket.Listen(connection_max); | |
Thread listenThread = new Thread(listen); | |
listenThread.Start(); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
return false; | |
} | |
} | |
internal bool StartListening(string ip, int port) | |
{ | |
this.target_host = ip; | |
this.target_port = port; | |
return StartListening(); | |
} | |
//监听客户端消息 | |
private void listen() | |
{ | |
while (true) | |
{ | |
Socket clientSocket = server_socket.Accept(); | |
Thread receiveThread = new Thread(receive); | |
receiveThread.Start(clientSocket); | |
} | |
} | |
//接收客户端消息 | |
private void receive(object socket) | |
{ | |
Socket clientSocket = socket as Socket; | |
if (clientSocket == null) | |
return; | |
while (true) | |
{ | |
try | |
{ | |
string receiveStr = ""; | |
byte[] receiveBytes = new byte[1024]; | |
int bytes = clientSocket.Receive(receiveBytes, receiveBytes.Length, 0); | |
receiveStr += Encoding.ASCII.GetString(receiveBytes, 0, bytes); | |
if (receiveStr != "") | |
{ | |
//客户端消息入列 | |
MessageStation.Messages.AppendMessage(Thread.CurrentThread.ManagedThreadId, receiveStr); | |
} | |
} | |
catch (Exception ex) | |
{ | |
clientSocket.Shutdown(SocketShutdown.Both); | |
clientSocket.Close(); | |
MessageBox.Show(ex.ToString()); | |
break; | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Autodesk.Revit.ApplicationServices; | |
using Autodesk.Revit.DB; | |
using Autodesk.Revit.DB.Events; | |
using Autodesk.Revit.UI; | |
using Autodesk.Revit.UI.Events; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SocketDemo | |
{ | |
public class Exporter : IExternalApplication | |
{ | |
private UIApplication uiApp = null; | |
public Result OnStartup(UIControlledApplication application) | |
{ | |
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; | |
// 创建并启动第二部分创建的服务器 | |
Communicator server = new Communicator("127.0.0.1", 2000); | |
server.StartListening(); | |
application.ControlledApplication.ApplicationInitialized += OnApplicationInitialized; | |
return Result.Succeeded; | |
} | |
public Result OnShutdown(UIControlledApplication application) | |
{ | |
return Result.Succeeded; | |
} | |
private void OnApplicationInitialized(object sender, ApplicationInitializedEventArgs e) | |
{ | |
//获取UIApplication对象 | |
Application app = sender as Application; | |
uiApp = new UIApplication(app); | |
uiApp.Idling += HandleMessage; | |
} | |
//循环处理消息 | |
private void HandleMessage(object sender, IdlingEventArgs e) | |
{ | |
if (MessageStation.Messages.HasMessage) | |
{ | |
KeyValuePair<int, string> message = MessageStation.Messages.GetMessage(); | |
try | |
{ | |
Document document = uiApp.Application.OpenDocumentFile(message.Value); | |
TaskDialog.Show("document", document.PathName); | |
} | |
catch (Exception ex) | |
{ | |
TaskDialog.Show("error", ex.ToString()); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SocketDemo | |
{ | |
internal class MessageStation | |
{ | |
//构造函数私有化 | |
private MessageStation() | |
{ | |
_messages = new Queue<KeyValuePair<int, string>>(); | |
} | |
//C#语言特性实现单例 | |
internal static readonly MessageStation Messages = new MessageStation(); | |
private Queue<KeyValuePair<int, string>> _messages = null; | |
//是否有消息 | |
internal bool HasMessage | |
{ | |
get { return _messages.Count > 0; } | |
} | |
//入列 | |
internal void AppendMessage(int clientId, string message) | |
{ | |
_messages.Enqueue(new KeyValuePair<int, string>(clientId, message)); | |
} | |
//出列 | |
internal KeyValuePair<int, string> GetMessage() | |
{ | |
if (_messages.Count > 0) | |
return _messages.Dequeue(); | |
return new KeyValuePair<int, string>(-1, "no message"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A demo about use socket for corresponding in Revit.
Reference: