/// <summary> /// Send a sequence of byte to server. /// </summary> /// <param name="buffer"> byte array to send. </param> /// <returns> boolean to check if success. </returns> public bool SendPacket(byte[] buffer) { if (mSocket == null || !mSocket.Connected) { Debug.Log("Must be connected to Send a message"); return(false); } if (buffer == null) { return(false); } byte[] encryptedBuffer = (byte[])JCS_CodecFactory.GetInstance().GetEncoder().Encode(buffer); try { mSocket.BeginSend( encryptedBuffer, 0, encryptedBuffer.Length, 0, new AsyncCallback(OnSendData), mSocket); } catch (Exception ex) { Debug.Log("Send Message Failed!: " + ex.Message); return(false); } return(true); }
/// <summary> /// Get the new data and send it out to all other connections. /// Note: If not data was recieved the connection has probably /// died. /// </summary> /// <param name="ar"></param> public void OnReceiveData(IAsyncResult ar) { // Socket was the passed in object Socket sock = (Socket)ar.AsyncState; // Check if we got any data try { int nBytesRec = sock.EndReceive(ar); if (nBytesRec > 0) { // Set decode Charset! byte[] buffer = GetBytesFromInputBuffer(0, nBytesRec); // send buffer to decoder and get the decrypted packet byte[] decryptedBuffer = (byte[])JCS_CodecFactory.GetInstance().GetDecoder().Decode(buffer); // invoke data to handler. if (mClientHandler != null) { mClientHandler.MessageReceived(decryptedBuffer); } // WARNING : The following line is NOT thread safe. Invoke is //m_lbRecievedData.Items.Add( sRecieved ); //Invoke(m_AddMessage, new string[] { sRecieved }); // If the connection is still usable restablish the callback SetupRecieveCallback(sock); } else { // If no data was recieved then the connection is probably dead Console.WriteLine("Client {0}, disconnected", sock.RemoteEndPoint); sock.Shutdown(SocketShutdown.Both); sock.Close(); } } catch (Exception ex) { Debug.LogError("Unusual error druing Recieve!: " + ex.Message); if (!SocketConnected(mSocket)) { mSocket.Close(); JCS_NetworkManager.SERVER_CLOSE = true; } } }
/// <summary> /// Callback when the data has sent. /// </summary> /// <param name="ar"></param> public void OnSendData(IAsyncResult ar) { // Socket was the passed in object Socket sock = (Socket)ar.AsyncState; try { // Complete sending the data to the remote device. int bytesSent = sock.EndSend(ar); if (bytesSent > 0) { // Set decode Charset! byte[] buffer = GetBytesFromOutputBuffer(0, bytesSent); // send buffer to decoder and get the decrypted packet byte[] encryptedBuffer = (byte[])JCS_CodecFactory.GetInstance().GetEncoder().Encode(buffer); // invoke data to handler. if (mClientHandler != null) { mClientHandler.MessageSent(encryptedBuffer); } } else { // If no data was recieved then the connection is probably dead Console.WriteLine("Client {0}, disconnected", sock.RemoteEndPoint); sock.Shutdown(SocketShutdown.Both); sock.Close(); } } catch (Exception ex) { Debug.LogError("Unusual error druing Send!: " + ex.Message); if (!SocketConnected(mSocket)) { mSocket.Close(); JCS_NetworkManager.SERVER_CLOSE = true; } } }
/// <summary> /// When receive the data. /// </summary> /// <param name="ar"></param> private void OnReceiveData(IAsyncResult ar) { // Socket was the passed in object Socket sock = (Socket)ar.AsyncState; try { // Read data from the remote device. int bytesRead = sock.EndReceive(ar); if (bytesRead > 0) { byte[] buffer = GetBytesFromInputBuffer(0, bytesRead); // send buffer to decoder and get the decrypted packet byte[] decryptedBuffer = (byte[])JCS_CodecFactory.GetInstance().GetDecoder().Decode(buffer); // invoke data to handler. if (mClientHandler != null) { mClientHandler.MessageReceived(decryptedBuffer); } } else { // .. Debug.Log("Receive buffer that length is lower than 0."); } } catch (Exception ex) { // If no data was recieved then the connection is probably dead Debug.LogError("Server OnReceive failed: " + ex.Message); Close(); } SetupRecieveCallback(sock); }