private void ReceiveRestCallback(IAsyncResult ar) { try { /* retrieve the SocketStateObject */ SocketStateObject state = (SocketStateObject)ar.AsyncState; Socket socketFd = state.m_SocketFd; /* read data */ int readSize = socketFd.EndReceive(ar); state.m_Data = Protocol.combine(state.m_Data, state.m_DataBuf, readSize); state.m_DataBuf = new byte[state.size - state.m_Data.Length + 21]; // receive the rest of header if (state.m_Data.Length - 21 < state.size) { socketFd.BeginReceive(state.m_DataBuf, 0, state.size - state.m_Data.Length + 21, 0, new AsyncCallback(ReceiveRestCallback), state); } else { Protocol.Request req = new Protocol.Request(); Protocol.req_decode(ref req, state.m_Data); /* manage response */ feedback(ref req); setThreadedSendButton(true); setThreadedReceiveButton(true); } } catch (Exception exc) { MessageBox.Show("Exception:\t\n" + exc.Message.ToString()); setThreadedStatusLabel("Check \"Server Info\" and try again!"); setThreadedButton(true); } }
private void SendCallback(IAsyncResult ar) { try { /* retrieve the SocketStateObject */ SocketStateObject state = (SocketStateObject)ar.AsyncState; Socket socketFd = state.m_SocketFd; // Complete sending the data to the remote device. int bytesSent = socketFd.EndSend(ar); setThreadedTextBox("Sent " + bytesSent.ToString() + " bytes to server."); state.size -= bytesSent; if (state.size > 0) { client.s_socket.BeginSend(state.m_DataBuf, state.m_DataBuf.Length - state.size, state.size, 0, new AsyncCallback(SendCallback), state); } else { state.size = 21; state.m_DataBuf = new byte[state.size]; state.m_Data = new byte[0]; /* begin receiving the header */ socketFd.BeginReceive(state.m_DataBuf, 0, state.size, 0, new AsyncCallback(ReceiveCallback), state); } } catch (Exception e) { setThreadedStatusLabel(e.ToString()); } }
private void ConnectCallback(IAsyncResult ar) { try { /* retrieve the socket from the state object */ Socket socketFd = (Socket)ar.AsyncState; byte[] Buf; string Login, Password, mess; /* complete the connection */ socketFd.EndConnect(ar); /* create the SocketStateObject */ SocketStateObject state = new SocketStateObject(); state.m_SocketFd = socketFd; this.fd = socketFd; //state.m_StringBuilder = new StringBuilder( this.textBoxAddr.Text.ToString(), 8 ); setThreadedStatusLabel("Wait! Reading..."); //socketFd.Send(dataBuf, dataBuf.Length, 0); Login = this.textBoxLogin.Text.ToString(); Password = this.textBoxPassword.Text.ToString(); mess = Login + "\n" + Password + "\n"; Buf = Encoding.ASCII.GetBytes(mess); socketFd.Send(Buf, Buf.Length, 0); setThreadedButton(true); //state.m_SocketFd.BeginReceive(state.m_DataBuf, 0, SocketStateObject.BUF_SIZE, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception exc) { MessageBox.Show("Exception:\t\n" + exc.Message.ToString()); setThreadedStatusLabel("Check \"Server Info\" and try again!"); setThreadedButton(true); } }
private void ConnectCallback(IAsyncResult ar) { try { /* retrieve the socket from the state object */ Socket socketFd = (Socket)ar.AsyncState; /* complete the connection */ socketFd.EndConnect(ar); /* create the SocketStateObject */ SocketStateObject state = new SocketStateObject(); state.m_SocketFd = socketFd; setThreadedStatusLabel("Wait! Reading..."); /* begin receiving the data */ socketFd.BeginReceive(state.m_DataBuf, 0, SocketStateObject.BUF_SIZE, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception exc) { MessageBox.Show("Exception:\t\n" + exc.Message.ToString()); setThreadedStatusLabel("Check \"Server Info\" and try again!"); setThreadedButton(true); } }
private void sendRequest(ref Protocol.Request req) { /* send */ SocketStateObject state = new SocketStateObject(); state.m_SocketFd = this.client.s_socket; state.m_DataBuf = Protocol.req_encode(ref req); setThreadedStatusLabel("Wait! Sending information..."); state.size = state.m_DataBuf.Length; client.s_socket.BeginSend(state.m_DataBuf, 0, state.size, 0, new AsyncCallback(SendCallback), state); }
private void ReceiveCallback(IAsyncResult ar) { try { /* retrieve the SocketStateObject */ SocketStateObject state = (SocketStateObject)ar.AsyncState; Socket socketFd = state.m_SocketFd; /* read data */ int size = socketFd.EndReceive(ar); if (size > 0) { state.m_StringBuilder.Append(Encoding.ASCII.GetString(state.m_DataBuf, 0, size)); /* get the rest of the data */ socketFd.BeginReceive(state.m_DataBuf, 0, SocketStateObject.BUF_SIZE, 0, new AsyncCallback(ReceiveCallback), state); } else { /* all the data has arrived */ if (state.m_StringBuilder.Length > 1) { setThreadedTextBox(state.m_StringBuilder.ToString()); setThreadedStatusLabel("Done."); setThreadedButton(true); /* shutdown and close socket */ socketFd.Shutdown(SocketShutdown.Both); socketFd.Close(); } } } catch (Exception exc) { MessageBox.Show("Exception:\t\n" + exc.Message.ToString()); setThreadedStatusLabel("Check \"Server Info\" and try again!"); setThreadedButton(true); } }
private void ReceiveCallback(IAsyncResult ar) { try { /* retrieve the SocketStateObject */ SocketStateObject state = (SocketStateObject)ar.AsyncState; Socket socketFd = state.m_SocketFd; /* read data */ int readSize = socketFd.EndReceive(ar); state.m_Data = Protocol.combine(state.m_Data, state.m_DataBuf, readSize); state.m_DataBuf = new byte[state.size - state.m_Data.Length]; // receive the rest of header if (state.m_Data.Length < state.size) { socketFd.BeginReceive(state.m_DataBuf, 0, state.size - state.m_Data.Length, 0, new AsyncCallback(ReceiveCallback), state); } else { Protocol.Request req = new Protocol.Request(); if (state.m_Data.Length != state.size) { throw new Exception("Wrong size of data"); } Protocol.req_decode(ref req, state.m_Data, true); state.size = (int)req.header.size; state.m_DataBuf = new byte[state.size]; /*begin receiving the rest */ socketFd.BeginReceive(state.m_DataBuf, 0, state.size, 0, new AsyncCallback(ReceiveRestCallback), state); } } catch (Exception exc) { MessageBox.Show("Exception:\t\n" + exc.Message.ToString()); setThreadedStatusLabel("Check \"Server Info\" and try again!"); setThreadedButton(true); } }