public void StartListeningToEnergyMeter(BytesReceivedDelegate EnergyMeterDataReceived) { IPEndPoint LocalHostIPEnd = new IPEndPoint(localEndPoint.Address, targetEndPoint.Port + 1); SocketsHelper.SendMessageToUDPSocketAndListenToCallback(LocalHostIPEnd, targetEndPoint, null, EnergyMeterDataReceived); }
void OnReceivedBytesFromServer(NetworkMessage netMsg) { NetworkMessages.SendBytesToClient msg = netMsg.ReadMessage <NetworkMessages.SendBytesToClient>(); if (msg != null) { //Forward bytes to receive handler BytesReceivedDelegate _brd = GetBytesReceivedHandler(msg.byteMessageID); if (_brd != null) { _brd(msg.bytes); } } }
public HeartbeatInfo(Socket destinationSocket, byte[] heartbeatMessage, Regex responseFormat, BytesReceivedDelegate callbackDelegate, int pollInterval) { this.destinationSocket = destinationSocket; destinationSocket.ReceiveTimeout = 3; this.heartbeatMessage = heartbeatMessage; this.responseFormat = responseFormat; this.CallbackDelegate += callbackDelegate; if (heartbeatMessage != null) { this.timer = new Timer(); timer.Interval = pollInterval * 1000; timer.Elapsed += timer_Elapsed; timer.Enabled = true; } }
/// <summary> /// Sets the bytes received handler. Setting handler to null will remove a handler. /// </summary> /// <param name="byteMessageID">Byte message I.</param> /// <param name="handler">Handler.</param> public void SetBytesReceivedHandler(ushort byteMessageID, BytesReceivedDelegate handler) { //If handler is null, remove existing handler, if there is one if (handler == null) { if (GetBytesReceivedHandler(byteMessageID) != null) { bytesReceivedDelegates.Remove(byteMessageID); } } else { //Replace handler if there already is a handler if (GetBytesReceivedHandler(byteMessageID) != null) { bytesReceivedDelegates[byteMessageID] = handler; } //Add new handler if there is no handler at the moment else { bytesReceivedDelegates.Add(byteMessageID, handler); } } }
private static byte[] SendMessageToSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, byte[] bufferToSend, BytesReceivedDelegate callBack, ProtocolType protocol, byte[] heartbeatMessage, Regex responseFormat, BytesReceivedDelegate heartbeatCallback, bool isSynchronious, bool useSocketPool) { Socket TCPSocket = null; if (useSocketPool) { TCPSocket = CheckIfSocketAlreadyOpen(source, destination); } HeartbeatInfo info = null; try { if (TCPSocket == null) { if (protocol == ProtocolType.Udp) { TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, protocol); } else if (protocol == ProtocolType.Tcp) { TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocol); } TCPSocket.Bind(source); if (protocol == ProtocolType.Tcp) { TCPSocket.Connect(destination); if (useSocketPool) { info = new HeartbeatInfo(TCPSocket, heartbeatMessage, responseFormat, heartbeatCallback, 10); info.CallbackDelegate += new BytesReceivedDelegate(CheckIfHeartbeatFailed); lock (openSocketsList) { openSocketsList.Add(info); } } } } // lock (TCPSocket) { if (bufferToSend != null) { if (protocol == ProtocolType.Tcp) { TCPSocket.Send(bufferToSend); } else if (protocol == ProtocolType.Udp) { TCPSocket.SendTo(bufferToSend, destination); } } StateObject state = new StateObject(); if (isSynchronious) { TCPSocket.ReceiveTimeout = 5000; TCPSocket.Receive(state.buffer); if (!useSocketPool) { TCPSocket.Shutdown(SocketShutdown.Both); TCPSocket.Close(); } return(state.buffer); } else if (callBack != null) { state.workSocket = TCPSocket; state.callbackDelegate += callBack; if (info != null) { state.callbackDelegate += info.ProcessMessageReceivedFromSocket; } TCPSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } // } return(null); } catch (Exception ex) { LoggingHelper.LogExceptionInApplicationLog(ex.Source, ex, EventLogEntryType.Error); LoggingHelper.WriteExceptionLogEntry(ex.Source, ex); if (TCPSocket != null && TCPSocket.Connected) { TCPSocket.Shutdown(SocketShutdown.Both); TCPSocket.Close(); } } return(null); }
public static void SendMessageToSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, byte[] bufferToSend, BytesReceivedDelegate callBack, ProtocolType protocol) { SendMessageToSocketAndListenToCallback(source, destination, bufferToSend, callBack, protocol, null, null, null, false, true); }
public static void ConnectToTCPSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, BytesReceivedDelegate callBack, byte[] heartbeatMessage, Regex responseFormat, BytesReceivedDelegate heartbeatCallback) { SendMessageToTCPSocketAndListenToCallback(source, destination, null, callBack, heartbeatMessage, responseFormat, heartbeatCallback); }
public static void ConnectToTCPSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, BytesReceivedDelegate callBack) { SendMessageToTCPSocketAndListenToCallback(source, destination, null, callBack); }
public static void SendMessageToUDPSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, byte[] bufferToSend, BytesReceivedDelegate callBack, byte[] heartbeatMessage, Regex responseFormat, BytesReceivedDelegate heartbeatCallback) { SendMessageToSocketAndListenToCallback(source, destination, bufferToSend, callBack, ProtocolType.Udp, heartbeatMessage, responseFormat, heartbeatCallback, false, true); }
public static void SendMessageToUDPSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, byte[] bufferToSend, BytesReceivedDelegate callBack) { SendMessageToSocketAndListenToCallback(source, destination, bufferToSend, callBack, ProtocolType.Udp); }