1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace HPSocketCS
- {
- public class TcpAgentEvent
- {
- public delegate HandleResult OnConnectEventHandler(IntPtr connId );
- public delegate HandleResult OnSendEventHandler(IntPtr connId, byte[] bytes);
- public delegate HandleResult OnReceiveEventHandler(IntPtr connId, byte[] bytes);
- public delegate HandleResult OnCloseEventHandler(IntPtr connId, SocketOperation enOperation, int errorCode);
- public delegate HandleResult OnShutdownEventHandler();
- public delegate HandleResult OnPrepareConnectEventHandler(IntPtr connId , uint socket);
- }
- public class TcpAgent
- {
- protected IntPtr _pAgent = IntPtr.Zero;
- protected IntPtr pAgent
- {
- get
- {
-
-
-
-
- return _pAgent;
- }
- set
- {
- _pAgent = value;
- }
- }
- protected IntPtr pListener = IntPtr.Zero;
-
-
-
- public TcpAgentEvent.OnConnectEventHandler OnConnect;
-
-
-
- public TcpAgentEvent.OnSendEventHandler OnSend;
-
-
-
- public TcpAgentEvent.OnPrepareConnectEventHandler OnPrepareConnect;
-
-
-
- public TcpAgentEvent.OnReceiveEventHandler OnReceive;
-
-
-
- public TcpAgentEvent.OnCloseEventHandler OnClose;
-
-
-
- public TcpAgentEvent.OnShutdownEventHandler OnShutdown;
- protected bool IsCreate = false;
- public TcpAgent()
- {
- CreateListener();
- }
- ~TcpAgent()
- {
- Destroy();
- }
-
-
-
-
- protected virtual bool CreateListener()
- {
- if (IsCreate == true || pListener != IntPtr.Zero || pAgent != IntPtr.Zero)
- {
- return false;
- }
- pListener = Sdk.Create_HP_TcpAgentListener();
- if (pListener == IntPtr.Zero)
- {
- return false;
- }
- pAgent = Sdk.Create_HP_TcpAgent(pListener);
- if (pAgent == IntPtr.Zero)
- {
- return false;
- }
- IsCreate = true;
- return true;
- }
-
-
-
- public virtual void Destroy()
- {
- Stop();
- if (pAgent != IntPtr.Zero)
- {
- Sdk.Destroy_HP_TcpAgent(pAgent);
- pAgent = IntPtr.Zero;
- }
- if (pListener != IntPtr.Zero)
- {
- Sdk.Destroy_HP_TcpAgentListener(pListener);
- pListener = IntPtr.Zero;
- }
- IsCreate = false;
- }
-
-
-
-
-
-
-
- public bool Start(string address, bool async = false)
- {
- if (string.IsNullOrEmpty(address) == true)
- {
- throw new Exception("address is null");
- }
- if (IsCreate == false)
- {
- return false;
- }
- if (IsStarted == true)
- {
- return false;
- }
- SetCallback();
- return Sdk.HP_Agent_Start(pAgent, address, async);
- }
-
-
-
-
- public bool Stop()
- {
- if (IsStarted == false)
- {
- return false;
- }
- return Sdk.HP_Agent_Stop(pAgent);
- }
-
-
-
-
-
-
-
- public bool Connect(string address, ushort port, ref IntPtr connId)
- {
- return Sdk.HP_Agent_Connect(pAgent, address, port, ref connId);
- }
-
-
-
-
-
-
-
- public bool Send(IntPtr connId, byte[] bytes, int size)
- {
- return Sdk.HP_Agent_Send(pAgent, connId, bytes, size);
- }
-
-
-
-
-
-
-
- public bool Send<T>(IntPtr connId, T obj)
- {
- byte[] buffer = StructureToByte<T>(obj);
- return Send(connId, buffer, buffer.Length);
- }
-
-
-
-
-
-
-
- public bool SendBySerializable(IntPtr connId, object obj)
- {
- byte[] buffer = ObjectToBytes(obj);
- return Send(connId, buffer, buffer.Length);
- }
-
-
-
-
-
-
-
- public bool Send(IntPtr connId, IntPtr bufferPtr, int size)
- {
- return Sdk.HP_Agent_Send(pAgent, connId, bufferPtr, size);
- }
-
-
-
-
-
-
-
-
- public bool Send(IntPtr connId, byte[] bytes, int offset, int size)
- {
- return Sdk.HP_Agent_SendPart(pAgent, connId, bytes, size, offset);
- }
-
-
-
-
-
-
-
-
- public bool Send(IntPtr connId, IntPtr bufferPtr, int offset, int size)
- {
- return Sdk.HP_Agent_SendPart(pAgent, connId, bufferPtr, size, offset);
- }
-
-
-
-
-
-
-
-
-
- public bool SendPackets(IntPtr connId, WSABUF[] pBuffers, int count)
- {
- return Sdk.HP_Agent_SendPackets(pAgent, connId, pBuffers, count);
- }
-
-
-
-
-
-
-
-
-
- public bool SendPackets<T>(IntPtr connId, T[] objects)
- {
- bool ret = false;
- WSABUF[] buffer = new WSABUF[objects.Length];
- IntPtr[] ptrs = new IntPtr[buffer.Length];
- try
- {
- for (int i = 0; i < objects.Length; i++)
- {
- buffer[i].Length = Marshal.SizeOf(typeof(T));
- ptrs[i] = Marshal.AllocHGlobal(buffer[i].Length);
- Marshal.StructureToPtr(objects[i], ptrs[i], true);
- buffer[i].Buffer = ptrs[i];
- }
- ret = SendPackets(connId, buffer, buffer.Length);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- for (int i = 0; i < ptrs.Length; i++)
- {
- if (ptrs[i] != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(ptrs[i]);
- }
- }
- }
- return ret;
- }
-
-
-
-
-
-
-
-
-
- public bool SendSmallFile(IntPtr connId, string filePath, ref WSABUF head, ref WSABUF tail)
- {
- return Sdk.HP_TcpAgent_SendSmallFile(pAgent, connId, filePath, ref head, ref tail);
- }
-
-
-
-
-
-
-
-
-
- public bool SendSmallFile(IntPtr connId, string filePath, byte[] head, byte[] tail)
- {
- IntPtr pHead = IntPtr.Zero;
- IntPtr pTail = IntPtr.Zero;
- WSABUF wsaHead = new WSABUF() { Length = 0, Buffer = pHead };
- WSABUF wsatail = new WSABUF() { Length = 0, Buffer = pTail };
- if (head != null)
- {
- wsaHead.Length = head.Length;
- wsaHead.Buffer = Marshal.UnsafeAddrOfPinnedArrayElement(head, 0);
- }
- if (tail != null)
- {
- wsaHead.Length = tail.Length;
- wsaHead.Buffer = Marshal.UnsafeAddrOfPinnedArrayElement(tail, 0);
- }
- return SendSmallFile(connId, filePath, ref wsaHead, ref wsatail);
- }
-
-
-
-
-
-
-
-
-
- public bool SendSmallFile<T1, T2>(IntPtr connId, string filePath, T1 head, T2 tail)
- {
- byte[] headBuffer = null;
- if (head != null)
- {
- headBuffer = StructureToByte<T1>(head);
- }
- byte[] tailBuffer = null;
- if (tail != null)
- {
- tailBuffer = StructureToByte<T2>(tail);
- }
- return SendSmallFile(connId, filePath, headBuffer, tailBuffer);
- }
-
-
-
-
-
-
- public bool Disconnect(IntPtr connId, bool force = true)
- {
- return Sdk.HP_Agent_Disconnect(pAgent, connId, force);
- }
-
-
-
-
-
-
- public bool DisconnectLongConnections(uint period, bool force = true)
- {
- return Sdk.HP_Agent_DisconnectLongConnections(pAgent, period, force);
- }
-
-
-
-
-
-
- public bool DisconnectSilenceConnections(uint period, bool force = true)
- {
- return Sdk.HP_Agent_DisconnectSilenceConnections(pAgent, period, force);
- }
-
-
-
-
-
-
- public bool SetConnectionExtra(IntPtr connId, object obj)
- {
- IntPtr ptr = IntPtr.Zero;
-
- if (Sdk.HP_Agent_GetConnectionExtra(pAgent, connId, ref ptr) && ptr != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(ptr);
- ptr = IntPtr.Zero;
- }
- if (obj != null)
- {
-
- ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj));
- Marshal.StructureToPtr(obj, ptr, false);
- }
- return Sdk.HP_Agent_SetConnectionExtra(pAgent, connId, ptr);
- }
-
-
-
-
-
-
-
-
- public bool GetConnectionExtra(IntPtr connId, ref IntPtr ptr)
- {
- return Sdk.HP_Agent_GetConnectionExtra(pAgent, connId, ref ptr) && ptr != IntPtr.Zero;
- }
-
-
-
- public SocketError ErrorCode
- {
- get
- {
- return Sdk.HP_Agent_GetLastError(pAgent);
- }
- }
-
-
-
- public string ErrorMessage
- {
- get
- {
- IntPtr ptr = Sdk.HP_Agent_GetLastErrorDesc(pAgent);
- string desc = Marshal.PtrToStringUni(ptr);
- return desc;
- }
- }
-
-
-
-
-
-
- public bool GetPendingDataLength(IntPtr connId, ref int length)
- {
- return Sdk.HP_Agent_GetPendingDataLength(pAgent, connId, ref length);
- }
-
- public bool IsStarted
- {
- get
- {
- if (pAgent == IntPtr.Zero)
- {
- return false;
- }
- return Sdk.HP_Agent_HasStarted(pAgent);
- }
- }
-
-
-
- public ServiceState State
- {
- get
- {
- return Sdk.HP_Agent_GetState(pAgent);
- }
- }
- public uint ConnectionCount
- {
- get
- {
- return Sdk.HP_Agent_GetConnectionCount(pAgent);
- }
- }
-
-
-
-
- public IntPtr[] GetAllConnectionIDs()
- {
- IntPtr[] arr = null;
- do
- {
- uint count = ConnectionCount;
- if (count == 0)
- {
- break;
- }
- arr = new IntPtr[count];
- if (Sdk.HP_Agent_GetAllConnectionIDs(pAgent, arr, ref count))
- {
- if (arr.Length > count)
- {
- IntPtr[] newArr = new IntPtr[count];
- Array.Copy(arr, newArr, count);
- arr = newArr;
- }
- break;
- }
- } while (true);
- return arr;
- }
-
-
-
-
-
-
- public bool GetLocalAddress(IntPtr connId, ref string ip, ref ushort port)
- {
- int ipLength = 40;
- StringBuilder sb = new StringBuilder(ipLength);
- bool ret = Sdk.HP_Agent_GetLocalAddress(pAgent, connId, sb, ref ipLength, ref port);
- if (ret == true)
- {
- ip = sb.ToString();
- }
- return ret;
- }
-
-
-
-
- public bool GetRemoteAddress(IntPtr connId, ref string ip, ref ushort port)
- {
- int ipLength = 40;
- StringBuilder sb = new StringBuilder(ipLength);
- bool ret = Sdk.HP_Agent_GetRemoteAddress(pAgent, connId, sb, ref ipLength, ref port);
- if (ret == true)
- {
- ip = sb.ToString();
- }
- return ret;
- }
-
-
-
-
-
-
- public bool GetConnectPeriod(IntPtr connId, ref uint period)
- {
- return Sdk.HP_Agent_GetConnectPeriod(pAgent, connId, ref period);
- }
-
-
-
-
-
-
- public bool GetSilencePeriod(IntPtr connId, ref uint period)
- {
- return Sdk.HP_Agent_GetSilencePeriod(pAgent, connId, ref period);
- }
-
-
-
-
- public bool IsReuseAddress
- {
- get
- {
- return Sdk.HP_TcpAgent_IsReuseAddress(pAgent);
- }
- set
- {
- Sdk.HP_TcpAgent_SetReuseAddress(pAgent, value);
- }
- }
-
-
-
- public uint WorkerThreadCount
- {
- get
- {
- return Sdk.HP_Agent_GetWorkerThreadCount(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetWorkerThreadCount(pAgent, value);
- }
- }
-
-
-
- public uint SocketBufferSize
- {
- get
- {
- return Sdk.HP_TcpAgent_GetSocketBufferSize(pAgent);
- }
- set
- {
- Sdk.HP_TcpAgent_SetSocketBufferSize(pAgent, value);
- }
- }
-
-
-
- public uint FreeSocketObjLockTime
- {
- get
- {
- return Sdk.HP_Agent_GetFreeSocketObjLockTime(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetFreeSocketObjLockTime(pAgent, value);
- }
- }
-
-
-
- public uint FreeSocketObjPool
- {
- get
- {
- return Sdk.HP_Agent_GetFreeSocketObjPool(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetFreeSocketObjPool(pAgent, value);
- }
- }
-
-
-
- public uint FreeBufferObjPool
- {
- get
- {
- return Sdk.HP_Agent_GetFreeBufferObjPool(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetFreeBufferObjPool(pAgent, value);
- }
- }
-
-
-
- public uint FreeSocketObjHold
- {
- get
- {
- return Sdk.HP_Agent_GetFreeSocketObjHold(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetFreeSocketObjHold(pAgent, value);
- }
- }
-
-
-
- public uint FreeBufferObjHold
- {
- get
- {
- return Sdk.HP_Agent_GetFreeBufferObjHold(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetFreeBufferObjHold(pAgent, value);
- }
- }
-
-
-
- public uint KeepAliveTime
- {
- get
- {
- return Sdk.HP_TcpAgent_GetKeepAliveTime(pAgent);
- }
- set
- {
- Sdk.HP_TcpAgent_SetKeepAliveTime(pAgent, value);
- }
- }
-
-
-
- public uint KeepAliveInterval
- {
- get
- {
- return Sdk.HP_TcpAgent_GetKeepAliveInterval(pAgent);
- }
- set
- {
- Sdk.HP_TcpAgent_SetKeepAliveInterval(pAgent, value);
- }
- }
-
-
-
- public bool IsMarkSilence
- {
- get
- {
- return Sdk.HP_Agent_IsMarkSilence(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetMarkSilence(pAgent, value);
- }
- }
-
-
-
- public SendPolicy SendPolicy
- {
- get
- {
- return Sdk.HP_Agent_GetSendPolicy(pAgent);
- }
- set
- {
- Sdk.HP_Agent_SetSendPolicy(pAgent, value);
- }
- }
-
-
-
-
-
- public string GetSocketErrorDesc(SocketError code)
- {
- IntPtr ptr = Sdk.HP_GetSocketErrorDesc(code);
- string desc = Marshal.PtrToStringUni(ptr);
- return desc;
- }
-
- Sdk.OnPrepareConnect _OnPrepareConnect = null;
- Sdk.OnConnect _OnConnect = null;
- Sdk.OnReceive _OnReceive = null;
- Sdk.OnSend _OnSend = null;
- Sdk.OnClose _OnClose = null;
- Sdk.OnShutdown _OnShutdown = null;
-
-
-
- protected virtual void SetCallback()
- {
- _OnPrepareConnect = new Sdk.OnPrepareConnect(SDK_OnPrepareConnect);
- _OnConnect = new Sdk.OnConnect(SDK_OnConnect);
- _OnSend = new Sdk.OnSend(SDK_OnSend);
- _OnReceive = new Sdk.OnReceive(SDK_OnReceive);
- _OnClose = new Sdk.OnClose(SDK_OnClose);
- _OnShutdown = new Sdk.OnShutdown(SDK_OnShutdown);
-
- Sdk.HP_Set_FN_Agent_OnPrepareConnect(pListener, _OnPrepareConnect);
- Sdk.HP_Set_FN_Agent_OnConnect(pListener, _OnConnect);
- Sdk.HP_Set_FN_Server_OnSend(pListener, _OnSend);
- Sdk.HP_Set_FN_Server_OnReceive(pListener, _OnReceive);
- Sdk.HP_Set_FN_Server_OnClose(pListener, _OnClose);
- Sdk.HP_Set_FN_Agent_OnShutdown(pListener, _OnShutdown);
- }
- protected virtual HandleResult SDK_OnPrepareConnect(IntPtr connId, uint socket)
- {
- if (OnPrepareConnect != null)
- {
- return OnPrepareConnect(connId, socket);
- }
- return HandleResult.Ignore;
- }
- protected virtual HandleResult SDK_OnConnect(IntPtr connId)
- {
- if (OnConnect != null)
- {
- return OnConnect(connId);
- }
- return HandleResult.Ignore;
- }
- protected virtual HandleResult SDK_OnSend(IntPtr connId, IntPtr pData, int length)
- {
- if (OnSend != null)
- {
- byte[] bytes = new byte[length];
- Marshal.Copy(pData, bytes, 0, length);
- return OnSend(connId, bytes);
- }
- return HandleResult.Ignore;
- }
- protected virtual HandleResult SDK_OnReceive(IntPtr connId, IntPtr pData, int length)
- {
- if (OnReceive != null)
- {
- byte[] bytes = new byte[length];
- Marshal.Copy(pData, bytes, 0, length);
- return OnReceive(connId, bytes);
- }
- return HandleResult.Ignore;
- }
- protected virtual HandleResult SDK_OnClose(IntPtr connId, SocketOperation enOperation, int errorCode)
- {
- if (OnClose != null)
- {
- return OnClose(connId, enOperation, errorCode);
- }
- return HandleResult.Ignore;
- }
- protected virtual HandleResult SDK_OnShutdown()
- {
- if (OnShutdown != null)
- {
- return OnShutdown();
- }
- return HandleResult.Ignore;
- }
-
-
-
-
-
- public int SYSGetLastError()
- {
- return Sdk.SYS_GetLastError();
- }
-
-
-
-
- public int SYSWSAGetLastError()
- {
- return Sdk.SYS_WSAGetLastError();
- }
-
-
-
-
-
-
-
-
-
-
- public int SYS_SetSocketOption(IntPtr sock, int level, int name, IntPtr val, int len)
- {
- return Sdk.SYS_SetSocketOption(sock, level, name, val, len);
- }
-
-
-
-
-
-
-
-
-
-
- public int SYSGetSocketOption(IntPtr sock, int level, int name, IntPtr val, ref int len)
- {
- return Sdk.SYS_GetSocketOption(sock, level, name, val, ref len);
- }
-
-
-
-
-
-
-
-
- public int SYSIoctlSocket(IntPtr sock, long cmd, IntPtr arg)
- {
- return Sdk.SYS_IoctlSocket(sock, cmd, arg);
- }
-
-
-
-
-
-
-
-
-
-
-
- public int SYS_WSAIoctl(IntPtr sock, uint dwIoControlCode, IntPtr lpvInBuffer, uint cbInBuffer,
- IntPtr lpvOutBuffer, uint cbOutBuffer, uint lpcbBytesReturned)
- {
- return Sdk.SYS_WSAIoctl(sock, dwIoControlCode, lpvInBuffer, cbInBuffer,
- lpvOutBuffer, cbOutBuffer, lpcbBytesReturned);
- }
-
-
-
- public byte[] StructureToByte<T>(T structure)
- {
- int size = Marshal.SizeOf(typeof(T));
- byte[] buffer = new byte[size];
- IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.StructureToPtr(structure, bufferIntPtr, true);
- Marshal.Copy(bufferIntPtr, buffer, 0, size);
- }
- finally
- {
- Marshal.FreeHGlobal(bufferIntPtr);
- }
- return buffer;
- }
-
-
-
- public T ByteToStructure<T>(byte[] dataBuffer)
- {
- object structure = null;
- int size = Marshal.SizeOf(typeof(T));
- IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
- structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
- }
- finally
- {
- Marshal.FreeHGlobal(allocIntPtr);
- }
- return (T)structure;
- }
-
-
-
-
-
- public byte[] ObjectToBytes(object obj)
- {
- using (MemoryStream ms = new MemoryStream())
- {
- IFormatter formatter = new BinaryFormatter();
- formatter.Serialize(ms, obj);
- return ms.GetBuffer();
- }
- }
-
-
-
-
-
- public object BytesToObject(byte[] bytes)
- {
- using (MemoryStream ms = new MemoryStream(bytes))
- {
- IFormatter formatter = new BinaryFormatter();
- return formatter.Deserialize(ms);
- }
- }
-
-
-
-
-
-
- public T BytesToStruct<T>(byte[] bytes)
- {
- Type strcutType = typeof(T);
- int size = Marshal.SizeOf(strcutType);
- IntPtr buffer = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.Copy(bytes, 0, buffer, size);
- return (T)Marshal.PtrToStructure(buffer, strcutType);
- }
- finally
- {
- Marshal.FreeHGlobal(buffer);
- }
- }
- }
- }
|