Thanks it helped me a lot.
So i decided to make plugin that will work for only one client at the time, when client connected i will render his file and send back photos.
I make some progress but got problem with socket. When i want read from socket on client side it say it's closed.
Sorry for having so much work with me
Server Side code:
Imports System.Net.Sockets
Imports System.Text
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports System.Net
Imports System.IO
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD
Namespace serverACDrendering
Public Class Command
Dim close As Boolean = False 'when true server ends
Dim client As ClientWrapper 'queue that holds clients and their files
Dim editor As Editor = Application.DocumentManager.MdiActiveDocument.Editor 'editor for writing to command line
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Private Const portNum = 8000
Private Const ipServer = "127.0.0.1"
Private PACKET_SIZE As UInt16 = 4096
Dim path As String = "c:\renderPluginACD\redering.dwg"
<CommandMethod("StartServer")>
Public Sub StartServer()
editor.WriteMessage(vbNewLine & "starting ..." & vbNewLine)
' While (Not close)
client = New ClientWrapper
Receiver()
Render()
Transmitter()
System.Threading.Thread.Sleep(2000)
' End While
End Sub
Private Sub Render()
Dim Mstream As MemoryStream = client.GetFile
If (Not Directory.Exists("c:\renderPluginACD")) Then
Directory.CreateDirectory("c:\renderPluginACD")
End If
If (File.Exists(path)) Then
File.Delete(path)
End If
Dim fs As FileStream = File.OpenWrite(path)
Mstream.WriteTo(fs)
client.SetResult(fs)
fs.Close()
End Sub
'receive connection and put file in queue
Private Sub Receiver()
Dim tcpListener As TcpListener
Dim ip As IPAddress = IPAddress.Parse(ipServer)
tcpListener = New TcpListener(ip, portNum)
tcpListener.Start()
Try
Dim TcpClient = tcpListener.AcceptTcpClient()
If (TcpClient.Connected) Then
client.SetTcpClient(TcpClient)
Dim Reader As BinaryReader
Dim ReadBuffer(PACKET_SIZE) As Byte
Dim NData As Int32
Dim MStream As MemoryStream
Dim LData As Int32
Reader = New BinaryReader(TcpClient.GetStream)
NData = Reader.ReadInt32
MStream = New MemoryStream
While NData > 0
LData = TcpClient.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
MStream.Write(ReadBuffer, 0, LData)
NData -= LData
End While
editor.WriteMessage(vbNewLine & "received file ..." & vbNewLine)
client.SetFile(MStream)
End If
Catch e As Exception
End Try
'tcpListener.Stop()
End Sub
'send back the resutls
Private Sub Transmitter()
Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read)
Dim length As Integer = CType(fs.Length, Integer)
Dim reader As New BinaryReader(fs)
Dim buffer() As Byte ' Data buffer
Dim Writer As New BinaryWriter(client.GetTcpClient.GetStream)
Writer.Write(length)
Do
'read data from file
buffer = reader.ReadBytes(PACKET_SIZE)
'write data to Network Stream
Writer.Write(buffer)
Loop While buffer.Length = PACKET_SIZE
Writer.Flush()
Writer.Close()
reader.Close()
fs.Close()
End Sub
'stops server
<CommandMethod("StopServer")>
Public Sub StopServer()
close = False
End Sub
End Class
'wrapper for clients in queue
Public Class ClientWrapper
Dim tcpClient As TcpClient
Dim file As Object
Dim result As Object
Dim flag As Integer
Public Function GetTcpClient() As TcpClient
Return tcpClient
End Function
Public Sub SetTcpClient(_tcpClient As TcpClient)
tcpClient = _tcpClient
End Sub
Public Function GetFile() As Object
Return file
End Function
Public Sub SetFile(_file As Object)
file = _file
End Sub
Public Function GetResult() As Object
Return result
End Function
Public Sub SetResult(_result As Object)
result = _result
End Sub
End Class
End Namespace
Client side Code:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports System.Net.Sockets
Imports System.Text
Imports System
Imports System.IO
Imports Autodesk.AutoCAD.DatabaseServices
Imports app = Autodesk.AutoCAD.ApplicationServices.Application
Namespace Plugin1
Public Class Commands
Private Const portNum = 8000
Private Const ipServer = "127.0.0.1"
Private Const BUFFER_SIZE As Integer = 8192
Dim isActive As Boolean = False
Dim acDoc As Document
Dim receive As Object
Private PACKET_SIZE As UInt16 = 4096
Dim tcpClient As New System.Net.Sockets.TcpClient()
<CommandMethod("RenderOnHPC")>
Public Sub SendToRenderer()
Save()
Connect()
Send()
ReceivePhoto()
End Sub
Private Sub Save()
acDoc = Application.DocumentManager.MdiActiveDocument
Dim strDWGName As String = acDoc.Name
Dim obj As Object = Application.GetSystemVariable("DWGTITLED")
strDWGName = "c:\renderPluginACD\MyDrawing.dwg"
'Save the active drawing
If (Not Directory.Exists("c:\renderPluginACD")) Then
Directory.CreateDirectory("c:\renderPluginACD")
End If
acDoc.Database.SaveAs(strDWGName, False, DwgVersion.Current, acDoc.Database.SecurityParameters)
acDoc.Database.Dispose()
End Sub
Private Sub Connect()
tcpClient.Connect(ipServer, portNum)
End Sub
Private Sub Send()
Dim fs As FileStream = New FileStream("c:\renderPluginACD\MyDrawing.dwg", FileMode.Open, FileAccess.Read)
Dim length As Integer = CType(fs.Length, Integer)
Dim reader As New BinaryReader(fs)
Dim buffer() As Byte ' Data buffer
Try
tcpClient.GetStream()
Dim Writer As New BinaryWriter(tcpClient.GetStream())
Writer.Write(length)
Do
'read data from file
buffer = reader.ReadBytes(PACKET_SIZE)
'write data to Network Stream
Writer.Write(buffer)
Loop While buffer.Length = PACKET_SIZE
Writer.Flush()
Writer.Close()
reader.Close()
Catch ex As System.Exception
' Handle errors
End Try
fs.Close()
End Sub
Private Sub ReceivePhoto()
Dim renderedPath As String = "c:\renderPluginACD\return.dwg"
Dim reader2 As BinaryReader
Dim readBuffer(PACKET_SIZE) As Byte
Dim nData As Int32
Dim mStream As MemoryStream
Dim lData As Int32
reader2 = New BinaryReader(tcpClient.GetStream())
nData = reader2.ReadInt32
MStream = New MemoryStream
While NData > 0
LData = tcpClient.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
MStream.Write(ReadBuffer, 0, LData)
NData -= LData
End While
If (File.Exists(RenderedPath)) Then
File.Delete(RenderedPath)
End If
Dim fs As FileStream = File.OpenWrite(RenderedPath)
MStream.WriteTo(fs)
End Sub
End Class
End Namespace