/// <inheritdoc /> public UdpClientApm(ushort maxPacketSize = Constants.UDP_PACKET_SIZE_MAX) { _stateObj = new ClientStateObject( new byte[(maxPacketSize > 0) & (maxPacketSize < Constants.UDP_PACKET_SIZE_MAX) ? maxPacketSize : Constants.UDP_PACKET_SIZE_MAX]); }
/// <summary> /// Receive asynchronous. /// </summary> private protected override void ReceiveAsync() { if ((_state & RECEIVE_FLAG) == RECEIVE_FLAG) { ClientStateObject state = _clientStateObjectPool.Rent() ?? new ClientStateObject(new byte[MaxPayloadSize + Constants.UDP_HEADER_OFFSET]); try { _clientSocket !.BeginReceive( state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ReceiveAsyncCallback, state); } catch (ObjectDisposedException) { Disconnect(DisconnectReason.Aborted); _clientStateObjectPool.Return(state); } catch (SocketException) { Disconnect(DisconnectReason.Error); _clientStateObjectPool.Return(state); } catch { Disconnect(DisconnectReason.Unspecified); _clientStateObjectPool.Return(state); } } }
void SendStringToServer(string txtCmd) { TcpClient client = new TcpClient(); ClientStateObject obj = new ClientStateObject(client, txtCmd); System.Timers.Timer timTimeOut = new System.Timers.Timer(600); System.Threading.Timer tmTimeOut = new System.Threading.Timer(new System.Threading.TimerCallback(tmTimeOut_Elapsed), client, 1000, System.Threading.Timeout.Infinite); IAsyncResult foo = client.BeginConnect(IPAddress.Loopback, 19080, new AsyncCallback(ConnectCallback), obj); }
private void ReceiveAsyncCallback(IAsyncResult iar) { int bytesTransferred; try { if ((bytesTransferred = _clientSocket !.EndReceive(iar)) <= 0) { Disconnect(DisconnectReason.Graceful); return; } } catch (ObjectDisposedException) { Disconnect(DisconnectReason.Aborted); return; } catch (SocketException) { Disconnect(DisconnectReason.Error); return; } catch { Disconnect(DisconnectReason.Unspecified); return; } ReceiveAsync(); ClientStateObject state = (ClientStateObject)iar.AsyncState !; if (Serialization.Serialization.DeserializeUdp( state.Buffer, bytesTransferred, _bigDataHandler, i => i, out DeserializePacketInfo deserializePacketInfo)) { DeserializeData(in deserializePacketInfo); } _clientStateObjectPool.Return(state); }
void ConnectCallback(IAsyncResult ar) { Functions.WriteLineToLogFileIfAdvanced("IRComm: connected to IR Server"); try { ClientStateObject obj = (ClientStateObject)ar.AsyncState; TcpClient client = obj.Client; NetworkStream stream = client.GetStream(); // Buffer for reading data Byte[] bytes = new Byte[256]; String strCommand = string.Empty; Functions.WriteLineToLogFileIfAdvanced("IRComm: Sending: [" + obj.Command + "]"); byte[] streamBytes = Encoding.ASCII.GetBytes(obj.Command); stream.Write(streamBytes, 0, streamBytes.Length); Functions.WriteLineToLogFileIfAdvanced("IRComm: Sent."); client.Close(); } catch { } // ObjectDisposedException if socket is already closed }
public void Connect(IPEndPoint epConnect) { sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sSocket.SendTimeout = 5000; sSocket.ReceiveTimeout = 5000; sSocket.Connect(epConnect); cStateObject = new ClientStateObject(); cStateObject.Parent = this; cStateObject.StateType = StateObjectType.Client; cStateObject.ClientSocket = sSocket; OnClientConnected(cStateObject); try { sSocket.BeginReceive(cStateObject.headerBuffer, 0, cStateObject.headerBuffer.Length, 0, new AsyncCallback(Core.ReadCallback), cStateObject); } catch { OnClientDisconnected(cStateObject); } }