/// <summary> /// Async send/receive thread constructor /// </summary> public ReceiveSendThread(Socket cs, Socket ss) { this.ClientSocket = cs; this.ServerSocket = ss; try { Task.Factory.StartNew(new Action(() => { ClientState clientState = new ClientState() { socket = ClientSocket }; ServerState serverState = new ServerState() { socket = ServerSocket }; ServerSocket.BeginReceive(serverState.buffer, 0, State.BufferSize, 0, new AsyncCallback(DataReceived), serverState); ClientSocket.BeginReceive(clientState.buffer, 0, State.BufferSize, 0, new AsyncCallback(DataReceived), clientState); })); } catch (Exception ex) { Logger.Log("Exception during the client/server receive procedure (" + ex.GetType() + ")!", LogType.EXCEPTION); Logger.Log("Please check if you changed state/buffer variables.", LogType.EXCEPTION); Program.WaitAndKill(); } }
/// <summary> /// Async send/receive thread /// </summary> public ReceiveSendThread(Socket cs, Socket ss) { this.ClientSocket = cs; this.ServerSocket = ss; Task.Factory.StartNew(new Action(() => { ClientState clientState = new ClientState() { socket = ClientSocket }; ServerState serverState = new ServerState() { socket = ServerSocket }; ServerSocket.BeginReceive(serverState.buffer, 0, State.BufferSize, 0, new AsyncCallback(DataReceived), serverState); ClientSocket.BeginReceive(clientState.buffer, 0, State.BufferSize, 0, new AsyncCallback(DataReceived), clientState); })); }
/// <summary> /// Send binary data to the server /// </summary> /// <param name="sendBuf">Buffer with the data to be sent</param> /// <param name="nrBytesToSend">Number of bytes to send from the buffer</param> /// <param name="receiveBuf">Buffer where the reply is to be stored</param> /// <returns></returns> public bool SendData(byte[] sendBuf, int nrBytesToSend, byte[] receiveBuf) { Received = false; if ((State)SM.CurrentState != State.Connected) { return(false); } try { RCVTimeout = false; SendTimer.Restart(); ServerSocket.BeginSend(sendBuf, 0, nrBytesToSend, SocketFlags.None, new AsyncCallback(SendCallback), ServerSocket); ServerSocket.BeginReceive(receiveBuf, 0, receiveBuf.Length, 0, new AsyncCallback(ReceiveCallback), ServerSocket); return(true); } catch { SendTimedOut = true; //when the send fails, the receive timout can be called immediately return(false); } }